Python Final
Which statement about a list is NOT correct? A list consists of two parts: a key and a value. A list is an ordered collection of items. A list is a numerically indexed array. A list is mutable.
A list consists of two parts: a key and a value
Given the following code, what output will be printed? browsers = ['Chrome', 'Firefox', 'Internet Explorer', 'Safari'] item = 'Internet Explorer' browsers.remove(item) print(browsers)
['Chrome', 'Firefox', 'Safari']
Given the following code, what output will be printed? grades = {'Joe':75,'John':95,'Matt':83} grades['Mark'] = 99 print(grades)
['Joe':75, 'John':95,'Matt':83, 'Mark':99}
Given the following code, what output will be printed? names = ['Mary', 'Joe', 'Matt'] names[1] = 'Tom' print(names)
['Mary', 'Tom', 'Matt']
Given the following code, what output will be printed? characters = ['a', 'b', 'c'] del characters[1] print(characters)
['a', 'c']
Given the following code, what output will be printed? integers = [1, 2, 3] * 3 print(integers)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Given the code, what output will be printed? numbers = [1, 2, 3] print(numbers)
[1, 2, 3]
Given the following code, what output will be printed? numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(numbers[4:6])
[5, 6]
Given the following code, what output will be printed? integers = [5, 7, 9] integers.reverse() print(integers)
[9, 7, 5]
Given the following code, what output will be printed? grades = {'Joe':75,'John':95,'Matt':83} print(grades['John'])
[95]
Given the following code, what output will be printed? students = [['Joe', 'Kim'], ['Sam', 'Sue'], ['Kelly', 'Chris']] print(students[1][1])
[Sue]
String testing method that returns true if the string contains only numeric digits and is at least one character in length.
isdigit()
Method used to read a single line from a file
readline()
Method that returns a copy of the string with all instances of old replaced by new.
replace()
A computer concept called DRY stands for ...
Don't repeat yourself
Given the following code, what output will be printed? grades = {'Joe':75,'John':95,'Matt':83} grades['Matt'] = 89 print(grades)
{'Joe': 75, 'john:95,'Matt':89}
Given the following code, what output will be printed? grades = {'Joe':75,'John':95,'Matt':83,'Mark':99} del grades['Mark'] print(grades)
{'Joe':75, 'John':95, 'Matt':83}
(T/F) A dictionary is an object that stores a collection of data. Each element in a dictionary has two parts: a key and a value. You use the value to locate the key.
FALSE
(T/F) If you define a function but never invoke or call it, the indented code for the function will execute (run) anyway.
FALSE
(T/F) User written Python modules CANNOT be imported into another Python program.
FALSE
Given the following code, what output will be printed? integers = [22, 33, 44] print(min(integers)) print(max(integers))
22 44
22 33 44
Given the following code, what output will be printed? integers = [22, 33, 44] for i in integers: print(i)
Given the following code, what output will be printed? def printHello(first, last = 'Jackson'): print('Hello {} {}!'.format(first, last)) printHello('Joe') printHello(first = 'Joe', last = 'Smith')
Hello Joe Jackson! Hello Joe Smith
An advantage of reusing code?
Improves consistancy
Method that accepts a value as a parameter and returns the index of the first value in the list, or an exception if the value is not in the list
Index
Given the following code, what output will be printed? colors = ['orange', 'gray', 'black'] try: print(colors.index('purple')) except: print('Color not found')
"Color not found"
Given the following code, what output will be printed? outputfile = open('griffin.txt','w') outputfile.write('Peter\n') outputfile.write('Lois\n') outputfile.write('Meg\n') outputfile.write('Chris\n') outputfile.write('Brian\n') outputfile.write('Stewie\n') outputfile.close() inputfile = open('griffin.txt', 'r') filecontent = inputfile.read() print(filecontent) inputfile.close()
Peter Lois Meg Chris Brian Stewie *in a notepad
Given the following code, what output will be printed? colors = ['red', 'blue', 'yellow', 'purple'] print(colors[3])
Purple
(T/F) A global variable is accessible to all the functions in a program file.
TRUE
(T/F) Lists can be concatenated using the + operator.
TRUE
(T/F) There are two types of functions in Python: those built into the Python language and functions defined by yourself (user-defined functions).
TRUE
(T/F) When a Python built-in module is imported, you gain access to all methods available in the module.
TRUE
(T/F) Your program must open a file before it can read from or write to a file.
TRUE
Given the following code, what output will be printed? def main(): texas() california() def texas(): birds = 150 print('Texas has', birds, 'birds') def california(): birds = 360 print('California has', birds, 'birds') main()
Texas has 150 birds California has 360 birds
Given the following code, what output will be printed? def main(): texas() california() def texas(): birds = 150 print('Texas has', birds, 'birds') def california(): birds = 360 print('California has', birds, 'birds') main()
Texas has 150 birds California has 360 birds
When reading a file using a while loop, what does the readline() method return at end of file?
an empty string
Method that allows you to add items, one at a time, to the end of a list
append()
Create an empty list
candy = []
Method that deletes all entries in a dictionary
clear()
Method used to add multiple items to the end of a list
extend()
Command needed to delete a text file in a Python program
import os
Method that allows you to add an item to a list in a specific location
insert
When working with classes, a class is like a "blueprint" that objects are created from. When we create that object in memory, we are actually creating a(an) ___________ of that class.
instance
Method that returns all of the dictionary's keys and their associated values
items()
String modification method that returns a copy of the string with all trailing whitespace characters removed. Trailing whitespace characters are spaces, newline(\n), and tabs(\t) that appear at the end of a string.
rstrip()
Method that will reorder the current list in ascending sequence, replacing the current list
sort()
Function that will sort a list in ascending sequence creating a new list with the new list name passed in parentheses
sorted()
Method that returns a list containing words in a string.
split()
Tuples are great for holding data that ...
will not or should not change during the execution of a program