Unit 2: Data Types and OOP Concepts - Practice Quiz
1 Which Python data type is used to store text?
tuple
str
list
dict
2
What is the result of "Python"[0]?
"y"
"P"
"n"
"Python"
3 Which string method converts all letters to uppercase?
split()
lower()
upper()
strip()
4 Which brackets are commonly used to create a Python list?
()
{}
<>
[]
5 Which list method adds one item to the end of a list?
append()
pop()
sort()
remove()
6 Which statement about Python lists is correct?
7 Which brackets are commonly used to create a tuple?
<>
[]
()
{}
8 Which statement about tuples is correct?
9 How are values commonly accessed in a Python dictionary?
10 Which example creates a dictionary containing one key-value pair?
{"name": "Ana"}
{"name", "Ana"}
["name", "Ana"]
("name", "Ana")
11 Which dictionary method returns all keys?
keys()
values()
items()
get()
12 Which statement best describes a Python set?
13 Which set method adds one element to a set?
extend()
add()
append()
insert()
14
What values are generated by range(4)?
0, 1, 2, 3
0, 1, 2, 3, 4
1, 2, 3
1, 2, 3, 4
15
Which function call generates the values 2, 3, 4?
range(2, 4)
range(1, 5)
range(3, 6)
range(2, 5)
16 In object-oriented programming, what is a class?
17 What is an object in Python OOP?
18 What does encapsulation mainly do in OOP?
19 What does inheritance allow a child class to do?
20
In class Dog(Animal):, which class is the parent class?
Dog
object
Animal
class
21
What is the output of s = "Python"; print(s[1:5:2])?
y h
yth
Pto
yh
22
Which expression returns the number of times the substring "is" occurs in "This is a list"?
"This is a list".index("is")
"This is a list".count("is")
"This is a list".find("is")
len("This is a list".split("is"))
23
What is the value of " PyThOn ".strip().lower()?
"python"
"PYTHON"
" python "
"PyThOn"
24
What is the final value of numbers = [1, 2, 3]; numbers += [4, 5]?
[1, 2, 3, [4, 5]]
[1, 2, 3]
[1, 2, 3, 4, 5]
[4, 5, 1, 2, 3]
25
What is printed by a = [1, 2, 3, 4]; print(a[-3:-1])?
[2, 3]
[3, 4]
[2, 3, 4]
[1, 2]
26
Which statement creates a new list containing the squares of all even numbers in values?
[x ** 2 for x in values if x % 2 == 0]
[x for x in values if x ** 2 % 2 == 0]
[x * 2 for x in values if x % 2 == 0]
[x ** 2 if x % 2 == 0 for x in values]
27
What is the result of t = (10, 20, 30); t[1] = 25?
(10, 25, 30)
[10, 25, 30]
IndexError is raised
TypeError is raised
28
What are the values of a and b after a, b = (7, 9)?
a = 9, b = 7
a = (7, 9), b = None
a = 7, b = (7, 9)
a = 7, b = 9
29
What is printed by data = (1, [2, 3]); data[1].append(4); print(data)?
TypeError is raised
(1, 2, 3, 4)
(1, [2, 3, 4])
(1, [2, 3])
30
What is the output of d = {"a": 1, "b": 2}; print(d.get("c", 0))?
0
None
"c"
KeyError is raised
31
What is the final value of d after d = {"x": 1}; d["x"] = 5?
{"x": [1, 5]}
KeyError is raised
{"x": 5}
{"x": 1, "x": 5}
32
Which expression creates a dictionary mapping each word in words to its length?
[word: len(word) for word in words]
dict(word, len(word) for word in words)
{word: len(word) for word in words}
{len(word): word for word in words}
33
What is the result of {1, 2, 3} & {2, 3, 4}?
{2, 3}
{1, 4}
set()
{1, 2, 3, 4}
34
What is the result of set([1, 2, 2, 3, 1])?
{1, 2, 2, 3, 1}
[1, 2, 3]
(1, 2, 3)
{1, 2, 3}
35
What list is produced by list(range(2, 10, 3))?
[2, 5, 8]
[2, 4, 6, 8]
[3, 6, 9]
[2, 5, 8, 10]
36
What is the result of list(range(5, 0, -2))?
[0, 2, 4]
[5, 3, 1]
[5, 3, 1, -1]
[5, 4, 3, 2, 1]
37 Which OOP feature allows the same method call to behave differently for objects of different classes?
38 In Python, which naming convention indicates that an attribute is intended for internal use within a class?
_balance
BALANCE
balance__
balance_
39
What is printed by the following code? class A: pass; class B(A): pass; print(issubclass(B, A))
True
TypeError is raised
False
None
40
What does super().__init__() typically do inside a subclass constructor?
41
What is the value of s[8:1:-3] when s = "0123456789"?
"8752"
"853"
"852"
"825"
42
What does the expression (s.count("aba"), s.find("aba", 1), s.replace("aba", "X")) return for s = "ababa"?
(1, -1, "abX")
(2, 0, "Xba")
(1, 2, "Xba")
(2, 2, "XX")
43
What is printed by a = [[0], [1]]; b = a[:]; a[0].append(2); b[1] = [3]; print(a, b)?
[[0], [1]] [[0, 2], [3]]
[[0, 2], [1]] [[0, 2], [3]]
[[0, 2], [1]] [[0], [3]]
[[0, 2], [3]] [[0, 2], [3]]
44
Given a = [0, 1, 2, 3, 4, 5], what happens when a[::2] = [9, 8] is executed?
a becomes [9, 1, 8, 3, 4, 5].
IndexError occurs after partial assignment.
a becomes [9, 1, 8, 3, 5].
ValueError occurs and a remains unchanged.
45
What is printed after executing t = ([1],); followed by try: t[0] += [2] and except TypeError: pass, then print(t)?
([2],)
([1, 2],)
([1], [2])
([1],)
46
Which tuple can be used as a dictionary key without raising TypeError?
(1, frozenset({2, 3}))
(1, {2, 3})
(1, {"x": 2})
(1, [2, 3])
47
What is the value of list(d.items()) after d = {True: "a", 1: "b", 1.0: "c"}?
[(1.0, "c")]
[(1, "c")]
[(True, "c")]
[(True, "a"), (1, "b"), (1.0, "c")]
48
What is the result of list(keys) after d = {"a": 1, "b": 2, "c": 3}; keys = d.keys(); d["b"] = 20; d.pop("a"); d["a"] = 10?
["a", "b", "c"]
["c", "b", "a"]
["b", "a", "c"]
["b", "c", "a"]
49
Given A = {1, 2}, B = {2, 3, 4}, C = {3, 4, 5}, and D = {4}, what is A | B & C - D?
{1, 2, 5}
{1, 2, 3}
{2, 3}
{1, 2, 3, 4}
50
What is the result of (len(d), d[frozenset({1, 2})]) after d = {frozenset({1, 2}): "first", frozenset({2, 1}): "second"}?
(1, "first")
(2, "first")
(1, "second")
(2, "second")
51
For r = range(10, 0, -2), what is (3 in r, 4 in r, r[-1], list(r[::-1]))?
(False, True, 2, [2, 4, 6, 8, 10])
(False, True, 0, [2, 4, 6, 8, 10])
(False, False, 2, [2, 4, 6, 8, 10])
(True, True, 2, [10, 8, 6, 4, 2])
52
Which comparison evaluates to True?
range(0) == range(1)
range(0, 10, 2) == range(0, 11, 2)
range(1, 2, 3) == range(1, 5, 9)
range(5, 0, -1) == range(5, -1, -1)
53
What is the result of (g(), A.f()) after defining class A: x = 1 with @classmethod def f(cls): return cls.x, then class B(A): x = 2, followed by g = B.f; B.x = 3?
(2, 1)
(3, 1)
(3, 3)
(1, 1)
54
What happens when hash(P()) is evaluated for class P: with only def __eq__(self, other): return isinstance(other, P) explicitly defined?
0.
TypeError because P is unhashable.
P.
55
What is returned by b.get() after class A: defines __x = 1 and def get(self): return self.__x, while class B(A): __x = 2, followed by b = B(); b._B__x = 3?
AttributeError is raised.
1
2
3
56
A class initializes self._x = 1, exposes property x whose getter returns _x, and whose setter stores self._x = value * 2. What is c.x after c = C(); c.x += 3?
4
8
2
6
57
Given A.f() returns "A", B(A).f() returns "B" + super().f(), C(A).f() returns "C" + super().f(), and D(B, C).f() returns "D" + super().f(), what does D().f() return?
"DBCA"
"DBAC"
"DCBA"
"DBA"
58
Suppose A(X, Y) and B(Y, X) are valid classes, where X and Y are unrelated. What happens when Python attempts to define class C(A, B): pass?
C, A, B, X, Y, object.
TypeError is raised due to an inconsistent MRO.
C, A, X, B, Y, object.
59
Given class A: with def f(self): return "A" and def g(self): return self.f(), and class B(A): overriding f to return "B", what does A.g(B()) return?
"B"
"A"
TypeError is raised.
None
60
Given class A: x = "A", class B(A): pass, class C(A): x = "C", and class D(B, C): pass, what is the value of D.x?
"B"
"A"
"C"
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 →