Exam 3
w
When opening a file, what is the correct file mode for writing?
a
Which is the file "mode" for append?
readline
Which is used to read a single line from a file?
myExample = {'someItem': 2, 'otherItem': 20}
Which of the following is a valid dictionary in Python?
sampleTuple = (1,2,3,4,5)
Which of the following is a valid tuple in Python?
file_ob.readline(4)
Which of the following reads the 4th line of file? Assuming:file_ob = open("hi.txt","r")
Python's syntax is much like PHP
Which of the following statements is NOT true about Python? Python's syntax is much like PHP Python can be used for web development Python can run on any type of platform Python can be used to generate dynamic web pages
try: except NameError: except: finally:
Whats the correct order? except: finally: except NameError: try:
True
When Python runs the code in the try block and no errors occur, python will execute the finally block.
Tuple
What data structure should I store a series of numbers that I do not want to make changes to?
Dictionary
What data structure should I use to store inventory? (name and amount)
Least Recently Used
What does LRU stand for?
FileNotFoundError
What error occurs when you try to open a file that does not exist?
11
What is the binary number: 1011 in decimal?
C
What is the binary number: 1100 in Hexadecimal?
Objects are created from classes.
What is the difference between a class and an object in Python?
except
What is the keyword used after the try statement to handle exceptions?
f = open("test.txt", "r")
What is the proper way to open a file that you intend to read from?
f = open("test.txt","w")
What is the proper way to open a file that you plan to write to?
elif
What keyword would you use to add an alternative condition to an if statement?
memoization
What technique was covered in the video to speed up the recursion?
Answer 1: delete pop remove clear Answer 2: deleteitem popitem removeitem clearitem
What two methods will allow you to remove something from a dictionary? ____ and _____
123.44
What would the statement "print('%.2f' % 123.444)" print out?
while a < 10:
Whats a valid way to start a while loop in Python?
Recursive function
A function definition that contains a function call to itself is known as:
a function that calls itself
A recursive function is:
Falso
A set in python will allow you to have multiple copies of something within the set.
file_ob.close()
Assuming:file_ob = open(data.txt","r"), which of the following lines of code close the file?
print("Hello {0}, you are {1} years old.".format(userInput, age))
Given the following code, rewrite the print statement using format to get the output to match exactly as the old print statement does. def main(): userInput = input("Enter your name: ") yearBorn = int(input("Enter year born: ")) age = 2018-yearBorn print("Hello " + userInput + ", you are " + str(age) + " years old.") main()
print("{0:.5}".format(value))
Given the following lines of code, write the print using the .format method so that the number only displays only 5 digits of the number. def main(): value = 42.24422442 #print statement goes here main()
print("{0:.4f}".format(value))
Given the following lines of code, write the print using the .format method so that the number only displays only the first 4 decimal places, def main(): value = 42.24422442 #print statement goes here main()
print("Hello {0}".format(userInput))
Given the following lines of code, write the print using the .format method. def main(): userInput = input("Enter your name: ") print("Hello " + userInput) main()
print(f"Hello {userInput}")
Given the following lines of code, write the print using the f-string method. It should display exactly the same thing, but using the new f-string method learned in the video. (NOTE: older versions of python do not support this format) def main(): userInput = input("Enter your name: ") print("Hello " + userInput) main()
example.replace('a','b')
How can you replace all of the letter a in a string variable "example" with the letter b?
str(a)
How would you cast the int variable "a" that is equal to 2 into the string "2"?
int(a)
How would you cast the string variable "a" that is equal to "2" into the integer 2?
print(example[1])
How would you print the second item in the list variable "example"?
for a in range(1,3):
How would you write a valid for loop in Python?
The file's contents will be erased
If you had a statement like, "f =open("test.txt","w")", what would happen to the file as soon as that statement is executed?
type(example)
If you have a variable "example", how do you check to see what type of variable you are working with?
set get acquire pop push
The _____ method (for dictionaries) lets you try and get the value for a specific key.
False
The file modes w and a are exactly the same.
True
The finally statement is the last statement in a try... except block.
read the entire contents of the file
The read method does what?
try: #body: a bunch of code that could cause an error except <ExceptionType>: #handler: what we want to happen
Whats the correct order for a try...except block?
except <ExceptionType> except else finally
Whats the correct order of these in a try block? finally except <ExceptionType> else except
Text and binary
The two types of files:
True
There are many built-in exceptions in python.
True
Tuples are immutable