Unit 3: Data Structures, Classes, and Inheritance - Practice Quiz
1 Which brackets are commonly used to create a Python list?
2
What does the following code produce? [x * 2 for x in range(3)]
3 Which statement about a Python tuple is correct?
4 Which data structure stores values using key-value pairs?
5 What is a key feature of a Python set?
6
What is the main purpose of the map() function?
7
What does the filter() function do?
8
What is the usual purpose of reduce()?
9 What is a Cartesian product of two sets?
10
What is the purpose of Python's itertools module?
11
What does Python's eval() function generally do?
12
What does Python's exec() function generally execute?
13
What does enumerate() add when iterating over a sequence?
14
What does the zip() function commonly do?
15 Which module provides common functions for copying objects?
16 What is a class in Python?
17 Which special method is commonly used as a constructor in a Python class?
__create__()
__start__()
__newobject__()
__init__()
18 What is an attribute in an object-oriented Python program?
19 What is method overriding?
20 Which keyword is used in a generator function to produce values one at a time?
21
What is the value of nums after this code runs?
nums = [1, 2, 3]
nums[1:2] = [7, 8]
[7, 8, 1, 2, 3]
[1, 7, 8, 3]
[1, 2, 7, 8, 3]
[1, 7, 3]
22
What does the following list comprehension produce?
result = [x * x for x in range(6) if x % 2 == 1]
[1, 4, 9, 16, 25]
[1, 9, 25]
[0, 1, 4, 9, 16, 25]
[0, 4, 16]
23
Which statement correctly describes the result of this code?
t = ([1, 2], 3)
t[0].append(4)
TypeError
([1, 2, 4], 3)
24
What is printed by this code?
data = {"a": 1, "b": 2}
value = data.pop("a", 0)
print(value, data)
1 {'a': 1, 'b': 2}
1 {'b': 2}
0 {'a': 1}
2 {'b': 2}
25
What is the value of result after this code?
items = {1, 2, 2, 3}
items.add(4)
result = items.intersection({2, 3, 5})
{1, 4}
{1, 2, 3, 4, 5}
{2, 3}
{2, 3, 4}
26
What does this expression evaluate to when converted to a list?
list(map(len, ["cat", "python", "AI"]))
[3, 6, 2]
[3, 5, 2]
[2, 5, 1]
["cat", "python", "AI"]
27
What is the result of the following expression?
list(filter(lambda n: n > 3, [1, 4, 3, 6, 2]))
[1, 3, 2]
[4, 6]
[3, 4, 6]
[1, 4, 3, 6, 2]
28
What value is produced by this code?
from functools import reduce
result = reduce(lambda a, b: a * b, [2, 3, 4])
24
10
12
9
29
How many pairs are generated by the Cartesian product of these two lists?
from itertools import product
pairs = list(product([1, 2, 3], ["a", "b"]))
30
What does the following expression produce?
from itertools import chain
list(chain([1, 2], [3], [4, 5]))
[5, 4, 3, 2, 1]
[[1, 2], [3], [4, 5]]
[1, 2, 3, 4, 5]
[1, 2, [3], 4, 5]
31
What is printed by this code?
expression = "3 * (2 + 4)"
print(eval(expression))
18
14
"3 * (2 + 4)"
SyntaxError is raised
32
What is the value of total after this code executes?
namespace = {}
exec("total = 4 + 5", namespace)
total = namespace["total"]
"4 + 5"
None
33
What is printed by this code?
colors = ["red", "blue"]
for index, color in enumerate(colors, start=1):
print(index, color)
1 blue followed by 2 red
0 blue followed by 1 red
1 red followed by 2 blue
0 red followed by 1 blue
34
What is the result of this expression?
list(zip([1, 2, 3], ["a", "b"]))
[(1, 'a'), (2, 'b'), (3, '')]
[(1, 'a'), (2, 'b'), (3, None)]
[(1, 2, 3), ('a', 'b')]
[(1, 'a'), (2, 'b')]
35
What is printed by this code?
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
shallow[0].append(9)
print(original)
[[1, 2, 9], [3, 4]]
[[9], [3, 4]]
[[1, 2], [3, 4]]
TypeError is raised
36
What is the output of this code?
class Counter:
def __init__(self, start):
self.value = start
c = Counter(5)
print(c.value)
5
None
AttributeError is raised
0
37
What is printed by this code?
class Account:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
account = Account(100)
account.deposit(25)
print(account.balance)
100
25
75
125
38
What is printed by this code?
class Student:
school = "Central"
s1 = Student()
s2 = Student()
s1.school = "North"
print(s1.school, s2.school)
AttributeError is raised
North Central
North North
Central Central
39
What is printed by this code?
class Animal:
def sound(self):
return "generic"
class Dog(Animal):
def sound(self):
return "bark"
print(Dog().sound())
bark
Dog
TypeError is raised
generic
40
What is printed by this code?
class A:
def identify(self):
return "A"
class B(A):
def identify(self):
return "B"
class C(A):
def identify(self):
return "C"
class D(B, C):
pass
print(D().identify())
B
A
C
41
What is printed by the following code? a = [1, [2, 3]]; b = a[:]; a[1].append(4); b.append(5); print(a, b)
[1, [2, 3, 4]] [1, [2, 3]]
[1, [2, 3, 4]] [1, [2, 3, 4], 5]
[1, [2, 3, 4], 5] [1, [2, 3, 4], 5]
[1, [2, 3], 5] [1, [2, 3, 4]]
42
What is the value of result after execution? data = [1, 2, 3]; result = [data.pop(0) for _ in range(len(data))]
IndexError is raised
[1, 2]
[3, 2, 1]
[1, 2, 3]
43
Which statement correctly explains the result of hash((1, [2, 3]))?
TypeError because the tuple contains an unhashable list
44
What is the value of merged? left = {"a": 1, "b": 2}; right = {"b": 9, "c": 3}; merged = left | right
KeyError is raised for the duplicate key
{"a": 1, "b": 2, "b": 9, "c": 3}
{"a": 1, "b": 2, "c": 3}
{"a": 1, "b": 9, "c": 3}
45
What is the most appropriate behavior of this code? s = {1, 2, 3}; for x in s: s.remove(x)
RuntimeError is raised because the set size changes during iteration
46
What does the following expression evaluate to? from functools import reduce; reduce(lambda a, b: a * b, filter(lambda x: x % 2, map(lambda x: x + 1, [1, 2, 3, 4])), 1)
120
45
24
15
47
What is the value of list(itertools.product([1, 2], repeat=2))?
[(1, 1), (2, 2)]
[(1, 1), (1, 2), (2, 1), (2, 2)]
[(1, 2), (2, 1)]
[(1, 1, 2, 2)]
48
What is printed by this code? from itertools import groupby; values = [1, 1, 2, 2, 1]; print([(k, list(g)) for k, g in groupby(values)])
[(1, [1]), (1, [1]), (2, [2]), (2, [2]), (1, [1])]
[(1, [1, 1, 1]), (2, [2, 2])]
[(1, [1, 1]), (2, [2, 2]), (1, [1])]
[(1, [1, 1]), (1, [2, 2]), (1, [1])]
49
What happens when this expression is evaluated? eval("__import__('os').getcwd()", {"__builtins__": {}}, {})
SyntaxError because imports are forbidden in eval
NameError because __import__ is unavailable
None because built-ins are disabled
50
What happens when f() is called? g = {}; l = {}; exec("x = 10\ndef f():\n return x", g, l); f = l["f"]; print(f())
10 because x is in the local execution namespace
NameError because the function searches g for global x
KeyError because x is not in g
None because exec does not retain function definitions
51
What is the value of list(enumerate("ab", start=3))?
[(1, 'a'), (2, 'b')]
[(3, 'b'), (4, 'a')]
[(0, 'a'), (1, 'b')]
[(3, 'a'), (4, 'b')]
52
What is the value of list(zip([1, 2, 3], "xy"))?
[(1, 2, 3), ('x', 'y')]
[(1, 'x'), (2, 'y')]
[(1, 'x'), (2, 'y'), (3, None)]
ValueError is raised because the lengths differ
53
What is printed? import copy; a = [[1], [2]]; b = copy.copy(a); c = copy.deepcopy(a); a[0].append(9); print(b, c)
[[1], [2]] [[1], [2]]
[[1, 9], [2]] [[1], [2]]
[[1, 9], [2]] [[1, 9], [2]]
[[1], [2]] [[1, 9], [2]]
54
What is printed? class A: value = 1\na = A(); b = A(); a.value = 2; print(a.value, b.value, A.value)
2 2 2
1 2 1
2 1 1
1 1 1
55
What is the result of this code? class Base:\n def __init__(self):\n self.x = 1\nclass Child(Base):\n def __init__(self):\n self.y = 2\nc = Child(); print(hasattr(c, "x"), c.y)
AttributeError is raised while constructing c
True 2
False 2
True 1
56
What is printed? class A:\n def show(self): return "A"\nclass B(A):\n def show(self): return super().show() + "B"\nprint(B().show())
BA
AB
B
A
57
Given class Animal: pass and class Dog(Animal): pass, which statement is correct?
isinstance(Animal(), Dog) is true
isinstance(Dog, Animal) is true
issubclass(Dog, Animal) is true
issubclass(Animal, Dog) is true
58
What is printed? class A:\n def f(self): return "A"\nclass B(A):\n def f(self): return "B"\nclass C(A):\n def f(self): return "C"\nclass D(B, C):\n pass\nprint(D().f())
A
C
B
59 Which design best preserves encapsulation for a bank account whose balance must never be directly set to a negative value?
_balance and validate changes through methods
balance publicly and trust callers
60
What is printed? def deco(f):\n def wrapper(): return f() * 2\n return wrapper\n@deco\ndef value(): return 3\nprint(value())
6
3
TypeError is raised because decorators cannot alter return values
5
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →