Unit 2: Data Types and OOP Concepts - Practice Quiz

CAP776 — Programming In Python 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which Python data type is used to store text?

Strings Easy
A. dict
B. list
C. str
D. tuple

2 What is the result of "Python"[0]?

Strings Easy
A. "n"
B. "Python"
C. "y"
D. "P"

3 Which string method converts all letters to uppercase?

Strings Easy
A. lower()
B. split()
C. strip()
D. upper()

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

Lists Easy
A. Angle brackets <>
B. Curly brackets {}
C. Round brackets ()
D. Square brackets []

5 Which list method adds one item to the end of a list?

Lists Easy
A. pop()
B. append()
C. remove()
D. sort()

6 Which statement about Python lists is correct?

Lists Easy
A. Lists store only text
B. Lists are immutable
C. Lists reject duplicates
D. Lists are mutable

7 Which brackets are commonly used to create a tuple?

Tuples Easy
A. Square brackets []
B. Curly brackets {}
C. Round brackets ()
D. Angle brackets <>

8 Which statement about tuples is correct?

Tuples Easy
A. Tuples are unordered
B. Tuples store only numbers
C. Tuples are immutable
D. Tuples reject duplicates

9 How are values commonly accessed in a Python dictionary?

Dictionaries Easy
A. By using keys
B. By using positions
C. By using methods
D. By using classes

10 Which example creates a dictionary containing one key-value pair?

Dictionaries Easy
A. ("name", "Ana")
B. {"name": "Ana"}
C. ["name", "Ana"]
D. {"name", "Ana"}

11 Which dictionary method returns all keys?

Dictionaries Easy
A. keys()
B. items()
C. values()
D. get()

12 Which statement best describes a Python set?

Sets Easy
A. It stores immutable elements only
B. It stores unique elements
C. It stores indexed elements
D. It stores key-value pairs

13 Which set method adds one element to a set?

Sets Easy
A. add()
B. append()
C. insert()
D. extend()

14 What values are generated by range(4)?

Range Easy
A. 1, 2, 3, 4
B. 0, 1, 2, 3
C. 1, 2, 3
D. 0, 1, 2, 3, 4

15 Which function call generates the values 2, 3, 4?

Range Easy
A. range(1, 5)
B. range(2, 4)
C. range(2, 5)
D. range(3, 6)

16 In object-oriented programming, what is a class?

OOP features Easy
A. A module for imports
B. A loop for objects
C. A variable for numbers
D. A blueprint for objects

17 What is an object in Python OOP?

OOP features Easy
A. An instance of a class
B. A name of a module
C. A branch of a condition
D. A type of loop

18 What does encapsulation mainly do in OOP?

Encapsulation Easy
A. Converts data types
B. Bundles data and methods
C. Repeats a code block
D. Imports external modules

19 What does inheritance allow a child class to do?

Inheritance Easy
A. Delete a parent class
B. Reuse a parent class
C. Import a parent module
D. Convert a parent object

20 In class Dog(Animal):, which class is the parent class?

Inheritance Easy
A. object
B. class
C. Animal
D. Dog

21 What is the output of s = "Python"; print(s[1:5:2])?

Strings Medium
A. yth
B. yh
C. Pto
D. y h

22 Which expression returns the number of times the substring "is" occurs in "This is a list"?

Strings Medium
A. len("This is a list".split("is"))
B. "This is a list".index("is")
C. "This is a list".count("is")
D. "This is a list".find("is")

23 What is the value of " PyThOn ".strip().lower()?

Strings Medium
A. " python "
B. "python"
C. "PYTHON"
D. "PyThOn"

24 What is the final value of numbers = [1, 2, 3]; numbers += [4, 5]?

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

25 What is printed by a = [1, 2, 3, 4]; print(a[-3:-1])?

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

26 Which statement creates a new list containing the squares of all even numbers in values?

Lists Medium
A. [x * 2 for x in values if x % 2 == 0]
B. [x ** 2 if x % 2 == 0 for x in values]
C. [x ** 2 for x in values if x % 2 == 0]
D. [x for x in values if x ** 2 % 2 == 0]

27 What is the result of t = (10, 20, 30); t[1] = 25?

Tuples Medium
A. An IndexError is raised
B. The tuple becomes (10, 25, 30)
C. The tuple becomes [10, 25, 30]
D. A TypeError is raised

28 What are the values of a and b after a, b = (7, 9)?

Tuples Medium
A. a = 9, b = 7
B. a = 7, b = 9
C. a = 7, b = (7, 9)
D. a = (7, 9), b = None

29 What is printed by data = (1, [2, 3]); data[1].append(4); print(data)?

Tuples Medium
A. A TypeError is raised
B. (1, [2, 3, 4])
C. (1, [2, 3])
D. (1, 2, 3, 4)

30 What is the output of d = {"a": 1, "b": 2}; print(d.get("c", 0))?

Dictionaries Medium
A. 0
B. A KeyError is raised
C. "c"
D. None

31 What is the final value of d after d = {"x": 1}; d["x"] = 5?

Dictionaries Medium
A. {"x": 5}
B. A KeyError is raised
C. {"x": 1, "x": 5}
D. {"x": [1, 5]}

32 Which expression creates a dictionary mapping each word in words to its length?

Dictionaries Medium
A. {word: len(word) for word in words}
B. [word: len(word) for word in words]
C. {len(word): word for word in words}
D. dict(word, len(word) for word in words)

