Editor

Python OOP MCQ questions with answers

Classes, objects, inheritance and special methods, with explanations.

0 of 20 answered

  1. Question 1 · Easy

    What is the output of this code?

    class Student:
        def __init__(self, name, grade):
            self.name = name
            self.grade = grade
    
    s = Student("Asha", 11)
    print(s.name, s.grade + 1)
    Show the answer

    Answer: A

    Student("Asha", 11) creates an object and runs __init__, which stores the two values on it as attributes. s.grade + 1 adds 1 to the stored number 11, giving 12; the attribute itself stays 11.

    Run this code in the editor
  2. Question 2 · Easy

    What is the output of this code?

    class Rectangle:
        def __init__(self, width, height):
            self.width = width
            self.height = height
    
        def area(self):
            return self.width * self.height
    
    r = Rectangle(4, 5)
    r.width = 10
    print(r.area())
    Show the answer

    Answer: C

    A method reads the object's attributes when it is called, not when the object was created. After r.width = 10, area() computes 10 * 5 = 50.

    Run this code in the editor
  3. Question 3 · Easy

    What is the output of this code?

    class Animal:
        def __init__(self, name):
            self.name = name
    
        def speak(self):
            return self.name + " makes a sound"
    
    class Cat(Animal):
        def speak(self):
            return self.name + " says meow"
    
    for pet in [Animal("Rex"), Cat("Tom")]:
        print(pet.speak())
    Show the answer

    Answer: B

    Cat inherits __init__ from Animal, so Cat("Tom") still gets a name. It overrides speak, so a Cat uses its own version while a plain Animal keeps the parent's. Only one speak runs for each call.

    Run this code in the editor
  4. Question 4 · Easy

    What is the output of this code?

    class Account:
        def __init__(self, balance=0):
            self.balance = balance
    
        def deposit(self, amount):
            self.balance += amount
            return self.balance
    
    acc = Account(100)
    acc.deposit(50)
    print(acc.deposit(25))
    Show the answer

    Answer: D

    Each call to deposit changes self.balance, the attribute stored on the object, so the changes add up: 100 + 50 = 150, then 150 + 25 = 175. The second call returns the new balance, which is printed.

    Run this code in the editor
  5. Question 5 · Medium

    What is the output of this code?

    class Counter:
        count = 0
    
        def __init__(self):
            Counter.count += 1
    
    a = Counter()
    b = Counter()
    c = Counter()
    print(Counter.count, a.count)
    Show the answer

    Answer: D

    count is a class attribute: one value shared by the class and all its objects. Each __init__ adds 1 to Counter.count, so after three objects it is 3. a has no attribute of its own named count, so a.count reads the class attribute, also 3.

    Run this code in the editor
  6. Question 6 · Medium

    What is the output of this code?

    class Person:
        def __init__(self, name):
            self.name = name
            print("Person init")
    
    class Teacher(Person):
        def __init__(self, name, subject):
            super().__init__(name)
            self.subject = subject
            print("Teacher init")
    
    t = Teacher("Meera", "Physics")
    print(t.name, t.subject)
    Show the answer

    Answer: B

    Creating a Teacher runs Teacher.__init__, whose first line calls the parent's __init__ through super(). That prints "Person init" and sets name; then the rest of Teacher.__init__ sets subject and prints "Teacher init".

    Run this code in the editor
  7. Question 7 · Medium

    What is the output of this code?

    class Score:
        def __init__(self, points):
            self.points = points
    
        def __add__(self, other):
            return Score(self.points + other.points)
    
        def __str__(self):
            return f"{self.points} points"
    
    total = Score(50) + Score(25)
    print(total)
    Show the answer

    Answer: C

    Score(50) + Score(25) calls Score(50).__add__(Score(25)), which adds the two numbers and returns a new Score. print then uses __str__, so the result shows as 75 points.

    Run this code in the editor
  8. Question 8 · Medium

    What is the output of this code?

    class Circle:
        def __init__(self, radius):
            self.radius = radius
    
        @property
        def diameter(self):
            return self.radius * 2
    
    c = Circle(3)
    c.radius = 5
    print(c.diameter)
    Show the answer

    Answer: A

    @property turns diameter into a method that is read like an attribute, without parentheses. It is computed each time it is read, so after radius changes to 5 it returns 10, not the 6 it would have given when the object was created.

    Run this code in the editor
  9. Question 9 · Hard

    What is the output of this code?

    class Dog:
        sound = "woof"
    
    a = Dog()
    b = Dog()
    a.sound = "grr"
    Dog.sound = "bark"
    print(a.sound, b.sound)
    Show the answer

    Answer: C

    a.sound = "grr" creates an instance attribute on a only, which hides the class attribute for a. b has no attribute of its own, so it reads Dog.sound, which has been changed to "bark".

    Run this code in the editor
  10. Question 10 · Hard

    What is the output of this code?

    class Team:
        members = []
    
        def add(self, name):
            self.members.append(name)
    
    red = Team()
    blue = Team()
    red.add("Asha")
    blue.add("Ravi")
    print(red.members)
    Show the answer

    Answer: D

    members is a class attribute, so there is one list shared by every Team. self.members.append changes that shared list rather than creating one per object, so both names end up in it. Creating the list in __init__ with self.members = [] gives each team its own.

    Run this code in the editor
  11. Question 11 · Hard

    What is the output of this code?

    class Point:
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def __str__(self):
            return f"({self.x}, {self.y})"
    
        def __repr__(self):
            return f"Point({self.x}, {self.y})"
    
    p = Point(1, 2)
    print(p)
    print([p])
    Show the answer

    Answer: A

    print(p) uses __str__, giving (1, 2). A list shows its items with repr(), even when the list itself is printed, so print([p]) uses __repr__ and shows [Point(1, 2)].

    Run this code in the editor
  12. Question 12 · Hard

    What is the output of this code?

    class Temperature:
        unit = "C"
    
        @classmethod
        def describe(cls):
            return "Unit is " + cls.unit
    
        @staticmethod
        def to_f(c):
            return c * 9 / 5 + 32
    
    class Kelvin(Temperature):
        unit = "K"
    
    print(Kelvin.describe(), Temperature.to_f(100))
    Show the answer

    Answer: B

    A classmethod receives the class it was called on as cls, so Kelvin.describe() reads Kelvin.unit, which is "K". A staticmethod gets no automatic first argument and works like a plain function; / makes its result a float, 212.0.

    Run this code in the editor
  13. Question 13 · Easy

    Which error does this code raise?

    class Greeter:
        def hello():
            return "hello"
    
    g = Greeter()
    print(g.hello())
    Show the answer

    Answer: B, TypeError: Greeter.hello() takes 0 positional arguments but 1 was given

    Calling a method on an object passes the object as the first argument automatically. hello was defined with no parameters, so it cannot accept that argument and Python raises TypeError. Defining it as def hello(self): fixes it.

    Run this code in the editor
  14. Question 14 · Medium

    Which error does this code raise?

    class Person:
        def __init__(self, name):
            self.name = name
    
    class Student(Person):
        def __init__(self, roll):
            self.roll = roll
    
    s = Student(7)
    print(s.roll, s.name)
    Show the answer

    Answer: A, AttributeError: 'Student' object has no attribute 'name'

    Student defines its own __init__, which replaces the parent's, and it never calls super().__init__(...). So self.name is never set, and reading s.name raises AttributeError. Python does not call the parent's __init__ for you.

    Run this code in the editor
  15. Question 15 · Medium

    Which error does this code raise?

    class Circle:
        def __init__(self, radius):
            self.radius = radius
    
        @property
        def area(self):
            return 3.14 * self.radius ** 2
    
    c = Circle(2)
    c.area = 20
    print(c.area)
    Show the answer

    Answer: D, AttributeError: property 'area' of 'Circle' object has no setter

    A property defined with @property alone is read-only: reading c.area works, but assigning to it raises AttributeError. To allow assignment, add a setter method decorated with @area.setter.

    Run this code in the editor
  16. Question 16 · Easy

    Inside a method, what does self refer to?

    Show the answer

    Answer: C, The object the method was called on

    obj.method() is the same as ClassName.method(obj): Python passes the object itself as the first argument, which by convention is named self. Through self the method reads and changes that object's attributes.

  17. Question 17 · Easy

    When does a class's __init__ method run?

    Show the answer

    Answer: A, Each time a new object of the class is created

    Defining the class does not call __init__. Every ClassName(...) call creates a new object and then runs __init__ on it with the arguments given, which is where attributes are usually set up.

  18. Question 18 · Medium

    If a class does not define __eq__, what does a == b check for two of its objects?

    Show the answer

    Answer: C, Whether a and b are the same object

    Without __eq__, == falls back to identity, the same test as is. Two objects with equal attributes are still different objects, so a == b is False. Define __eq__ to compare attributes instead.

  19. Question 19 · Easy

    Which class gives each object its own name, so that Pet("Tom").name is "Tom"?

    Show the answer

    Answer: B

    __init__ needs self as its first parameter and must store the value on the object with self.name = name. The third option assigns only to the local variable name, the first has no self, and a method called init is never run automatically.

  20. Question 20 · Medium

    Class Date has __init__(self, year, month, day). Which method, added to it, makes Date.from_text("2026-10-22") return a new Date?

    Show the answer

    Answer: D

    @classmethod passes the class in as cls, so cls(...) builds a new object; this is the usual way to write an alternative constructor. Without a decorator, the text lands in self and text is missing. In the second option the class itself fills text, leaving no parameter for the string, so the call raises TypeError.

Keep going