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)
  1. Guest
  2. None
  3. AttributeError
  4. 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

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