Unit 3: Data Structures, Classes, and Inheritance - Practice Quiz

CSR101 — Python Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which brackets are commonly used to create a Python list?

Lists Easy
A. < >
B. { }
C. ( )
D. [ ]

2 What does the following code produce? [x * 2 for x in range(3)]

List comprehension Easy
A. [0, 2, 4]
B. [2, 4, 6]
C. [1, 2, 3]
D. [0, 1, 2]

3 Which statement about a Python tuple is correct?

Tuples Easy
A. It requires square brackets
B. It cannot contain strings
C. It stores only numbers
D. It is immutable

4 Which data structure stores values using key-value pairs?

Dictionaries Easy
A. Tuple
B. Set
C. List
D. Dictionary

5 What is a key feature of a Python set?

Sets: creation, manipulation, and mutability Easy
A. It is always immutable
B. It uses numeric indexes
C. It stores duplicate values
D. It stores unique values

6 What is the main purpose of the map() function?

Map Easy
A. To remove duplicate items
B. To apply a function to items
C. To sort a sequence
D. To combine two dictionaries

7 What does the filter() function do?

Filter Easy
A. Combines items into pairs
B. Selects items meeting a condition
C. Reverses every item
D. Creates a class automatically

8 What is the usual purpose of reduce()?

Reduce Easy
A. To combine items into one result
B. To remove all strings
C. To generate dictionary keys
D. To create several classes

9 What is a Cartesian product of two sets?

Cartesian product Easy
A. Their elements in reverse order
B. Their common elements
C. All ordered pairs from both sets
D. Only the largest elements

10 What is the purpose of Python's itertools module?

Itertools Easy
A. Connecting to databases
B. Changing file permissions
C. Drawing graphical windows
D. Working with iterators

11 What does Python's eval() function generally do?

eval Easy
A. Prints every list item
B. Executes a string as an expression
C. Defines a new module
D. Deletes a string variable

12 What does Python's exec() function generally execute?

exec Easy
A. Only integer values
B. A string containing Python statements
C. Only imported packages
D. A list of dictionary keys

13 What does enumerate() add when iterating over a sequence?

enumerate Easy
A. Keys and methods
B. Indexes and values
C. Classes and objects
D. Files and folders

14 What does the zip() function commonly do?

zip Easy
A. Copies a complete class
B. Sorts all numbers
C. Compresses a Python file
D. Pairs items from iterables

15 Which module provides common functions for copying objects?

copy Easy
A. time
B. copy
C. random
D. math

16 What is a class in Python?

Introduction to classes Easy
A. A blueprint for objects
B. A type of loop
C. A command-line argument
D. A built-in file format

17 Which special method is commonly used as a constructor in a Python class?

Constructors Easy
A. __create__()
B. __start__()
C. __newobject__()
D. __init__()

18 What is an attribute in an object-oriented Python program?

Attributes Easy
A. A repeated conditional statement
B. A special type of comment
C. Data associated with an object
D. A package installation command

19 What is method overriding?

Method overriding Easy
A. Adding a variable outside Python
B. Redefining an inherited method
C. Calling a method without parentheses
D. Removing every method in a class

20 Which keyword is used in a generator function to produce values one at a time?

Use of decorators and generators Easy
A. return
B. produce
C. yield
D. generate

21 What is the value of nums after this code runs?

PYTHON
nums = [1, 2, 3]
nums[1:2] = [7, 8]

Lists Medium
A. [7, 8, 1, 2, 3]
B. [1, 7, 8, 3]
C. [1, 2, 7, 8, 3]
D. [1, 7, 3]

22 What does the following list comprehension produce?

PYTHON
result = [x * x for x in range(6) if x % 2 == 1]

List comprehension Medium
A. [1, 4, 9, 16, 25]
B. [1, 9, 25]
C. [0, 1, 4, 9, 16, 25]
D. [0, 4, 16]

23 Which statement correctly describes the result of this code?

PYTHON
t = ([1, 2], 3)
t[0].append(4)

Tuples Medium
A. The tuple changes into a list automatically
B. The code raises a TypeError
C. The list changes but the tuple structure remains fixed
D. The tuple becomes ([1, 2, 4], 3)

24 What is printed by this code?

PYTHON
data = {"a": 1, "b": 2}
value = data.pop("a", 0)
print(value, data)

