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. [1, 2, 3]
C. [2, 4, 6]
D. [0, 1, 2]

3 Which statement about a Python tuple is correct?

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

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 stores unique values
B. It is always immutable
C. It uses numeric indexes
D. It stores duplicate values

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

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

7 What does the filter() function do?

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

8 What is the usual purpose of reduce()?

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

9 What is a Cartesian product of two sets?

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

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

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

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

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

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

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

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

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

14 What does the zip() function commonly do?

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

15 Which module provides common functions for copying objects?

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

16 What is a class in Python?

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

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

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

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

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

19 What is method overriding?

Method overriding Easy
A. Redefining an inherited method
B. Adding a variable outside Python
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. generate
C. yield
D. produce

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

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

Lists Medium
A. [1, 7, 3]
B. [7, 8, 1, 2, 3]
C. [1, 7, 8, 3]
D. [1, 2, 7, 8, 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. [0, 4, 16]
B. [1, 4, 9, 16, 25]
C. [1, 9, 25]
D. [0, 1, 4, 9, 16, 25]

23 Which statement correctly describes the result of this code?

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

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

24 What is printed by this code?

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

Dictionaries Medium
A. 0 {'a': 1}
B. 1 {'a': 1, 'b': 2}
C. 1 {'b': 2}
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. {2, 3, 4}
C. {2, 3}
D. {1, 2, 3, 4, 5}

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

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

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

27 What is the result of the following expression?

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

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

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. 12
C. 9
D. 10

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. 9
B. 6
C. 3
D. 5

30 What does the following expression produce?

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

Itertools Medium
A. [1, 2, 3, 4, 5]
B. [5, 4, 3, 2, 1]
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. 14
B. 18
C. A SyntaxError is raised
D. "3 * (2 + 4)"

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

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

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

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 red followed by 2 blue
B. 0 red followed by 1 blue
C. 1 blue followed by 2 red
D. 0 blue followed by 1 red

34 What is the result of this expression?

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

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

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. A TypeError is raised
B. [[9], [3, 4]]
C. [[1, 2, 9], [3, 4]]
D. [[1, 2], [3, 4]]

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. None
B. An AttributeError is raised
C. 0
D. 5

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. 125
B. 100
C. 25
D. 75

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. Central Central
C. North North
D. North 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. generic
B. A TypeError is raised
C. Dog
D. bark

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. A
B. C
C. A method resolution error occurs
D. B

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], 5] [1, [2, 3, 4]]
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, 4]] [1, [2, 3]]

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. [1, 2, 3]
D. [3, 2, 1]

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

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

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

Dictionaries Hard
A. {"a": 1, "b": 9, "c": 3}
B. {"a": 1, "b": 2, "b": 9, "c": 3}
C. {"a": 1, "b": 2, "c": 3}
D. A KeyError is raised for the duplicate key

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 new set is created automatically for safe iteration
C. All elements are removed successfully
D. A 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)

Map, Filter, Reduce Hard
A. 45
B. 24
C. 120
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, 2), (2, 1)]
C. [(1, 1), (1, 2), (2, 1), (2, 2)]
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])]
B. [(1, [1]), (1, [1]), (2, [2]), (2, [2]), (1, [1])]
C. [(1, [1, 1]), (1, [2, 2]), (1, [1])]
D. [(1, [1, 1]), (2, [2, 2]), (1, [1])]

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

eval Hard
A. It silently returns None because built-ins are disabled
B. It raises NameError because __import__ is unavailable
C. It returns the current working directory
D. It raises SyntaxError because imports are forbidden in eval

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 None because exec does not retain function definitions
B. It raises NameError because the function searches g for global x
C. It prints 10 because x is in the local execution namespace
D. It raises KeyError because x is not in g

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

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

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

zip Hard
A. [(1, 'x'), (2, 'y'), (3, None)]
B. A ValueError is raised because the lengths differ
C. [(1, 2, 3), ('x', 'y')]
D. [(1, 'x'), (2, 'y')]

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, 9], [2]] [[1], [2]]
B. [[1], [2]] [[1, 9], [2]]
C. [[1, 9], [2]] [[1, 9], [2]]
D. [[1], [2]] [[1], [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. 1 2 1
B. 2 2 2
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. True 1
B. True 2
C. An AttributeError is raised while constructing c
D. False 2

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. AB
B. A
C. BA
D. B

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

Inheritance concepts Hard
A. isinstance(Animal(), Dog) is true
B. issubclass(Dog, Animal) is true
C. issubclass(Animal, Dog) is true
D. isinstance(Dog, Animal) 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. Expose balance publicly and trust callers
B. Use a class variable shared by every account
C. Store the balance in a global variable for central control
D. Store _balance and validate changes through methods

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. A TypeError is raised because decorators cannot alter return values
B. 6
C. 3
D. 5