Python OOP MCQ questions with answers
Classes, objects, inheritance and special methods, with explanations.
0 of 20 answered
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
Run this code in the editorStudent("Asha", 11)creates an object and runs__init__, which stores the two values on it as attributes.s.grade + 1adds 1 to the stored number 11, giving 12; the attribute itself stays 11.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
Run this code in the editorr.width = 10,area()computes 10 * 5 = 50.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
Run this code in the editorCatinherits__init__fromAnimal, soCat("Tom")still gets a name. It overridesspeak, so a Cat uses its own version while a plain Animal keeps the parent's. Only onespeakruns for each call.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
Run this code in the editordepositchangesself.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.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
Run this code in the editorcountis a class attribute: one value shared by the class and all its objects. Each__init__adds 1 toCounter.count, so after three objects it is 3.ahas no attribute of its own namedcount, soa.countreads the class attribute, also 3.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
Run this code in the editorTeacher.__init__, whose first line calls the parent's__init__throughsuper(). That prints "Person init" and setsname; then the rest ofTeacher.__init__setssubjectand prints "Teacher init".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
Run this code in the editorScore(50) + Score(25)callsScore(50).__add__(Score(25)), which adds the two numbers and returns a new Score.printthen uses__str__, so the result shows as 75 points.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
Run this code in the editor@propertyturnsdiameterinto a method that is read like an attribute, without parentheses. It is computed each time it is read, so afterradiuschanges to 5 it returns 10, not the 6 it would have given when the object was created.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
Run this code in the editora.sound = "grr"creates an instance attribute onaonly, which hides the class attribute fora.bhas no attribute of its own, so it readsDog.sound, which has been changed to "bark".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
Run this code in the editormembersis a class attribute, so there is one list shared by every Team.self.members.appendchanges that shared list rather than creating one per object, so both names end up in it. Creating the list in__init__withself.members = []gives each team its own.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
Run this code in the editorprint(p)uses__str__, giving (1, 2). A list shows its items withrepr(), even when the list itself is printed, soprint([p])uses__repr__and shows [Point(1, 2)].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
Run this code in the editorcls, soKelvin.describe()readsKelvin.unit, which is "K". A staticmethod gets no automatic first argument and works like a plain function;/makes its result a float, 212.0.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.
Run this code in the editorhellowas defined with no parameters, so it cannot accept that argument and Python raises TypeError. Defining it asdef hello(self):fixes it.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'
Run this code in the editorStudentdefines its own__init__, which replaces the parent's, and it never callssuper().__init__(...). Soself.nameis never set, and readings.nameraises AttributeError. Python does not call the parent's__init__for you.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
Run this code in the editor@propertyalone is read-only: readingc.areaworks, but assigning to it raises AttributeError. To allow assignment, add a setter method decorated with@area.setter.Question 16 · Easy
Inside a method, what does
selfrefer to?Show the answer
Answer: C, The object the method was called on
obj.method()is the same asClassName.method(obj): Python passes the object itself as the first argument, which by convention is namedself. Throughselfthe method reads and changes that object's attributes.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__. EveryClassName(...)call creates a new object and then runs__init__on it with the arguments given, which is where attributes are usually set up.Question 18 · Medium
If a class does not define
__eq__, what doesa == bcheck 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 asis. Two objects with equal attributes are still different objects, soa == bis False. Define__eq__to compare attributes instead.Question 19 · Easy
Which class gives each object its own
name, so thatPet("Tom").nameis"Tom"?Show the answer
Answer: B
__init__needsselfas its first parameter and must store the value on the object withself.name = name. The third option assigns only to the local variablename, the first has noself, and a method calledinitis never run automatically.Question 20 · Medium
Class
Datehas__init__(self, year, month, day). Which method, added to it, makesDate.from_text("2026-10-22")return a newDate?Show the answer
Answer: D
@classmethodpasses the class in ascls, socls(...)builds a new object; this is the usual way to write an alternative constructor. Without a decorator, the text lands inselfandtextis missing. In the second option the class itself fillstext, leaving no parameter for the string, so the call raises TypeError.
Keep going
- Read the lesson behind these questions: Classes and objects in the PythonBook
- Next topic: Python file handling MCQ questions with answers
- Before this: Python exception handling MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.