Home »
MCQs
Python OOP Concepts MCQs (Multiple-Choice Questions)
Python Object-Oriented Programming (OOP) is a programming approach based on objects and classes. Python supports important OOP concepts such as classes, objects, inheritance, encapsulation, polymorphism, abstraction, methods, properties, special methods, and multiple inheritance.
Python OOP Concepts MCQs
These Python OOP Concepts multiple-choice questions cover classes, objects, constructors, instance and class attributes, methods, inheritance, method overriding, polymorphism, encapsulation, abstraction, properties, special methods, multiple inheritance, MRO, and data classes.
List of Python OOP Concepts MCQs
Practice these Python OOP Concepts MCQs with answers and explanations to improve your Python programming knowledge and prepare for exams, interviews, quizzes, and technical assessments.
1. What does OOP stand for?
- Object-Oriented Programming
- Object-Organized Processing
- Operational Object Programming
- Object-Oriented Processing
Answer: A) Object-Oriented Programming
Explanation:
OOP stands for Object-Oriented Programming, a programming approach based on objects and classes.
2. Which keyword is used to define a class in Python?
- object
- class
- define
- struct
Answer: B) class
Explanation:
The class keyword is used to define a new class in Python.
3. What is a class in Python?
- A template for creating objects
- Only a collection of variables
- A database table
- A Python module
Answer: A) A template for creating objects
Explanation:
A class defines the structure and behavior that its instances can have, including attributes and methods.
4. What is an object in Python?
- An instance of a class
- Only a class variable
- A function without parameters
- A module imported into Python
Answer: A) An instance of a class
Explanation:
An object is an instance created from a class and contains its own state while providing access to the behavior defined by the class.
5. Which syntax correctly creates an object of the Student class?
Student.create()
new Student()
Student()
object Student()
Answer: C) Student()
Explanation:
Calling a class, such as Student(), creates an instance of that class.
6. Which special method is commonly used to initialize a newly created object?
__start__()
__init__()
__create__()
__new_object__()
Answer: B) __init__()
Explanation:
__init__() is commonly used to initialize instance attributes after an instance has been created.
7. What does the self parameter normally refer to in an instance method?
- The current instance
- The parent class
- The Python interpreter
- The current module
Answer: A) The current instance
Explanation:
By convention, self refers to the current instance of the class and is used to access its attributes and methods.
8. Which statement about self is correct?
- It is a Python keyword that cannot be changed
- It is a conventional name for the instance parameter
- It always refers to the class itself
- It can only be used inside
__init__()
Answer: B) It is a conventional name for the instance parameter
Explanation:
self is the conventional name for the first parameter of an instance method. Python does not reserve it as a keyword.
9. What is an instance attribute?
- An attribute associated with a particular object
- An attribute shared only by imported modules
- An attribute that can only belong to a parent class
- An attribute defined outside Python
Answer: A) An attribute associated with a particular object
Explanation:
Instance attributes store data associated with individual objects and are commonly created using self.attribute.
10. What is a class attribute?
- An attribute defined on the class and potentially shared by its instances
- An attribute that can only exist inside
__init__()
- An attribute available only to functions outside the class
- An attribute that belongs exclusively to one object
Answer: A) An attribute defined on the class and potentially shared by its instances
Explanation:
A class attribute is defined in the class namespace and can be accessed through the class or its instances unless shadowed by an instance attribute.
11. Which method is automatically called when an instance is created using a class call?
__init__() is used for initialization
__print__()
__execute__()
__classcall__()
Answer: A) __init__() is used for initialization
Explanation:
After an instance is created, Python calls __init__() when it is defined, allowing the object to be initialized.
12. Which special method actually creates a new instance before initialization?
__create__()
__new__()
__init__()
__instance__()
Answer: B) __new__()
Explanation:
__new__() is responsible for creating and returning a new instance, while __init__() initializes the instance.
13. Which type of method receives the instance as its first argument by convention?
- Instance method
- Static method
- Class method
- Module method
Answer: A) Instance method
Explanation:
An instance method operates on an instance and conventionally receives the instance as its first parameter, usually named self.
14. Which decorator is used to define a class method?
@staticmethod
@classmethod
@classmethod()
@method
Answer: B) @classmethod
Explanation:
The @classmethod decorator converts a method into a class method whose first argument conventionally refers to the class.
15. What is conventionally used as the first parameter of a class method?
- self
- cls
- class
- this
Answer: B) cls
Explanation:
cls is the conventional name for the first parameter of a class method and represents the class.
16. Which decorator is used to define a static method?
@staticmethod
@static
@classmethod
@staticmethod()
Answer: A) @staticmethod
Explanation:
The @staticmethod decorator defines a method that does not receive an implicit instance or class argument.
17. Which statement best describes a static method?
- It automatically receives the current instance
- It automatically receives the current class
- It does not receive an implicit self or cls argument
- It can only be called from the class definition
Answer: C) It does not receive an implicit self or cls argument
Explanation:
A static method behaves like a regular function placed in a class namespace and does not receive an implicit instance or class argument.
18. What is inheritance in Python?
- A mechanism by which a class can derive attributes and methods from another class
- A method for converting Python into SQL
- A technique for importing packages
- A method for deleting objects
Answer: A) A mechanism by which a class can derive attributes and methods from another class
Explanation:
Inheritance allows a derived class to inherit attributes and methods from one or more base classes.
19. Which syntax correctly defines a derived class?
class Child extends Parent:
class Child(Parent):
class Child -> Parent:
inherit Child(Parent):
Answer: B) class Child(Parent):
Explanation:
Python specifies base classes inside parentheses in the class definition.
20. What is method overriding?
- Defining a subclass method with the same name as an inherited method
- Deleting a method from a parent class
- Calling a method twice
- Changing a method's indentation
Answer: A) Defining a subclass method with the same name as an inherited method
Explanation:
A derived class can provide its own implementation of a method inherited from a base class. This is known as method overriding.
21. Which function can be used to call a method from a parent or next class in the MRO?
parent()
super()
base()
ancestor()
Answer: B) super()
Explanation:
super() provides access to methods and attributes from a parent or next class according to the method resolution order.
22. What does MRO stand for in Python?
- Method Resolution Order
- Method Runtime Object
- Multiple Reference Order
- Module Resolution Object
Answer: A) Method Resolution Order
Explanation:
MRO stands for Method Resolution Order and determines the order in which Python searches classes for attributes and methods.
23. Which attribute provides the method resolution order of a class?
__order__
__mro__
__resolution__
__inheritance__
Answer: B) __mro__
Explanation:
The __mro__ attribute exposes the method resolution order of a class.
24. Which built-in function can also display the method resolution order of a class?
mro()
order()
resolve()
inherit()
Answer: A) mro()
Explanation:
The class method mro() returns the method resolution order for a class.
25. Does Python support multiple inheritance?
- Yes
- No
- Only for built-in classes
- Only for abstract classes
Answer: A) Yes
Explanation:
Python supports multiple inheritance, allowing a class to have more than one base class.
26. Which syntax represents multiple inheritance?
class C(A, B):
class C(A & B):
class C extends A, B:
class C -> A, B:
Answer: A) class C(A, B):
Explanation:
Multiple base classes are specified as a comma-separated list inside the class definition's parentheses.
27. Which built-in function checks whether an object is an instance of a class or its subclass?
typecheck()
isinstance()
instanceof()
checkinstance()
Answer: B) isinstance()
Explanation:
isinstance() checks whether an object is an instance of a specified class or of a class derived from it.
28. Which built-in function checks whether one class is a subclass of another?
isinstance()
issubclass()
isderived()
subclass()
Answer: B) issubclass()
Explanation:
issubclass() checks whether a class is derived from another class or from a class in a specified class hierarchy.
29. Which OOP concept allows the same interface to work with objects of different types?
- Polymorphism
- Compilation
- Serialization
- Iteration
Answer: A) Polymorphism
Explanation:
Polymorphism allows code to operate on different object types through a common interface or compatible behavior.
30. Which Python feature allows an operation such as + to behave differently depending on object types?
- Operator overloading
- Variable hiding
- Package inheritance
- Namespace merging
Answer: A) Operator overloading
Explanation:
Python special methods allow classes to define or customize behavior for operators such as +.
31. Which special method is associated with the + operator?
__plus__()
__add__()
__sum__()
__operator__()
Answer: B) __add__()
Explanation:
The special method __add__() defines behavior for the + operator when implemented by a class.
32. Which special method provides the official string representation commonly used by repr()?
__str__()
__repr__()
__string__()
__display__()
Answer: B) __repr__()
Explanation:
__repr__() defines the representation of an object used by repr().
33. Which special method is commonly used to define an object's user-friendly string representation?
__text__()
__str__()
__display__()
__stringify__()
Answer: B) __str__()
Explanation:
__str__() defines the string representation used by str() and commonly by print().
34. What is encapsulation in OOP?
- Combining data and behavior within an object while controlling access to implementation details
- Creating multiple unrelated modules
- Executing all methods simultaneously
- Converting objects into databases
Answer: A) Combining data and behavior within an object while controlling access to implementation details
Explanation:
Encapsulation groups data and behavior together and can use naming conventions and properties to control how an object's internal state is accessed.
35. What does a single leading underscore generally indicate in a Python attribute name?
- It is intended as a non-public implementation detail
- It is a constant
- It must be private at the language level
- It is automatically converted into a class variable
Answer: A) It is intended as a non-public implementation detail
Explanation:
A single leading underscore is a convention indicating that an attribute or method is intended for internal or non-public use. It does not create strict language-level privacy.
36. What happens to an attribute with two leading underscores in a class?
- Python applies name mangling
- Python automatically deletes the attribute
- Python makes the attribute globally accessible
- Python converts it into a static method
Answer: A) Python applies name mangling
Explanation:
Names beginning with two underscores in a class are subject to name mangling, which helps avoid accidental name conflicts in subclasses.
37. Which feature provides a managed attribute interface using getter-like and setter-like methods?
- property
- iterator
- generator
- namespace
Answer: A) property
Explanation:
The property mechanism allows attribute-style access while defining methods that control getting, setting, or deleting a value.
38. Which decorator is commonly used to define a property getter?
@property
@getter
@attribute
@getproperty
Answer: A) @property
Explanation:
The @property decorator turns a method into a property that can be accessed using attribute syntax.
39. Which decorator is commonly used to define the setter for an existing property named age?
@age.setter
@setter.age
@property.set
@age.set
Answer: A) @age.setter
Explanation:
After defining age as a property, @age.setter can be used to define its setter method.
40. What is abstraction in OOP?
- Exposing only relevant interfaces while hiding unnecessary implementation details
- Creating a copy of every object
- Removing all methods from a class
- Making every attribute public
Answer: A) Exposing only relevant interfaces while hiding unnecessary implementation details
Explanation:
Abstraction focuses on the essential interface or behavior while hiding implementation details that users of the class do not need to know.
41. Which standard library module provides infrastructure for Abstract Base Classes?
- abc
- abstract
- interfaces
- oop
Answer: A) abc
Explanation:
The abc module provides infrastructure for defining and working with Abstract Base Classes in Python.
42. Which class can be inherited to conveniently define an Abstract Base Class?
ABC
AbstractClass
BaseABC
Interface
Answer: A) ABC
Explanation:
The abc module provides the ABC helper class for defining Abstract Base Classes.
43. Which decorator marks a method as abstract?
@abstractmethod
@abstract
@virtualmethod
@interface
Answer: A) @abstractmethod
Explanation:
The @abstractmethod decorator marks a method as abstract. A class containing abstract methods cannot normally be instantiated until the abstract requirements are implemented.
44. Which statement about Python abstract methods is correct?
- They can have an implementation
- They must always contain no code
- They can only be used outside classes
- They automatically become static methods
Answer: A) They can have an implementation
Explanation:
Python abstract methods may have an implementation. A subclass can override the method and can also use super() to call the abstract method's implementation.
45. What is a dataclass in Python?
- A class for which Python can automatically generate several special methods based on declared fields
- A class that can only store database records
- A class that cannot have methods
- A replacement for all Python classes
Answer: A) A class for which Python can automatically generate several special methods based on declared fields
Explanation:
The dataclasses module provides the @dataclass decorator, which can generate methods such as __init__() and __repr__() based on declared fields.
46. Which decorator is used to create a dataclass?
@dataclass
@data
@classdata
@record
Answer: A) @dataclass
Explanation:
The @dataclass decorator from the dataclasses module transforms a class into a data class.
47. Which parameter of @dataclass can emulate immutability by preventing normal attribute assignment after initialization?
immutable=True
frozen=True
readonly=True
static=True
Answer: B) frozen=True
Explanation:
Using frozen=True with @dataclass prevents normal assignment and deletion of fields after initialization by providing methods that raise FrozenInstanceError.
48. Which special method is used to define behavior for equality comparison?
__compare__()
__eq__()
__equals__()
__same__()
Answer: B) __eq__()
Explanation:
The special method __eq__() defines behavior for equality comparison using the == operator.
49. What is the root base class of ordinary Python classes?
object
Base
Class
Root
Answer: A) object
Explanation:
Python's class hierarchy ultimately includes the built-in object class. Classes without an explicit base class implicitly derive from it.
50. Which statement best describes OOP in Python?
- Python supports classes, objects, inheritance, polymorphism, encapsulation, and abstraction
- Python supports only procedural programming
- Python classes cannot inherit from other classes
- Python objects cannot contain methods
Answer: A) Python supports classes, objects, inheritance, polymorphism, encapsulation, and abstraction
Explanation:
Python supports object-oriented programming through classes and objects, inheritance, method overriding, polymorphism, properties, abstract base classes, special methods, and other object-oriented features.
Advertisement
Advertisement