Classes & Objects · Question 9 · Medium
The child class Student defines its own __init__ and does not call the parent's. What is the result of this code?
class Person:
def __init__(self, name="Guest"):
self.name = name
class Student(Person):
def __init__(self, roll):
self.roll = roll
s = Student(7)
print(s.name)
- Guest
- None
- AttributeError
- 7
Answer and explanation
Answer: C. AttributeError
Explanation: Student.__init__ replaces Person.__init__, and it never calls super().__init__(). So self.name is never set, and reading s.name raises AttributeError.
Previous: wheels is a class attribute of Car, and it is then assigned through one… · Next: Three classes in a chain each extend fee() using super(). What does the last…
Practise classes & objects
Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.
More classes & objects questions
- Square overrides the area method of its parent Shape. What is printed for a…
- The method on() in the Lamp class is written without a self parameter. What…
- wheels is a class attribute of Car, and it is then assigned through one…
- Three classes in a chain each extend fee() using super(). What does the last…
- The Point class defines __eq__. What do == and is give for two separately…
- A class method reads a counter that __init__ increases on the class. What is…
- fahrenheit is declared with @property and is accessed without brackets. What…
- What is the key difference between a method marked @classmethod and one marked…
- B is a child of A. What do isinstance, a direct type comparison and issubclass…
- D inherits from both B and C, and B and C both inherit from A. Which who()…
More python practice
Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge