Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.

Unit 3 MCQs OOP concepts

ECAP776 60 questions. Pick an option to check it.

Read the notes Subjective questions

0 of 60 answered 0 right 0 wrong , 60 left

  1. Question 1 of 60 OOP features Easy

    What is a class in Python?

  2. Question 2 of 60 OOP features Easy

    What is an object in object-oriented programming?

  3. Question 3 of 60 OOP features Easy

    Which special method commonly initializes a new Python object?

  4. Question 4 of 60 OOP features Easy

    What does the self parameter represent in an instance method?

  5. Question 5 of 60 OOP features Easy

    What is a method in a Python class?

  6. Question 6 of 60 OOP features Easy

    Which OOP feature allows one interface to support different behaviors?

  7. Question 7 of 60 OOP features Easy

    Which OOP feature focuses on showing essential behavior while hiding implementation details?

  8. Question 8 of 60 Encapsulation Easy

    What is encapsulation in object-oriented programming?

  9. Question 9 of 60 Encapsulation Easy

    Which Python naming style indicates that an attribute is intended for internal use?

  10. Question 10 of 60 Encapsulation Easy

    What happens to an instance attribute named __balance inside a Python class?

  11. Question 11 of 60 Encapsulation Easy

    What is the usual purpose of a getter method?

  12. Question 12 of 60 Encapsulation Easy

    What is the usual purpose of a setter method?

  13. Question 13 of 60 Encapsulation Easy

    Which Python feature can provide controlled access to an attribute using method-like logic?

  14. Question 14 of 60 Encapsulation Easy

    Which built-in decorator is commonly used to define a property getter?

  15. Question 15 of 60 Inheritance Easy

    What does inheritance allow a Python class to do?

  16. Question 16 of 60 Inheritance Easy

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

  17. Question 17 of 60 Inheritance Easy

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

  18. Question 18 of 60 Inheritance Easy

    Which function is commonly used to access a parent class implementation?

  19. Question 19 of 60 Inheritance Easy

    What is method overriding?

  20. Question 20 of 60 Inheritance Easy

    What is multiple inheritance in Python?

  21. Question 21 of 60 OOP features Medium

    What is printed by the following code?

    Python
    class Wallet:
        def __init__(self, amount):
            self.amount = amount
    
        def add(self, value):
            self.amount += value
    
    w1 = Wallet(100)
    w2 = Wallet(100)
    w1.add(50)
    print(w1.amount, w2.amount)

  22. Question 22 of 60 OOP features Medium

    What is the output of this polymorphic code?

    Python
    class Dog:
        def speak(self):
            return 'Woof'
    
    class Cat:
        def speak(self):
            return 'Meow'
    
    animals = [Dog(), Cat()]
    print([animal.speak() for animal in animals])

  23. Question 23 of 60 OOP features Medium

    A Car object creates and stores an Engine object as self.engine. Which OOP relationship does this primarily represent?

  24. Question 24 of 60 OOP features Medium

    What is printed by the following code?

    Python
    class Counter:
        total = 0
    
        def __init__(self):
            Counter.total += 1
    
    c1 = Counter()
    c2 = Counter()
    c1.total = 10
    print(c1.total, c2.total, Counter.total)

  25. Question 25 of 60 OOP features Medium

    Which method should be implemented so that print(book) displays a student-friendly description of a Book object?

  26. Question 26 of 60 OOP features Medium

    A function calls shape.area() on objects from unrelated classes without checking their types. Which Python OOP feature makes this design practical?

  27. Question 27 of 60 OOP features Medium

    What happens when this code executes?

    Python
    from abc import ABC, abstractmethod
    
    class Report(ABC):
        @abstractmethod
        def generate(self):
            pass
    
    class SalesReport(Report):
        pass
    
    r = SalesReport()

  28. Question 28 of 60 Encapsulation Medium

    What happens when the final statement executes?

    Python
    class Account:
        def __init__(self):
            self.__balance = 500
    
    a = Account()
    print(a.__balance)

  29. Question 29 of 60 Encapsulation Medium

    What is the result of assigning p.price = -20?

    Python
    class Product:
        def __init__(self, price):
            self._price = price
    
        @property
        def price(self):
            return self._price
    
        @price.setter
        def price(self, value):
            if value < 0:
                raise ValueError('Invalid price')
            self._price = value
    
    p = Product(50)
    p.price = -20

  30. Question 30 of 60 Encapsulation Medium

    What does a single leading underscore in an attribute such as self._score indicate in Python?

  31. Question 31 of 60 Encapsulation Medium

    What happens at the final statement?

    Python
    class Temperature:
        def __init__(self, value):
            self._value = value
    
        @property
        def value(self):
            return self._value
    
    t = Temperature(25)
    t.value = 30

  32. Question 32 of 60 Encapsulation Medium

    What is printed by this code?

    Python
    class Config:
        __mode = 'safe'
    
        @classmethod
        def get_mode(cls):
            return cls.__mode
    
    print(Config.get_mode(), hasattr(Config, '__mode'))

  33. Question 33 of 60 Encapsulation Medium

    What does the following code print?

    Python
    class Base:
        def __value(self):
            return 'Base'
    
        def show(self):
            return self.__value()
    
    class Child(Base):
        def __value(self):
            return 'Child'
    
    print(Child().show())

  34. Question 34 of 60 Encapsulation Medium

    What is printed by the final statement?

    Python
    class Cart:
        def __init__(self):
            self.__items = []
    
        @property
        def items(self):
            return self.__items.copy()
    
    cart = Cart()
    view = cart.items
    view.append('pen')
    print(cart.items)

  35. Question 35 of 60 Inheritance Medium

    What is printed by this code?

    Python
    class Person:
        def __init__(self, name):
            self.name = name
    
    class Student(Person):
        def __init__(self, name, grade):
            super().__init__(name)
            self.grade = grade
    
    s = Student('Asha', 9)
    print(s.name, s.grade)

  36. Question 36 of 60 Inheritance Medium

    What does this overridden method return?

    Python
    class Parent:
        def message(self):
            return 'P'
    
    class Child(Parent):
        def message(self):
            return super().message() + 'C'
    
    print(Child().message())

  37. Question 37 of 60 Inheritance Medium

    What is printed according to Python's method resolution order?

    Python
    class A:
        def run(self):
            print('A', end=' ')
    
    class B(A):
        def run(self):
            print('B', end=' ')
            super().run()
    
    class C(A):
        def run(self):
            print('C', end=' ')
            super().run()
    
    class D(B, C):
        pass
    
    D().run()

  38. Question 38 of 60 Inheritance Medium

    What is printed by the following code?

    Python
    class Base:
        rate = 5
    
    class Child(Base):
        pass
    
    c1 = Child()
    c2 = Child()
    c1.rate = 8
    print(c2.rate, Base.rate)

  39. Question 39 of 60 Inheritance Medium

    What does this code print?

    Python
    class Base:
        pass
    
    class Child(Base):
        pass
    
    obj = Child()
    print(isinstance(obj, Child), isinstance(obj, Base), type(obj) is Base)

  40. Question 40 of 60 Inheritance Medium

    What is printed by the following code?

    Python
    class Base:
        def greet(self):
            return 'Base'
    
    class Child(Base):
        def greet(self):
            return 'Child'
    
    c = Child()
    print(c.greet(), super(Child, c).greet())

  41. Question 41 of 60 OOP features Hard

    What does the following code print?

    Python
    class A:
        items = []
    
        def add(self, value):
            self.items += [value]
    
    a = A()
    b = A()
    a.add(1)
    print(A.items, b.items, a.items is b.items)

  42. Question 42 of 60 Encapsulation Hard

    What does the following code print?

    Python
    class A:
        def __f(self):
            return "A"
    
        def call(self):
            return self.__f()
    
    class B(A):
        def __f(self):
            return "B"
    
    print(B().call())

  43. Question 43 of 60 Encapsulation Hard

    What is printed when both a base class and its subclass declare a private slot with the same source-level name?

    Python
    class A:
        __slots__ = ("__x",)
    
        def __init__(self):
            self.__x = 1
    
        def from_a(self):
            return self.__x
    
    class B(A):
        __slots__ = ("__x",)
    
        def __init__(self):
            super().__init__()
            self.__x = 2
    
        def from_b(self):
            return self.__x
    
    b = B()
    print(b.from_a(), b.from_b())

  44. Question 44 of 60 Encapsulation Hard

    What does this code print?

    Python
    class C:
        @property
        def x(self):
            return 10
    
    c = C()
    c.__dict__["x"] = 99
    print(c.x, c.__dict__["x"])

  45. Question 45 of 60 Encapsulation Hard

    What is the result of this managed attribute lookup?

    Python
    class C:
        @property
        def x(self):
            raise AttributeError("hidden")
    
        def __getattr__(self, name):
            return f"fallback:{name}"
    
    print(C().x)

  46. Question 46 of 60 Encapsulation Hard

    What does the following code print?

    Python
    class Base:
        @property
        def x(self):
            return 1
    
    class Child(Base):
        x = 5
    
    c = Child()
    c.x = 9
    print(c.x)

  47. Question 47 of 60 Inheritance Hard

    What does Python print for this cooperative diamond hierarchy?

    Python
    class A:
        def f(self):
            return "A"
    
    class B(A):
        def f(self):
            return "B" + super().f()
    
    class C(A):
        def f(self):
            return "C" + super().f()
    
    class D(B, C):
        def f(self):
            return "D" + super().f()
    
    print(D().f())

  48. Question 48 of 60 Inheritance Hard

    What is the final trace produced by these cooperative initializers?

    Python
    class A:
        def __init__(self, **kwargs):
            self.trace = ["A"]
            super().__init__(**kwargs)
    
    class B(A):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.trace.append("B")
    
    class C(A):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.trace.append("C")
    
    class D(B, C):
        def __init__(self):
            super().__init__()
            self.trace.append("D")
    
    print(D().trace)

  49. Question 49 of 60 Inheritance Hard

    At which statement does Python first raise an exception?

    Python
    class X:
        pass
    
    class Y:
        pass
    
    class A(X, Y):
        pass
    
    class B(Y, X):
        pass
    
    class C(A, B):
        pass

  50. Question 50 of 60 Inheritance Hard

    What does this class-method call return?

    Python
    class A:
        @classmethod
        def who(cls):
            return cls.__name__
    
    class B(A):
        @classmethod
        def who(cls):
            return super().who() + ":B"
    
    print(B.who())

  51. Question 51 of 60 Inheritance Hard

    What happens in this abstract-base-class example?

    Python
    from abc import ABC, abstractmethod
    
    class A(ABC):
        @abstractmethod
        def f(self):
            return "A"
    
    class B(A):
        def f(self):
            return super().f() + "B"
    
    print(B().f())

  52. Question 52 of 60 Inheritance Hard

    Which result correctly describes this virtual subclass registration?

    Python
    from abc import ABC, abstractmethod
    
    class Protocol(ABC):
        @abstractmethod
        def run(self):
            pass
    
    class Worker:
        pass
    
    Protocol.register(Worker)
    w = Worker()

  53. Question 53 of 60 Inheritance Hard

    What does this code print after mutation through one subclass and rebinding through another?

    Python
    class A:
        data = []
    
    class B(A):
        pass
    
    class C(A):
        pass
    
    B.data.append(1)
    C.data = C.data + [2]
    print(A.data, B.data, C.data)

  54. Question 54 of 60 OOP features Hard

    Which special method handles the following addition?

    Python
    class A:
        def __add__(self, other):
            return "A.__add__"
    
    class B(A):
        def __radd__(self, other):
            return "B.__radd__"
    
    print(A() + B())

  55. Question 55 of 60 OOP features Hard

    What happens when hash(B()) is evaluated?

    Python
    class A:
        def __hash__(self):
            return 7
    
    class B(A):
        def __eq__(self, other):
            return True

  56. Question 56 of 60 OOP features Hard

    What does this code print?

    Python
    class C:
        def __len__(self):
            return 3
    
    c = C()
    c.__len__ = lambda: 9
    print(len(c), c.__len__())

  57. Question 57 of 60 OOP features Hard

    What is printed by this singleton-style implementation?

    Python
    class C:
        _one = None
    
        def __new__(cls):
            if cls._one is None:
                cls._one = super().__new__(cls)
            return cls._one
    
        def __init__(self):
            self.n = getattr(self, "n", 0) + 1
    
    a = C()
    b = C()
    print(a is b, a.n)

  58. Question 58 of 60 OOP features Hard

    After executing the setup below, which expression raises TypeError?

    Python
    class C:
        def f(self, x):
            return x
    
    c = C()
    c.g = C.f

  59. Question 59 of 60 Inheritance Hard

    What happens when the copied method is called?

    Python
    class A:
        def ping(self):
            return "A"
    
    class B(A):
        def ping(self):
            return super().ping() + "B"
    
    class C(A):
        ping = B.ping
    
    print(C().ping())

  60. Question 60 of 60 Encapsulation Hard

    What does this descriptor example print?

    Python
    class Label:
        def __get__(self, obj, owner):
            return "descriptor"
    
    class C:
        x = Label()
    
    c = C()
    c.x = "instance"
    print(c.x)

That's unit 3

Answer a few questions and your score shows up here.