Classes & Objects questions and answers
Attributes, inheritance, super(), dunder methods and properties. Practise classes & objects questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.
Sample classes & objects questions with worked answers
Question 1
Inside a method defined in a class, what does the first parameter, conventionally named self, refer to?
- The particular object on which the method was called
- The class in which the method is defined
- The parent class of the current class
- The module that contains the class
Answer: A. The particular object on which the method was called
Explanation: When you write obj.method(), Python passes obj itself as the first argument. That is how the method reaches the attributes of that specific object.
Question 2
What is the key difference between a method marked @classmethod and one marked @staticmethod?
- A static method can be called only on an object, never on the class
- A class method cannot be inherited by child classes, but a static method can
- A static method automatically receives the object as its first argument; a class method receives nothing
- A class method automatically receives the class as its first argument; a static method receives no automatic argument
Answer: D. A class method automatically receives the class as its first argument; a static method receives no automatic argument
Explanation: @classmethod passes the class (usually named cls) as the first argument, so it can read or change class-level data. @staticmethod is a plain function kept inside the class and gets neither self nor cls.
Question 3
D inherits from both B and C, and B and C both inherit from A. Which who() runs, and what is D's method resolution order?
class A:
def who(self): return "A"
class B(A):
def who(self): return "B"
class C(A):
def who(self): return "C"
class D(B, C):
pass
print(D().who(), [k.__name__ for k in D.__mro__])
- B ['D', 'B', 'A', 'C', 'object']
- C ['D', 'C', 'B', 'A', 'object']
- B ['D', 'B', 'C', 'A', 'object']
- A ['D', 'A', 'B', 'C', 'object']
Answer: C. B ['D', 'B', 'C', 'A', 'object']
Explanation: Python searches D first, then the parents in the order they are listed, B then C, and only after both does it go to their shared parent A. B is found first, so who() returns B.
All 20 classes & objects questions with answers
- A Student object is created and one of its attributes is updated. What does…
- Inside a method defined in a class, what does the first parameter…
- The deposit method of an Account object is called twice. What does the second…
- 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…
- 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…
- 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…
- 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()…
- Every class in this diamond calls super().go(). What string does D().go() build?
- The Coin class defines both __str__ and __repr__. What is printed for the…
- total is a property with only a getter. What happens when the code tries to…
- sell() uses self.stock -= 1 where stock starts as a class attribute, and the…
Classes & Objects: common questions
What are classes & objects questions?
Attributes, inheritance, super(), dunder methods and properties. They appear in the python section of many placement tests.
Where can I practise classes & objects questions with answers for free?
Right here on LogicWiz Exams. Every classes & objects question has a worked explanation after you answer. Practice is free and needs no sign-up.
How should I practise classes & objects?
Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.
Start practising
On this page you can start a fresh set of classes & objects questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.
More python topics
- Variables, Types & Operators: Dynamic typing, numbers, operators, precedence and truthiness.
- Python Strings: Indexing, slicing, methods, formatting and immutability.
- Lists & Tuples: Slicing, methods, copies, sorting and unpacking.
- Dictionaries & Sets: Lookups, iteration, hashable keys and set operations.
- Conditions & Loops: if/elif, for, while, range, break, continue and loop else.
- Functions & Scope: Arguments, defaults, *args, closures, lambda and recursion.
- Comprehensions & Generators: List, dict and set comprehensions, yield and lazy evaluation.
- Exceptions & Files: try/except/else/finally, raising errors and the with statement.
- Built-ins & Standard Library: sorted, zip, any/all, collections, itertools and imports.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- SQL: SQL quizzes by topic, from SELECT and joins to window functions: query output and concepts for analyst and developer roles.
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep