Related chapter: Object-Oriented Programming


Q1. What is self in a Python method?

  • A) A keyword like this in Java
  • B) The first parameter referring to the instance ✓
  • C) The class itself
  • D) Optional and can be omitted

Q2. Which method is called when creating an instance?

  • A) __new__ only
  • B) __init__
  • C) __create__
  • D) constructor()

__new__ creates the object; __init__ initializes it. You almost always define __init__.


Q3. What does @property do?

  • A) Makes a method private
  • B) Turns a method into an attribute-like accessor ✓
  • C) Creates a static method
  • D) Defines a class variable

Q4. What is the output?

  class A:
    x = 1
class B(A):
    pass
print(B.x)
  
  • A) Error
  • B) 1
  • C) None
  • D) 0

Class attributes are inherited.


Q5. What does super().__init__() do?

  • A) Creates a new superclass
  • B) Calls the parent class’s __init__
  • C) Returns the parent object
  • D) Deletes the parent class

Q6. Which naming convention indicates “private” in Python?

  • A) private name
  • B) __name (double underscore) ✓
  • C) #name
  • D) @private name

Python uses name mangling (_ClassName__name), not true privacy.


Q7. What is polymorphism?

  • A) Having many classes
  • B) Different classes responding to the same interface ✓
  • C) Copying a class
  • D) Deleting a parent class

Q8. What does __str__ control?

  • A) String comparison
  • B) User-friendly string representation via print()
  • C) String concatenation
  • D) String encoding

Q9. Can Python have multiple inheritance?

  • A) No
  • B) Yes ✓
  • C) Only with Java syntax
  • D) Only for abstract classes
  class D(B, C):  # Valid Python
    pass
  

Q10. What is composition?

  • A) Inheriting from multiple classes
  • B) Building classes by containing other objects ✓
  • C) Combining strings
  • D) A type of loop

“Has-a” (composition) is often preferred over “is-a” (inheritance).


Next: Intermediate Quiz | Practice: OOP Exercises