Classes & Objects · Question 7 · Easy
The method on() in the Lamp class is written without a self parameter. What happens when it is called on an object?
class Lamp:
def on():
return "glowing"
lamp = Lamp()
print(lamp.on())
- glowing
- TypeError
- None
- AttributeError
Answer and explanation
Answer: B. TypeError
Explanation: lamp.on() passes the object as the first argument automatically, but on() accepts no parameters. Python raises TypeError saying on() takes 0 positional arguments but 1 was given.
Previous: Square overrides the area method of its parent Shape. What is printed for a… · Next: wheels is a class attribute of Car, and it is then assigned through one…
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
- Which special method does Python call automatically, right after an object is…
- Manager inherits from Employee and defines nothing of its own. What does…
- Square overrides the area method of its parent Shape. What is printed for a…
- wheels is a class attribute of Car, and it is then assigned through one…
- The child class Student defines its own __init__ and does not call the…
- 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…
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