wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CSE 102 Exam 3 Practice

Total questions: 18

Worksheet time: 10mins

Name
Class
Date
1.

What is the correct way to open a file for reading in Python? (Assume there is a variable filename that contains the filename)

a)

open(filename, 'r')

b)

open(filename, 'w')

c)

with open(filename, 'r') as openFile:

d)

with open(filename, 'r')

2.

What is the correct syntax to define a user-defined function in Python?

a)

function myFunction():

b)

def myFunction():

c)

create myFunction():

d)

define myFunction():

3.

For the following function, what are the parameters? (select all that apply)

a)

val1

b)

val2

c)

val3

d)

val4

e)

val5

4.

Which of the following is a valid way to create a dictionary in Python?

a)

my_dict = {1, 2}

b)

my_dict = [1: 'a', 2: 'b']

c)

my_dict = {'a': 1, 'b': 2}

d)

my_dict = {'a' =1, 'b' = 2}

5.

Which function can be used to skip (and if desired gather) a row in a csv file.

a)

next()

b)

skip()

c)

pass()

d)

read()

6.

How do you read all lines from a text file into a list in Python?

a)

file.readlines()

b)

file.read()

c)

file.readlist()

d)

file.read_all()

7.

What is the correct way to update a value in a dictionary?

a)

b)

c)

d)

8.

What is the difference between parameters and arguments?

a)

Arguments are the placeholders in the function definition and parameters are the values passed to the function when it is called.

b)

Parameters and Arguments are interchangeable words that have no difference.

c)

Parameters are the placeholders in the function definition and arguments are the values passed to the function when it is called.

d)

Parameters are used with built-in functions and arguments are used with user-defined functions.

9.

How do you remove a key-value pair from a dictionary?

a)

my_dict.remove(key)

b)

del my_dict[key]

c)

remove my_dict[key]

d)

my_dict.del(key)

10.

What is the output of the code snippet?

a)

[1, 2, 3]

b)

[5, 2, 3]

c)

None

d)

Error for an undefined variable

11.

What does 'a' stand for when using it to open a file?

a)

attach

b)

additional

c)

append

d)

access

12.

How do you check if a key exists in a dictionary?

a)

key in my_dict

b)

my_dict.has_key(key)

c)

my_dict[key]

d)

my_dict.contains(key)

13.

What is the output of the code snippet?

a)

{'a': 1, 'b': 2}

b)

{'a': 1, 'b': 2, 'c': 3}

c)

{'c': 3}

d)

An error will occur

14.

What is the purpose of if __name__ equals '__main__'?

a)

To cause unnecessary difficulty

b)

To break apart our file so it can be used as a module and only run certain code when the file is run directly compared to imported

c)

To prevent syntax errors in programs

d)

To define our modules name when we want to import it later.

15.

Which of the statements is not true regarding the opening mode of a file?

a)

When you open a file for reading, if the file does not exist, an error occurs.

b)

When you open a file for writing, if the file does not exist, an error occurs.

c)

When you open a file in append mode, new data is added to the end of the file without deleting existing data.

d)

When you open a file for writing, all previous data in the file is deleted.

16.

Which of the following are true of Python dictionaries (select all that apply):

a)

Dictionary keys can be any type.

b)

Dictionaries are accessed by key.

c)

All the keys in a dictionary must be of the same type.

d)

Dictionaries are mutable.

e)

A dictionary value can contain any object type except another dictionary.

17.

What is the value of the variable out at the end of the code snippet?

a)

[4, 6, 3, 8, 7, 9, 3]

b)

An error will occur

c)

None

d)

[8, 12, 6, 16, 14, 18, 6]

18.

True or False: The list myList will be modified from the function.

a)

True

b)

False