practice Quiz Qs Flashcards
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)
[‘pear’, [‘peach’], ‘banana’]
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)
[‘apple’, [‘orange’, ‘peach’]]
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)
[‘apple’, [‘orange’, ‘banana’]]
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))
23
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])
(‘Los Angeles’, ‘Seattle’)
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()
data is string and split_data is list
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)
Emily
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()
“Be Happy”
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
foo(“penguin”, 5)
Using assets is most useful for which category of errors?
syntax
runtime
semantic
exceptional
semantic
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’)
‘cd’
What will the following code print?
a = [1]
b = [2]
c = [2]
d = c
d.append(3)
print(b, c)
[2] [2,3]
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))
test 5
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]))
4 4
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))
‘C’