Classes Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q1
What is the purpose of the __init__ method in a Python class?
  • A To destroy the object
  • B To initialize the object
  • C To represent the class as string
  • D To compare two objects
Answer: Option B
Explanation: The __init__ method is a constructor that initializes new objects when they are created.
Q2
Which keyword is used to create a class in Python?
  • A class
  • B def
  • C struct
  • D object
Answer: Option A
Explanation: The "class" keyword is used to define a new class in Python.
Q3
What does "self" represent in a class method?
  • A The class itself
  • B The parent class
  • C The current object instance
  • D A static method
Answer: Option C
Explanation: The "self" parameter refers to the current instance of the class.
Q4
Which method is called when an object is created?
  • A __del__
  • B __init__
  • C __str__
  • D __main__
Answer: Option B
Explanation: The __init__ method is the constructor called when a new object is instantiated.
Q5
What is inheritance in Python classes?
  • A Creating multiple objects
  • B Hiding implementation details
  • C Inheriting properties from parent class
  • D Importing modules
Answer: Option C
Explanation: Inheritance allows a class to inherit attributes and methods from another class.
Q6
How do you call a parent class method from a child class?
  • A parent()
  • B super()
  • C base()
  • D self.parent()
Answer: Option B
Explanation: The super() function is used to call parent class methods.
Q7
What is method overriding in Python?
  • A Creating multiple methods with same name
  • B Hiding a method from parent class
  • C Providing new implementation in child class
  • D Deleting a parent method
Answer: Option C
Explanation: Method overriding allows a child class to provide a specific implementation of a method already defined in its parent class.
Q8
What is encapsulation in OOP?
  • A Hiding implementation details
  • B Inheriting from multiple classes
  • C Bundling data and methods together
  • D Creating abstract classes
Answer: Option C
Explanation: Encapsulation is the bundling of data and methods that operate on that data within one unit (class).
Q9
How do you create a private variable in Python?
  • A Using private keyword
  • B Prefix with single underscore
  • C Prefix with double underscores
  • D Using var keyword
Answer: Option C
Explanation: Private variables in Python are created by prefixing the variable name with double underscores.
Q10
What is polymorphism in Python?
  • A Multiple inheritance
  • B Same method behaving differently for different objects
  • C Method overloading
  • D Data hiding
Answer: Option B
Explanation: Polymorphism allows methods to do different things based on the object that is acting upon them.
Questions and Answers for Competitive Exams Various Entrance Test