What will be the output of the following code?
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return 'Some sound'
class Dog(Animal):
def speak(self):
return 'Woof'
d = Dog('Rex')
print(d.speak())
Answer: B
Dog overrides the speak() method of Animal. When speak() is called on a Dog instance, Python uses method resolution order (MRO) and finds speak() in Dog first, so it returns 'Woof'. This is an example of method overriding in inheritance.
Q.2Medium
Which of the following best describes the purpose of the __str__ method in a Python class?
Answer: B
__str__ is a dunder (magic) method that defines the human-readable string representation of an object. When you call print(obj) or str(obj), Python internally calls obj.__str__(). It is meant to be informative and readable for end users.
Q.3Medium
What will be the output of the following code?
class Counter:
count = 0
def __init__(self):
Counter.count += 1
a = Counter()
b = Counter()
c = Counter()
print(Counter.count)
Answer: C
count is a class variable shared across all instances. Each time __init__ is called (once per object creation), Counter.count is incremented by 1. Since three objects (a, b, c) are created, Counter.count becomes 3.
Q.4Medium
What will be the output of the following code?
class MyClass:
def __init__(self, x):
self.__x = x
obj = MyClass(10)
print(obj.__x)
Answer: C
__x is a name-mangled private attribute. Python internally renames it to _MyClass__x to prevent direct external access. Accessing obj.__x from outside the class raises an AttributeError. The correct way to access it would be obj._MyClass__x.
Q.5Medium
Which of the following correctly defines a class method in Python?
Answer: C
A class method is defined using the @classmethod decorator and takes cls as its first argument, which refers to the class itself rather than an instance. Option A is an instance method, Option B is a static method (with wrong usage), and Option D uses @classmethod but incorrectly names the first parameter self instead of cls (though it would still work, cls is the convention and expected answer).
Advertisement
Q.6Medium
What is the output of the following code?
class Shape:
def area(self):
return 0
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r * self.r
shapes = [Shape(), Circle(7)]
for s in shapes:
print(s.area())
Answer: B
This demonstrates polymorphism. The list contains a Shape and a Circle. When area() is called on the Shape instance, it returns 0. When called on the Circle instance with r=7, it returns 3.14 * 7 * 7 = 153.86. Python dynamically resolves the correct method for each object.
Q.7Medium
What will be the output of the following code?
class A:
def greet(self):
return 'Hello from A'
class B(A):
def greet(self):
return super().greet() + ' and B'
obj = B()
print(obj.greet())
Answer: B
super().greet() in class B calls the greet() method of the parent class A, which returns 'Hello from A'. The string ' and B' is concatenated to it. So the final output is 'Hello from A and B'. The super() function is used to call methods from the parent class.
Q.8Medium
What will be the output of the following code?
class Vehicle:
def __init__(self, brand):
self.brand = brand
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
self.model = model
c = Car('Toyota', 'Camry')
print(c.brand, c.model)
Answer: A
Car's __init__ calls super().__init__(brand), which invokes Vehicle's __init__ and sets self.brand = 'Toyota'. Then self.model = 'Camry' is set in Car's __init__. So c.brand is 'Toyota' and c.model is 'Camry', printing 'Toyota Camry'.
Q.9Medium
What is the output of the following code?
class Box:
def __init__(self, vol):
self.vol = vol
def __gt__(self, other):
return self.vol > other.vol
b1 = Box(10)
b2 = Box(20)
print(b1 > b2)
Answer: B
The __gt__ method is a dunder method that overloads the > operator. When b1 > b2 is evaluated, Python calls b1.__gt__(b2), which returns self.vol > other.vol, i.e., 10 > 20, which is False. This is an example of operator overloading in Python.
Q.10Medium
What will be the output of the following code?
class Foo:
def __init__(self):
self.x = 10
def bar(self):
self.x += 5
return self.x
obj1 = Foo()
obj2 = Foo()
obj1.bar()
print(obj1.x, obj2.x)
Answer: C
Each instance of Foo has its own copy of self.x initialized to 10. Calling obj1.bar() modifies only obj1.x by adding 5, making it 15. obj2 is a separate instance and its x remains 10. So the output is '15 10', demonstrating instance variable independence.