Python Final Exam
Which of the following can be involved in an expression? *Variables *Conditional Operators *Other expressions *Statements *Mathematical operators *Values
Variables Conditional operators Other expressions Mathematical operators Values
A for loop will process a file sentence-by-sentence
False
A while loop will always execute at least once
False
A while loop will execute once for each expression in the conditional
False
Dictionaries are useful because you can have duplicate keys
False
Dictionaries will always have at least one key and one value
False
The given program correctly closes the file: def has_a_cat(filename): a_file = open(filename) return "cat" in a_file.read() a_file.close() has_a_cat("story.txt")
False
The open function consumes a String representing a path and returns a String of the file's contents
False
The print function is necessary to call a function
False
The statement count = count + 1 will cause an error because no number can be greater than itself
False
Tuples are composed of key/value pairs
False
Variable names are important because computers understand the meaning of names and change their value accordingly
False
Variables in Python are used to solve for unknown values, like in Algebra.
False
You can nest if statements inside other if statements, but not inside functions
False
You can store a List as one of the keys of a Dictionary
False
You should not put error messages in to help seeking emails because it can clutter up the email
False
Which of the following types are sequences? *Float *Boolean *File *Tuple *List *Dictionary *String
File Tuple List Dictionary String
Which of the following is the best description of this line of code? from math import sqrt as square_root *Loads the sqrt function in the math module but renames it square_root *Loads the square_root function from the math module but renames it sqrt *Loads the math function from the sqrt module but renames it sqrt *Loads the sqrt and square_root functions from the math module
Loads the sqrt function in the math module but renames it square_root
The input function does which of the following? *Writes output to the console *Retrieves input from the user *Consumes arguments *Returns a string
Writes output to the console Retrieves input from the user consumes arguments Returns a string
Unlike Lists, Tuples are immutable, meaning Tuples cannot be changed after they are created
True
Variables change their value over time according to the instructions in a program
True
You can store a Dictionary as one of the elements of a List
True
You can store a List as one of the values of a Dictionary
True
You must import the json module before you can use the load function
True
The print function does which of the following? *Retrieves input from the user *Writes output to the console *Returns a string *Consumes arguments
Writes output to the console Consumes arguments
How many values can be used at a time with each of the following operators? *+ *not *and *!=
2 1 2 2
Mark each of the following email titles if they are GOOD titles when seeking help from a senior colleague. *"How do I update a variable?" *"Why do I get an error?" *"Work Confusion" *"What is a TypeError and how do I get rid of it?" *The example code literally makes ZERO sense! *"HELP ME!!!!!!!" *"i got an eror hlp plz"
"How do I update a variable?" "What is a TypeError and how do I get rid of it?"
Given the following code: movies = [ {"Name": "Iron Man", "Length": 126, "Characters": ["Tony", "Pepper", "Obidiah"]}, {"Name": "Thor", "Length": 130, "Characters": ["Thor", "Jane", "Loki"]}, {"Name": "Captain America", "Length": 126, "Characters": ["Steve", "Peggy", "Bucky"]}] What is the value of the following expressions? *movies[0]["Name"] *movies[2]["Name"] *movies[0][127] *movies[1][1] *movies[127]["Name"] *movies[2]["Characters"][0] *movies[2]["Characters"]["Steve"] *movies[1]["Characters"][2] *movies[2]["Steve"] *movies[-1]["Length"]
"Iron Man" "Captain American" Raises an error Raises an error Raises an error "Steve" Raises an error Loki Raises an error 126
How many branches do each of these code snippets have? if conditional: print(4) print(4) if conditional: print(1) else: print(3) if conditional1: if conditional2: print(4) print(3) print(2)
2 2 3
What will be printed after this code is run? points = {"Virginia Tech": 5} points["Virginia Tech"] = 3 prints(points["Virginia Tech"]) *[5, 3] *3 *[3, 5] *5
3
What does the pass do? *Passes a value to a function *Absolutely nothing *Passes the result of the function back to where it was called
Absolutely nothing
How many times does the loop body below execute? command = "" words = [] while command != "quit": command = input("Give me another word:") words.append("Said "+command) print(words) *No more than four times *At least twice *At least once *None
At least once
What is the type of the iteration variable in this code? groceries = [{"Name": "Apple", "Cost": 1.99, "Calories": 20}, {"Name": "Cookie", "Cost": 4.99, "Calories": 300}, {"Name": "Steak", "Cost": 6.99, "Calories": 100}] for grocery in groceries: print(grocery) *Dictionary *Float *Integer *List *String
Dictionary
Every function create with the def keyword must have AT LEAST ONE parameter
False
Expression are always evaluated from left to right
False
Humans discovered programming languages in the 1940s and have been decoding them ever since
False
Once a function returns a variable, that variable is available outside o the function
False
Printing is the same as returning
False
Strings are composed of only letters and symbols
False
The body of a for loop will contain one statement for each element of the iteration list
False
Is the print call before, inside, or after the for loop? for name in names: print(name) *inside *after *before
Inside
Which of the following are possible subtypes for a list? *Integer *Boolean *String *Dictionary *List
Integer Boolean String Dictionary List
Given the file named scores.txt with the following contents: 44, 23, 37 And the following python code: score_file = open("scores.txt") score_data = score_file.read() What will be the type of score_data? *String *Dictionary of integers and strings *List of string *List of integer *File object *Integer
List of string
Label each of the following as a function call, method call, or not a call. *function.call() *print() *file.(close) *method() *"function".method() *(input)
Method call Function call not a call Function call Method call not a call
What is the value of tax after this program executes? if False: tax = 5 print(tax) *5, because of the assignment on line 2 *No value, because an error occurs since the value has not been defined *None, because that is the default value for all variables *0, because that is the default value for integer variables
No value, because an error occurs since the value has not been defined
When the following code is executed, what will be the value of the variable named greeting? def make_greeting(name): print("Hello", name) greeting = make_greeting("Mr. Anderson") *"HelloMr.Anderson" *None *"Mr. Anderson" *"Hello Mr. Anderson
None
The print function can only print literal values.
True
Match the type of graph with the kind of question it is most suited to. Scatter Plot Histogram Line Plot
The relationship between two lists of numbers The distribution of a list of numbers The trend in a list of numbers
Keys can be added to a dictionary after the dictionary has been created
True
Like an if statement and a function call, the for Loop might cause the execution to not follow the sequential order of lines
True
List comprehensions cannot express everything that a for loop can
True
Normally, statements are executed from top to bottom
True
The Iteration Variable will take on each value of the List Variable, one at a time
True
Label each of the following as either "Read", a "Write", or a "Read and Write" for the variable. *price = 0 *print(price) *adjusted = price * tax *price = price * .9
Write Read Read Read and Write
Which of the following demonstrates proper syntax for accessing a dictionary's value? *["Key"]a_dictionary *{"Key"}a_dictionary *a_dictionary{"Key"} *a_dictionary["Key"]
a_dictionary["Key"]
Given the following code, dict = {"Key": "Value", 7: 8, True: "Hello"} key = 7 *dict[7] *dict[8] *dict["Value"] *dict["Key"] *dict[key] *dict[True] *dict["He" + "llo"] *dict[Key] *dict[False]
dict[7] dict["Key"] dict[key] dict[True]
Which of the following two snippets of code are equal? from json import load file = open("scores.txt") json.load(file) from json import load file = open("scores.txt") load(file) import load file = open("scores.txt") load(file) import json as js file = open("scores.txt") json.load(file) import json file = open("scores.txt") json.load(file)
from json import load file = open("scores.txt") load(file) import json file = open("scores.txt") json.load(file)
Which of the following expressions creates two new variables? *month = 11 * month = year *"month", "year" = 11, 17 *date = 11, 17 *month, year = (11, 17)
month, year = (11, 17)
Given the program below, name = input("What is your name?") def fix_capitalization(name): return name.title().strip() def print_message(name): print("Hello", name, "how are you?") Which of the following lines of code will have the data flow correctly through both functions in order to print the message with the fixed capitalization? fix_capitalization(name) print_message(name) print_message(fix_capitalization(name)) name = fix_capitalization(name) print_message(name) fix_capitalization() print_message() None are necessary, because the variables share the same name. print(fix_capitalization(name)) print(print_message(name))
print_message(fix_capitalization(name)) name = fix_capitalization(name) print_message(name)
Choose which of the following are valid Python variables names. Note that they do not have to be good variable names, just valid. *book-title *5th_value *stor_txt *_27_ *first name *BankAccount *"dogs_list"
stor_txt _27_ BankAcconut
Given this dictionary... animal = {"Name": "Pumpkin", "Age": 2, "Small?": True} What is the type pf value associated with the key "Name"? What is the type of the key "Name"? What is the type of the key "Small?"? What is the type of the value associated with the key "Age"?
string string string integer