Python Final
Which of the following can be involved in an expression? Variables Mathematical operators Other expressions Values Conditional operators Statements
Variables Mathematical operators Other expressions Values Conditional operators
The function double below returns the number multiplied by 2. What value will the variable "x" hold after the line is executed? x = double(4) + double(2)
12
List what each item is in order. {5: "Key", "Value": 9}
{ Curly Brace 5 Key : Colon "Key" Value , Comma "Value" Key : Colon 9 Value } Curly Brace
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 body of a for loop will contain one statement for each element of the iteration list.
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 can only print literal values.
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 of 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 into help seeking emails because it can clutter up the email.
False
A sequence of data stored in your computer's long term memory
File
Which of the following types are sequences? File Dictionary Boolean String Integer List Tuple Float
File Dictionary String List Tuple
-1.4
Float
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 4/5
Float
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 grade - grade
Float
A control structure for performing actions on each element of a sequence
For Loop
A reusable chunk of code with inputs and outputs
Function
Label each of the following as a function call, method call, or not a call. print()
Function Call
Label each of the following as a function call, method call, or not a call. method()
Function call
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
The input function does which of the following? Writes output to the console Consumes arguments Retrieves input from the user Returns a string
Writes output to the console Consumes arguments Retrieves input from the user Returns a string
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
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
What is the value of the variable x after this code is run? values = [-1, -2, 2, 3] x = False for value in values: x = x or value > 0 print(x)
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
(1,2,3)
Tuple
A categorization of values of similar kinds that determines what you can do with the values.
Type
Classify each of the following as Python keywords, types, values, or built-in functions. Boolean
Type
Classify each of the following as Python keywords, types, values, or built-in functions. Dictionary
Type
Classify each of the following as Python keywords, types, values, or built-in functions. Float
Type
Classify each of the following as Python keywords, types, values, or built-in functions. Integer
Type
Classify each of the following as Python keywords, types, values, or built-in functions. List
Type
Classify each of the following as Python keywords, types, values, or built-in functions. String
Type
Classify each of the following as Python keywords, types, values, or built-in functions. ""
Value
Classify each of the following as Python keywords, types, values, or built-in functions. 5
Value
Classify each of the following as Python keywords, types, values, or built-in functions. True
Value
Classify each of the following as Python keywords, types, values, or built-in functions. []
Value
Classify each of the following as Python keywords, types, values, or built-in functions. {1:2}
Value
A name that can be associated with a value
Variable
The print function does which of the following? Writes output to the console Consumes arguments Returns a string Retrieves input from the user
Writes output to the console Consumes arguments
{}
Dictionary
[1,2,3,]
list
True
Boolean
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[2]["Name"]
"Captain America"
The function sayHi below returns the string "Hi". What value will the variable x hold after the line is executed? x = sayHi() + sayHi() + sayHi()
"HiHiHi"
Mark each of the following email titles if they are GOOD titles when seeking help from a senior colleague. "HELP ME!!!!!!!!!" The example code literally makes ZERO sense! "Why do I get an error?" "How do I update a variable?" "i got an eror hlp plz" "What is a TypeError and how do I get rid of it?" "Work Confusion"
"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"]
"Iron Man"
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[2]["Characters"][0]
"Steve"
What is printed after the code below is executed? def calculate_sum(values): sum = 0 for value in values: sum = sum + value return sum sum = 0 calculate_sum([1, 2, 3]) print(sum)
0
How many values can be used at a time with each of the following operators? not
1
Assume that the following code is run: values = [1,2,3] word = "Python" Predict the output of each of the following operations: print(values[ 0 ]) print(values[ -1 ]) print(values[ 2 ]) And then also for these: print(word[ :2 ]) print(word[ -2: ]) print(word[ 3:4 ])
1 3 3 Py on h
Fill in the blanks of the following code in order to print out each movie title followed by the names of its characters: 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"]}] for movie in movies: print(movie[ __1__ ]) for character in movie[ __2__ ]: print(character)
1. "Name" 2."Characters"
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[-1]["Length"]
126
How many branches do each of these code snippets have? if conditional: print(4) print(4)
2
How many branches do each of these code snippets have? if conditional: print(1) else: print(3)
2
How many values can be used at a time with each of the following operators? !=
2
How many values can be used at a time with each of the following operators? +
2
How many values can be used at a time with each of the following operators? and
2
How many branches do each of these code snippets have? if conditional1: if conditional 2: print(4) print(3) print(2)
3
How many elements are in the following list? movies = [{"Name": "Iron Man", "Released": 2008}, {"Name": "Thor", "Released": 2011}, {"Name": "Captain America", "Released": 2011}]
3
What will be printed after this code is run? points = {"Virginia Tech": 5} points["Virginia Tech"] = 3 print(points["Virginia Tech"])
3
After the second iteration of the loop, what is the value of sum? sum = 0 values = [4, 2, 6] for value in values: sum = sum + value
6
If an if statement is nested inside another if statement, how many spaces will the body of the inner if statement have?
8
How do you write each of the following as string literal values? A tab character: A new line: A double quote: A single quote:
A tab character: "\t" A new line: "\n" A double quote:"\"" A single quote: '\''
What does the pass do? Passes the result of the function back to where it was called. Passes a value to a function Absolutely nothing
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)
At least once
Choose which of the following are valid Python variable names. Note that they do not have to be good variable names, just valid. BankAccount stor_txt "dogs_list" book-title _27_ 5th_value first name
BankAccount stor_txt _27_
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 1 < 5.0
Boolean
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 True or False
Boolean
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 1+1 < 5 or 4 > 3
Boolen
Classify each of the following as Python keywords, types, values, or built-in functions. Print
Built-in function
Classify each of the following as Python keywords, types, values, or built-in functions. input
Built-in function
A data structure for mapping keys to values
Dictionary
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 {name: grade}
Dictionary
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
{1:2}
Dictionary
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" [1, 2, 3] < 5
Error
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" "1" + "1" == "2"
False
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" "quote" in quote
False
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" 10 == "10"
False
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" False
False
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" []
False
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" not True and False
False
Every function created with the def keyword must have AT LEAST ONE parameter.
False
Expressions 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 of the function.
False
Printing is the same thing as returning.
False
Strings are composed of only letters and symbols.
False
A control structure for branching the execution of a program
If Statement
Is the print call before, inside, or after the for loop? for name in names: print(name)
Inside
-10000000257
Integer
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 1+1
Integer
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 100-99
Integer
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 [0][0]
Integer
Given this dictionary... animal = {"Name": "Pumpkin", "Age": 2, "Small?": True} What is the type of the value associated with the key "Age"?
Integer
Classify each of the following as Python keywords, types, values, or built-in functions. While
Keyword
Classify each of the following as Python keywords, types, values, or built-in functions. def
Keyword
Classify each of the following as Python keywords, types, values, or built-in functions. for
Keyword
Classify each of the following as Python keywords, types, values, or built-in functions. import
Keyword
Classify each of the following as Python keywords, types, values, or built-in functions. in
Keyword
Classify each of the following as Python keywords, types, values, or built-in functions. return
Keyword
Read the error message below, then fill in the blanks at the bottom. Traceback (most recent call last): File "my_code.py", line 49, in <module> 21 + "NameError" TypeError: unsupported operand type(s) for +: 'int' and 'str' What line of code did the error occur on? What type of error occurred?
Line 49 Type Error
A data structure for storing elements in an ordered sequence
List
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 "1,2,3,4".split(",")
List
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 [Name]
List
[]
List
Specific instances of data written directly in source code
Literal Value
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[1]["Characters"][2]
Loki
Label each of the following as a function call, method call, or not a call. "function".method()
Method Call
Label each of the following as a function call, method call, or not a call. function.call()
Method Call
A collection of functions, classes, and variables available to be imported
Module
What is the value of tax after this program executes? if False: tax = 5 print(tax)
No value, because an error occurs since the value has not been defined
None
None
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 name[0]
String
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") "Hello Mr. Anderson" "HelloMr. Anderson" None "Mr. Anderson"
None
Label each of the following as a function call, method call, or not a call. (input)
Not a call
Label each of the following as a function call, method call, or not a call. file.(close)
Not a call
A sequence of coded instructions that manipulate data inside a computer
Program
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][127]
Raises an error
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[127]["Name"]
Raises an error
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[1][1]
Raises an error
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[2]["Characters"]["Steve"]
Raises an error
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[2]["Steve"]
Raises an error
Label each of the following as either a "Read", a "Write", or a "Read and Write" for the variable price. adjusted = price * tax
Read
Label each of the following as either a "Read", a "Write", or a "Read and Write" for the variable price. print(price)
Read
Label each of the following as either a "Read", a "Write", or a "Read and Write" for the variable price. price = price * .9
Read and Write
Match the type of graph with the kind of question it is most suited to. Scatter plot Histogram Lineplot
Scatter plot: The relationship between two lists of numbers Histogram: The distribution of a list of numbers Line plot: The trend in a list of numbers
"2.0"
String
"Python"
String
"True"
String
''
String
A type that represents textual data
String
Evaluate the type of the following expressions. Assume the following code is run first: name = "Python" grade = 100.0 "1"+"1"
String
Given this dictionary... animal = {"Name": "Pumpkin", "Age": 2, "Small?": True} What is the type of the key "Name"?
String
Given this dictionary... animal = {"Name": "Pumpkin", "Age": 2, "Small?": True} What is the type of the key "Small?"?
String
Given this dictionary... animal = {"Name": "Pumpkin", "Age": 2, "Small?": True} What is the type of the value associated with the key "Name"?
String
Which of the following are possible subtypes for a list? String Boolean Dictionary List Integer
String Boolean Dictionary List Integer
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" " " in quote
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" 10==10.0
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" "" in quote
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" "Alaska" < "Delaware"
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" "False" == "False"
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" "turtle" in quote
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" 3<5
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" 5 < 4 or 3 == 3
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" 5 in [1, 3, 5]
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" True or (True and False)
True
Evaluate whether each of the following expressions are True, False, or cause an error. Apply the rules of Truthiness when they are not boolean expressions. Assume the following code is run first: quote = "I like turtles" not False
True
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
Read the following code, and then fill in the blanks below. 1| def f(x): 2| x = x + 1 3| return x 4| y = f(0) 5| print(y) What line is executed first? What line is executed second? After line 3 is executed, what will be the next line executed?
What line is executed first? 1 What line is executed second? 4 After line 3 is executed, what will be the next line executed? 5
Label each of the following as either a "Read", a "Write", or a "Read and Write" for the variable price. price = 0
Write
Which of the following demonstrates proper syntax for accessing a dictionary's value? ["Key"]a_dictionary a_dictionary{"Key"} a_dictionary["Key"] {"Key"}a_dictionary
a_dictionary["Key"]
How would you call a function named close_file? Assume it takes no arguments
close_file()
Given the following dictionary, convert = { 5: 10, 10: 5 } What is the value of the following expressions? convert[ 5 ] convert[ 5+5 ] convert[ 5 ]+5
convert[ 5 ] = 10 convert[ 5+5 ] = 5 convert[ 5 ]+5 = 15
Given the following code, dict = {"Key": "Value", 7: 8, True: "Hello"} key = 7 Which of the following are valid expressions? dict[7] dict[8] dict[False] dict[Key] dict[True] dict["Key"] dict["Value"] dict[key] dict["He"+"llo"]
dict[7] dict[True] dict["Key"] dict[key]
Which of the following have a body? List comprehension if statement. Dictionary access while loop for loop Function definition Function call
if statement. while loop for loop Function definition
Which of the following two snippets of code are equal? import json file = open("scores.txt") json.load(file) from json import load file = open("scores.txt") load(file) from json import load file = open("scores.txt") json.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)
5
integer
Which of the following expressions creates two new variables? date = 11, 17 month, year = (11, 17) month = 11 month = year "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? print_message(fix_capitalization(name)) fix_capitalization() print_message() fix_capitalization(name) print_message(name) None are necessary, because the variables share the same name. print(fix_capitalization(name)) print(print_message(name)) name = fix_capitalization(name) print_message(name)
print_message(fix_capitalization(name)) name = fix_capitalization(name) print_message(name)