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. tuple
B. str
C. list
D. dict

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

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

3 Which string method converts all letters to uppercase?

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

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

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

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

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

6 Which statement about Python lists is correct?

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

7 Which brackets are commonly used to create a tuple?

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

8 Which statement about tuples is correct?

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

9 How are values commonly accessed in a Python dictionary?

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

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. values()
C. items()
D. get()

12 Which statement best describes a Python set?

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

13 Which set method adds one element to a set?

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

14 What values are generated by range(4)?

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

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

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

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

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

17 What is an object in Python OOP?

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

18 What does encapsulation mainly do in OOP?

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

19 What does inheritance allow a child class to do?

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

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

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

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

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

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

Strings Medium
A. "This is a list".index("is")
B. "This is a list".count("is")
C. "This is a list".find("is")
D. len("This is a list".split("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, [4, 5]]
B. [1, 2, 3]
C. [1, 2, 3, 4, 5]
D. [4, 5, 1, 2, 3]

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

Lists Medium
A. [2, 3]
B. [3, 4]
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 for x in values if x ** 2 % 2 == 0]
C. [x * 2 for x in values if x % 2 == 0]
D. [x ** 2 if x % 2 == 0 for x in values]

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

Tuples Medium
A. The tuple becomes (10, 25, 30)
B. The tuple becomes [10, 25, 30]
C. An IndexError is raised
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, 9), b = None
C. a = 7, b = (7, 9)
D. a = 7, b = 9

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

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

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

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

Dictionaries Medium
A. {"x": [1, 5]}
B. A KeyError is raised
C. {"x": 5}
D. {"x": 1, "x": 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. dict(word, len(word) for word in words)
C. {word: len(word) for word in words}
D. {len(word): word for word in words}

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

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

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

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

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

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

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

Range Medium
A. [0, 2, 4]
B. [5, 3, 1]
C. [5, 3, 1, -1]
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. Inheritance
B. Instantiation
C. Polymorphism
D. Encapsulation

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

Encapsulation Medium
A. A single leading underscore, such as _balance
B. Capital letters, such as BALANCE
C. Two trailing underscores, such as balance__
D. A single trailing underscore, 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. True
B. A TypeError is raised
C. False
D. None

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

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

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

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

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

Strings Hard
A. (1, -1, "abX")
B. (2, 0, "Xba")
C. (1, 2, "Xba")
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], [1]] [[0, 2], [3]]
B. [[0, 2], [1]] [[0, 2], [3]]
C. [[0, 2], [1]] [[0], [3]]
D. [[0, 2], [3]] [[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 becomes [9, 1, 8, 3, 4, 5].
B. An IndexError occurs after partial assignment.
C. a becomes [9, 1, 8, 3, 5].
D. A 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)?

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

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

Tuples Hard
A. (1, frozenset({2, 3}))
B. (1, {2, 3})
C. (1, {"x": 2})
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. ["a", "b", "c"]
B. ["c", "b", "a"]
C. ["b", "a", "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, "first")
B. (2, "first")
C. (1, "second")
D. (2, "second")

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

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

52 Which comparison evaluates to True?

Range Hard
A. range(0) == range(1)
B. range(0, 10, 2) == range(0, 11, 2)
C. range(1, 2, 3) == range(1, 5, 9)
D. 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?

OOP features Hard
A. (2, 1)
B. (3, 1)
C. (3, 3)
D. (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?

OOP features Hard
A. It returns the object's identity-based hash.
B. It always returns the integer value 0.
C. It raises TypeError because P is unhashable.
D. It returns the hash of the class 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?

Encapsulation Hard
A. An AttributeError is raised.
B. 1
C. 2
D. 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?

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

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

Inheritance Hard
A. "B"
B. "A"
C. A TypeError is raised.
D. 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?

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