Dictionaries Medium
A. 1 {'a': 1, 'b': 2}
B. 1 {'b': 2}
C. 0 {'a': 1}
D. 2 {'b': 2}

25 What is the value of result after this code?

PYTHON
items = {1, 2, 2, 3}
items.add(4)
result = items.intersection({2, 3, 5})

Sets: creation, manipulation, and mutability Medium
A. {1, 4}
B. {1, 2, 3, 4, 5}
C. {2, 3}
D. {2, 3, 4}

26 What does this expression evaluate to when converted to a list?

PYTHON
list(map(len, ["cat", "python", "AI"]))

Map Medium
A. [3, 6, 2]
B. [3, 5, 2]
C. [2, 5, 1]
D. ["cat", "python", "AI"]

27 What is the result of the following expression?

PYTHON
list(filter(lambda n: n > 3, [1, 4, 3, 6, 2]))

Filter Medium
A. [1, 3, 2]
B. [4, 6]
C. [3, 4, 6]
D. [1, 4, 3, 6, 2]

28 What value is produced by this code?

PYTHON
from functools import reduce
result = reduce(lambda a, b: a * b, [2, 3, 4])

Reduce Medium
A. 24
B. 10
C. 12
D. 9

29 How many pairs are generated by the Cartesian product of these two lists?

PYTHON
from itertools import product
pairs = list(product([1, 2, 3], ["a", "b"]))

Cartesian product Medium
A. 6
B. 5
C. 9
D. 3

30 What does the following expression produce?

PYTHON
from itertools import chain
list(chain([1, 2], [3], [4, 5]))

Itertools Medium
A. [5, 4, 3, 2, 1]
B. [[1, 2], [3], [4, 5]]
C. [1, 2, 3, 4, 5]
D. [1, 2, [3], 4, 5]

31 What is printed by this code?

PYTHON
expression = "3 * (2 + 4)"
print(eval(expression))

eval Medium
A. 18
B. 14
C. "3 * (2 + 4)"
D. A SyntaxError is raised

32 What is the value of total after this code executes?

PYTHON
namespace = {}
exec("total = 4 + 5", namespace)
total = namespace["total"]

exec Medium
A. An undefined variable error occurs
B. 9
C. "4 + 5"
D. The value is None

33 What is printed by this code?

PYTHON
colors = ["red", "blue"]
for index, color in enumerate(colors, start=1):
    print(index, color)

enumerate Medium
A. 1 blue followed by 2 red
B. 0 blue followed by 1 red
C. 1 red followed by 2 blue
D. 0 red followed by 1 blue

34 What is the result of this expression?

PYTHON
list(zip([1, 2, 3], ["a", "b"]))

zip Medium
A. [(1, 'a'), (2, 'b'), (3, '')]
B. [(1, 'a'), (2, 'b'), (3, None)]
C. [(1, 2, 3), ('a', 'b')]
D. [(1, 'a'), (2, 'b')]

35 What is printed by this code?

PYTHON
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
shallow[0].append(9)
print(original)

copy Medium
A. [[1, 2, 9], [3, 4]]
B. [[9], [3, 4]]
C. [[1, 2], [3, 4]]
D. A TypeError is raised

36 What is the output of this code?

PYTHON
class Counter:
    def __init__(self, start):
        self.value = start

c = Counter(5)
print(c.value)

Constructors Medium
A. 5
B. None
C. An AttributeError is raised
D. 0

37 What is printed by this code?

PYTHON
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)

Methods Medium
A. 100
B. 25
C. 75
D. 125

38 What is printed by this code?

PYTHON
class Student:
    school = "Central"

s1 = Student()
s2 = Student()
s1.school = "North"
print(s1.school, s2.school)

Attributes Medium
A. An AttributeError is raised
B. North Central
C. North North
D. Central Central

39 What is printed by this code?

PYTHON
class Animal:
    def sound(self):
        return "generic"

class Dog(Animal):
    def sound(self):
        return "bark"

print(Dog().sound())

Method overriding Medium
A. bark
B. Dog
C. A TypeError is raised
D. generic

40 What is printed by this code?

PYTHON
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())

Multiple inheritance Medium
A. B
B. A
C. C
D. A method resolution error occurs

41 What is printed by the following code? a = [1, [2, 3]]; b = a[:]; a[1].append(4); b.append(5); print(a, b)

