practice Quiz Qs Flashcards

1
Q

What gets printed after the following code snippet?

def some_func(some_list):
some_list[1] = [‘peach’]
list_a = [‘apple’, ‘orange’, ‘banana’]
some_func(list_a)
list_a[0] = ‘pear
print(list_a)

A

[‘pear’, [‘peach’], ‘banana’]

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

What gets printed after this following code snippet?

import copy
list_a = [‘apple’, [‘orange’, ‘banana’]]
list_b = copy.copy(list_a)
list_b[0] = ‘pear’
list_b[1][1] = ‘peach’
print(list_a)

A

[‘apple’, [‘orange’, ‘peach’]]

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

What gets printed after this following code snippet?

import copy
list_a = [‘apple’, [‘orange’, ‘banana’]]
list_b = copy.deepcopy(list_a)
list_b[0] = ‘pear’
list_b[1][1] = ‘peach’
print(list_a)

A

[‘apple’, [‘orange’, ‘banana’]]

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

What is the output of the following code snippet?

def my_func(n):
if n > 5:
return n + my_func(n-2)
else:
return -1
print(my_func(10))

A

23

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

What is the output of the following code snippet?

def get_diff(city_tuple):
return len(city_tuple[1] ) - len(city_tuple[0])

names = [(‘Madison’, ‘Chicago’), (‘Chicago’, ‘Los Angeles’), (‘Los Angeles’, ‘Seattle’)

names.sort(key = get_diff)
print(names[0])

A

(‘Los Angeles’, ‘Seattle’)

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

What is the type of data and split_data in the following?

f = open(‘madison.txt’)
data = f.read()
split_data = data_split(‘\n’)
f.close()

A

data is string and split_data is list

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

What does roster[1].name evaluate to?

Roster is a list with a nested list

import copy
new_roster1 = copy.copy(roster)
new_roster2 = roster
new_roster1[1] = Student(“Rose”, “B”, 17)
new_roster2[1] = Student(“Emily”, “A”, 42)

A

Emily

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

What will be in happy.txt after this code runs?

f = open(“happy.txt”, “w”)
f.write(“Don’t worry,”)
f.close()
f = open(“happy.text”, “w”)
f.write(“Be “)
f.write(“Happy”)
f.close()

A

“Be Happy”

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

What call to foo will not casue an AssertionError?

def foo(word, num):
assert type(word) == str and type(num) == int
assert len(word) > num
return (word + “ “) * num

A

foo(“penguin”, 5)

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

Using assets is most useful for which category of errors?

syntax
runtime
semantic
exceptional

A

semantic

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

What will be printed?

def f(x, y):
if len(x) == 0:
return -1
elif x[0] = y
return x
else:
return f(x[1:], y)
print(f(‘abcd’, ‘c’)

A

‘cd’

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

What will the following code print?

a = [1]
b = [2]
c = [2]
d = c
d.append(3)
print(b, c)

A

[2] [2,3]

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

wines = [[“Cabernet Franc”, “orange”, “vanilla”] , [“Babernet Sauvigon”, “vanilla” , “cherry”], [“Malbec”, “Orange” , none] , [“MALBEC”, “cherry”, None]]

import copy
wines_v2 = copy.copy(wines)
wines_v2.append([“Malbec”, “vanilla”, None])
wines_v2 [0] [2] = “test”
print( wines_v2 [0][2], len(wines))

A

test 5

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

Movies = [{“title” : “A”, “year”: 18}, {“title” : “B”, “year”: 18}, {“title” : “C”, “year”: 19}, {“title”: “D”, “year” : 19}]

buckets = {}
bucket = []
for movie in Movies:
key = movie[“year”]
if not key in buckets:
buckets[key] = bucket
buckets[key].append(m)
print(len(buckets[18]), len(buckets[19]))

A

4 4

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

movies = [{‘title’: ‘A’, ‘year’: 18}, {‘title’:’B’, ‘year’: 18}, {‘title’: ‘C’, ‘year’: 19}, {‘title’:’D’, ‘year’:19}]

movies.append(movies.pop(0))
print(movies[1].get(‘title’, None))

A

‘C’

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

What will the following print?

import copy
x = [{“movie”: “Wonder Woman”, “stars” : 4.5}, {“movie” : “Captain Marvel”, “stars”: 5}, {“movie” : “Ghost Busters”, “stars”: 4}]
y = copy.deepcopy(x)
x[0][“movie”] = “Akira”
print(y[0][“movie”]

A

Wonder Woman