Font size
WorksheetsCSE 102 Exam 3 Practice
Total questions: 18
Worksheet time: 10mins
What is the correct way to open a file for reading in Python? (Assume there is a variable filename that contains the filename)
open(filename, 'r')
open(filename, 'w')
with open(filename, 'r') as openFile:
with open(filename, 'r')
What is the correct syntax to define a user-defined function in Python?
function myFunction():
def myFunction():
create myFunction():
define myFunction():
For the following function, what are the parameters? (select all that apply)
val1
val2
val3
val4
val5
Which of the following is a valid way to create a dictionary in Python?
my_dict = {1, 2}
my_dict = [1: 'a', 2: 'b']
my_dict = {'a': 1, 'b': 2}
my_dict = {'a' =1, 'b' = 2}
Which function can be used to skip (and if desired gather) a row in a csv file.
next()
skip()
pass()
read()
How do you read all lines from a text file into a list in Python?
file.readlines()
file.read()
file.readlist()
file.read_all()
What is the correct way to update a value in a dictionary?
What is the difference between parameters and arguments?
Arguments are the placeholders in the function definition and parameters are the values passed to the function when it is called.
Parameters and Arguments are interchangeable words that have no difference.
Parameters are the placeholders in the function definition and arguments are the values passed to the function when it is called.
Parameters are used with built-in functions and arguments are used with user-defined functions.
How do you remove a key-value pair from a dictionary?
my_dict.remove(key)
del my_dict[key]
remove my_dict[key]
my_dict.del(key)
What is the output of the code snippet?
[1, 2, 3]
[5, 2, 3]
None
Error for an undefined variable
What does 'a' stand for when using it to open a file?
attach
additional
append
access
How do you check if a key exists in a dictionary?
key in my_dict
my_dict.has_key(key)
my_dict[key]
my_dict.contains(key)
What is the output of the code snippet?
{'a': 1, 'b': 2}
{'a': 1, 'b': 2, 'c': 3}
{'c': 3}
An error will occur
What is the purpose of if __name__ equals '__main__'?
To cause unnecessary difficulty
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
To prevent syntax errors in programs
To define our modules name when we want to import it later.
Which of the statements is not true regarding the opening mode of a file?
When you open a file for reading, if the file does not exist, an error occurs.
When you open a file for writing, if the file does not exist, an error occurs.
When you open a file in append mode, new data is added to the end of the file without deleting existing data.
When you open a file for writing, all previous data in the file is deleted.
Which of the following are true of Python dictionaries (select all that apply):
Dictionary keys can be any type.
Dictionaries are accessed by key.
All the keys in a dictionary must be of the same type.
Dictionaries are mutable.
A dictionary value can contain any object type except another dictionary.
What is the value of the variable out at the end of the code snippet?
[4, 6, 3, 8, 7, 9, 3]
An error will occur
None
[8, 12, 6, 16, 14, 18, 6]
True or False: The list myList will be modified from the function.
True
False