Lists Hard
A. [1, [2, 3, 4]] [1, [2, 3]]
B. [1, [2, 3, 4]] [1, [2, 3, 4], 5]
C. [1, [2, 3, 4], 5] [1, [2, 3, 4], 5]
D. [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))]

List comprehension Hard
A. An IndexError is raised
B. [1, 2]
C. [3, 2, 1]
D. [1, 2, 3]

43 Which statement correctly explains the result of hash((1, [2, 3]))?

Tuples Hard
A. It hashes the list by converting it to a tuple automatically
B. It returns different hashes each time because lists are mutable
C. It raises TypeError because the tuple contains an unhashable list
D. It returns a stable hash because tuples are immutable

44 What is the value of merged? left = {"a": 1, "b": 2}; right = {"b": 9, "c": 3}; merged = left | right

Dictionaries Hard
A. A KeyError is raised for the duplicate key
B. {"a": 1, "b": 2, "b": 9, "c": 3}
C. {"a": 1, "b": 2, "c": 3}
D. {"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)

Sets: creation, manipulation, and mutability Hard
A. Only the first element is removed, then iteration stops normally
B. A RuntimeError is raised because the set size changes during iteration
C. A new set is created automatically for safe iteration
D. All elements are removed successfully

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)

Map, Filter, Reduce Hard
A. 120
B. 45
C. 24
D. 15

47 What is the value of list(itertools.product([1, 2], repeat=2))?

Cartesian product Hard
A. [(1, 1), (2, 2)]
B. [(1, 1), (1, 2), (2, 1), (2, 2)]
C. [(1, 2), (2, 1)]
D. [(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)])

Itertools Hard
A. [(1, [1]), (1, [1]), (2, [2]), (2, [2]), (1, [1])]
B. [(1, [1, 1, 1]), (2, [2, 2])]
C. [(1, [1, 1]), (2, [2, 2]), (1, [1])]
D. [(1, [1, 1]), (1, [2, 2]), (1, [1])]

49 What happens when this expression is evaluated? eval("__import__('os').getcwd()", {"__builtins__": {}}, {})

eval Hard
A. It returns the current working directory
B. It raises SyntaxError because imports are forbidden in eval
C. It raises NameError because __import__ is unavailable
D. It silently returns 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())

exec Hard
A. It prints 10 because x is in the local execution namespace
B. It raises NameError because the function searches g for global x
C. It raises KeyError because x is not in g
D. It prints None because exec does not retain function definitions

51 What is the value of list(enumerate("ab", start=3))?

enumerate Hard
A. [(1, 'a'), (2, 'b')]
B. [(3, 'b'), (4, 'a')]
C. [(0, 'a'), (1, 'b')]
D. [(3, 'a'), (4, 'b')]

52 What is the value of list(zip([1, 2, 3], "xy"))?

zip Hard
A. [(1, 2, 3), ('x', 'y')]
B. [(1, 'x'), (2, 'y')]
C. [(1, 'x'), (2, 'y'), (3, None)]
D. A 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)

copy Hard
A. [[1], [2]] [[1], [2]]
B. [[1, 9], [2]] [[1], [2]]
C. [[1, 9], [2]] [[1, 9], [2]]
D. [[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)

Attributes Hard
A. 2 2 2
B. 1 2 1
C. 2 1 1
D. 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)

Constructors Hard
A. An AttributeError is raised while constructing c
B. True 2
C. False 2
D. 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())

Method overriding Hard
A. BA
B. AB
C. B
D. A

57 Given class Animal: pass and class Dog(Animal): pass, which statement is correct?

Inheritance concepts Hard
A. isinstance(Animal(), Dog) is true
B. isinstance(Dog, Animal) is true
C. issubclass(Dog, Animal) is true
D. 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())

Multiple inheritance Hard
A. A method-resolution conflict is raised
B. A
C. C
D. B

59 Which design best preserves encapsulation for a bank account whose balance must never be directly set to a negative value?

Practical OOP examples Hard
A. Store _balance and validate changes through methods
B. Store the balance in a global variable for central control
C. Expose balance publicly and trust callers
D. Use a class variable shared by every account

60 What is printed? def deco(f):\n def wrapper(): return f() * 2\n return wrapper\n@deco\ndef value(): return 3\nprint(value())

Use of decorators and generators Hard
A. 6
B. 3
C. A TypeError is raised because decorators cannot alter return values
D. 5