Classes Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
What is the difference between class and instance variables?
  • A No difference
  • B Class variables are per instance, instance variables are shared
  • C Class variables are shared, instance variables are per object
  • D Both are the same
Answer: Option C
Explanation: Class variables are shared among all instances, while instance variables are unique to each instance.
Q12
How do you create a static method in Python?
  • A @staticmethod decorator
  • B static keyword
  • C def static()
  • D Using static method
Answer: Option A
Explanation: Static methods are created using the @staticmethod decorator and don't take self or cls parameters.
Q13
What is a class method in Python?
  • A Method that only works with instances
  • B Method that takes cls parameter and works with class
  • C Method that cannot access class variables
  • D Method that is private to class
Answer: Option B
Explanation: Class methods take cls as first parameter and can access and modify class state.
Q14
What is the purpose of the __str__ method?
  • A String conversion of object
  • B Object destruction
  • C Object comparison
  • D Object creation
Answer: Option A
Explanation: The __str__ method returns a string representation of the object, used by print() and str().
Q15
What is multiple inheritance in Python?
  • A One class inheriting from one parent
  • B One class inheriting from multiple parents
  • C Multiple classes inheriting from one parent
  • D No inheritance
Answer: Option B
Explanation: Multiple inheritance allows a class to inherit from more than one parent class.
Q16
What is an abstract class in Python?
  • A Class that can be instantiated directly
  • B Class with no methods
  • C Class that cannot be instantiated and requires method implementation
  • D Class with only variables
Answer: Option C
Explanation: Abstract classes cannot be instantiated and require subclasses to implement abstract methods.
Q17
How do you create a property in Python?
  • A Using property keyword
  • B @property decorator
  • C def property()
  • D create_property()
Answer: Option B
Explanation: Properties are created using the @property decorator to define getter methods.
Q18
What is method overloading in Python?
  • A Creating multiple methods with same name but different parameters
  • B Only one method per class
  • C Method that overloads memory
  • D Inheriting methods
Answer: Option A
Explanation: Python doesn't support traditional method overloading, but you can use default arguments or variable arguments to achieve similar functionality.
Q19
What is the MRO (Method Resolution Order) in Python?
  • A Method return order
  • B Method resolution order for inheritance
  • C Memory read order
  • D Module load order
Answer: Option B
Explanation: MRO determines the order in which base classes are searched when looking for a method.
Q20
What is a destructor method in Python?
  • A __init__
  • B __del__
  • C __destroy__
  • D __cleanup__
Answer: Option B
Explanation: The __del__ method is called when an object is about to be destroyed.
Questions and Answers for Competitive Exams Various Entrance Test