Exam Questions Flashcards
What is the output of the Python code below?
my_list = [3, 2, 1]
print(my_list.sort())
None.
Remember, the sort function returns a value of None when using it with the print statement.
Consider the following Python program.
fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)
What is line?
A string that may have a newline.
fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)
What does the program loop over?
Lines in a file.
True or False:
Given a Python dictionary d and a value v, it is efficient to find the corresponding key: d[k] = v.
False
What output will the following code produce?
print (“%s %d %f” % (5, 5, 5))
5 5 5.000000
Which of the following types are allowed for Python dictionary keys?
dictionary
list
list of dictionaries
tuple
All of the above
A tuple. The key has to be immutable.
What is the output of the Python method invocation below?
“bib”.find(‘b’, 1, 2)
-1
variable.find searches for the index of ‘b’, and we are telling it to start in index 1, and stop at index 2 which means it stops at 1. The -1 indicates the item was not found in the specified range.
Is the following code correct?
8 += 2
No. You can only do this with variables.
fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)
What is fin?
The variable fin is used to refer to the file object returned by ‘open()’.
For the Python program below, will there be any output, and will the program terminate?
while True:
while 1 > 0: break print("Got it!") break
Yes. ‘Got it’ will display and the program will stop because of the break.
The meaning of a program is called?
Semantics
A programming language like Python that is designed to be easy for
humans to read and write.
High-level language
A programming language that is designed to be easy for a computer
to run; also called “machine language” or “assembly language”.
Low-level language