33 What is the result of {1, 2, 3} & {2, 3, 4}?

Sets Medium
A. {2, 3}
B. {1, 2, 3, 4}
C. set()
D. {1, 4}

34 What is the result of set([1, 2, 2, 3, 1])?

Sets Medium
A. {1, 2, 3}
B. (1, 2, 3)
C. [1, 2, 3]
D. {1, 2, 2, 3, 1}

35 What list is produced by list(range(2, 10, 3))?

Range Medium
A. [2, 5, 8, 10]
B. [2, 4, 6, 8]
C. [3, 6, 9]
D. [2, 5, 8]

36 What is the result of list(range(5, 0, -2))?

Range Medium
A. [5, 3, 1, -1]
B. [5, 3, 1]
C. [0, 2, 4]
D. [5, 4, 3, 2, 1]

37 Which OOP feature allows the same method call to behave differently for objects of different classes?

OOP features Medium
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Instantiation

38 In Python, which naming convention indicates that an attribute is intended for internal use within a class?

Encapsulation Medium
A. A single trailing underscore, such as balance_
B. A single leading underscore, such as _balance
C. Two trailing underscores, such as balance__
D. Capital letters, such as BALANCE

39 What is printed by the following code? class A: pass; class B(A): pass; print(issubclass(B, A))

Inheritance Medium
A. A TypeError is raised
B. False
C. True
D. None

40 What does super().__init__() typically do inside a subclass constructor?

Inheritance Medium
A. Deletes the parent class object
B. Creates a new subclass automatically
C. Calls the parent class constructor
D. Overrides every parent method

41 What is the value of s[8:1:-3] when s = "0123456789"?

Strings Hard
A. "852"
B. "853"
C. "825"
D. "8752"

42 What does the expression (s.count("aba"), s.find("aba", 1), s.replace("aba", "X")) return for s = "ababa"?

Strings Hard
A. (1, 2, "Xba")
B. (2, 0, "Xba")
C. (1, -1, "abX")
D. (2, 2, "XX")

43 What is printed by a = [[0], [1]]; b = a[:]; a[0].append(2); b[1] = [3]; print(a, b)?

Lists Hard
A. [[0, 2], [1]] [[0], [3]]
B. [[0], [1]] [[0, 2], [3]]
C. [[0, 2], [3]] [[0, 2], [3]]
D. [[0, 2], [1]] [[0, 2], [3]]

44 Given a = [0, 1, 2, 3, 4, 5], what happens when a[::2] = [9, 8] is executed?

Lists Hard
A. A ValueError occurs and a remains unchanged.
B. An IndexError occurs after partial assignment.
C. a becomes [9, 1, 8, 3, 5].
D. a becomes [9, 1, 8, 3, 4, 5].

45 What is printed after executing t = ([1],); followed by try: t[0] += [2] and except TypeError: pass, then print(t)?

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

46 Which tuple can be used as a dictionary key without raising TypeError?

Tuples Hard
A. (1, frozenset({2, 3}))
B. (1, {"x": 2})
C. (1, {2, 3})
D. (1, [2, 3])

47 What is the value of list(d.items()) after d = {True: "a", 1: "b", 1.0: "c"}?

Dictionaries Hard
A. [(1.0, "c")]
B. [(1, "c")]
C. [(True, "c")]
D. [(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?

Dictionaries Hard
A. ["c", "b", "a"]
B. ["b", "a", "c"]
C. ["a", "b", "c"]
D. ["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?

Sets Hard
A. {1, 2, 5}
B. {1, 2, 3}
C. {2, 3}
D. {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"}?

Sets Hard
A. (1, "second")
B. (2, "second")
C. (2, "first")
D. (1, "first")

51 For r = range(10, 0, -2), what is (3 in r, 4 in r, r[-1], list(r[::-1]))?

Range Hard
A. (False, False, 2, [2, 4, 6, 8, 10])
B. (False, True, 0, [2, 4, 6, 8, 10])
C. (True, True, 2, [10, 8, 6, 4, 2])
D. (False, True, 2, [2, 4, 6, 8, 10])

52 Which comparison evaluates to True?

Range Hard
A. range(0) == range(1)
B. range(5, 0, -1) == range(5, -1, -1)
C. range(0, 10, 2) == range(0, 11, 2)
D. range(1, 2, 3) == range(1, 5, 9)

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?

OOP features Hard
A. (2, 1)
B. (1, 1)
C. (3, 1)
D. (3, 3)

54 What happens when hash(P()) is evaluated for class P: with only def __eq__(self, other): return isinstance(other, P) explicitly defined?

OOP features Hard
A. It always returns the integer value 0.
B. It raises TypeError because P is unhashable.
C. It returns the hash of the class P.
D. It returns the object's identity-based hash.

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?

Encapsulation Hard
A. An AttributeError is raised.
B. 3
C. 1
D. 2

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?

Encapsulation Hard
A. 8
B. 2
C. 4
D. 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?

Inheritance Hard
A. "DBAC"
B. "DBA"
C. "DBCA"
D. "DCBA"

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?

Inheritance Hard
A. The MRO becomes C, A, X, B, Y, object.
B. The class is created using depth-first lookup order.
C. A TypeError is raised due to an inconsistent MRO.
D. The MRO becomes C, A, B, X, 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?

Inheritance Hard
A. "A"
B. None
C. "B"
D. A TypeError is raised.

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?

Inheritance Hard
A. "C"
B. "B"
C. "A"
D. An ambiguous-inheritance error is raised.