Exam Questions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the output of the Python code below?

my_list = [3, 2, 1]
print(my_list.sort())

A

None.
Remember, the sort function returns a value of None when using it with the print statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Consider the following Python program.

fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)

What is line?

A

A string that may have a newline.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)

What does the program loop over?

A

Lines in a file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

True or False:
Given a Python dictionary d and a value v, it is efficient to find the corresponding key: d[k] = v.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What output will the following code produce?

print (“%s %d %f” % (5, 5, 5))

A

5 5 5.000000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following types are allowed for Python dictionary keys?

dictionary
list
list of dictionaries
tuple
All of the above

A

A tuple. The key has to be immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the output of the Python method invocation below?

“bib”.find(‘b’, 1, 2)

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is the following code correct?
8 += 2

A

No. You can only do this with variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

fin = open(‘words.txt’)
for line in fin:
word = line.strip()
print(word)

What is fin?

A

The variable fin is used to refer to the file object returned by ‘open()’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
A

Yes. ‘Got it’ will display and the program will stop because of the break.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The meaning of a program is called?

A

Semantics

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A programming language like Python that is designed to be easy for
humans to read and write.

A

High-level language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A programming language that is designed to be easy for a computer
to run; also called “machine language” or “assembly language”.

A

Low-level language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly