Python Classes and Objects MCQ Questions and Answers

1. What is a class in Python?

a) A collection of functions
b) A blueprint for creating objects
c) A built-in data type
d) A library for mathematical operations

Answer:

b) A blueprint for creating objects

Explanation:

In Python, a class is a blueprint for creating objects with specific attributes and methods.

2. What is an object in Python?

a) A variable
b) An instance of a class
c) A built-in data type
d) A module in a program

Answer:

b) An instance of a class

Explanation:

An object is an instance of a class, created based on the class blueprint.

3. How are attributes defined in a Python class?

a) As global variables outside the class
b) As local variables inside the class methods
c) As instance variables inside the class constructor
d) As class variables shared among all instances

Answer:

c) As instance variables inside the class constructor

Explanation:

Attributes in a Python class are defined as instance variables inside the class constructor.

4. What is the self keyword used for in Python class methods?

a) To access the class itself
b) To access the superclass
c) To refer to the current instance of the class
d) To create a new instance of the class

Answer:

c) To refer to the current instance of the class

Explanation:

The self keyword is used to refer to the current instance of the class in class methods.

5. What is encapsulation in object-oriented programming?

a) A process of creating objects from classes
b) The ability to hide the internal details of an object
c) The process of inheritance
d) The process of object destruction

Answer:

b) The ability to hide the internal details of an object

Explanation:

Encapsulation is the concept of hiding the internal details of an object and providing controlled access to its attributes and methods.

6. In Python, which method is automatically called when an object is created from a class?

a) init()
b) constructor()
c) create()
d) new()

Answer:

a) init()

Explanation:

The __init__() method is automatically called when an object is created from a class in Python.

7. What is inheritance in object-oriented programming?

a) The process of creating new classes from existing ones
b) The process of creating new objects
c) The process of encapsulating data
d) The process of creating global variables

Answer:

a) The process of creating new classes from existing ones

Explanation:

Inheritance is the process of creating new classes by deriving them from existing classes.

8. What is method overriding in Python?

a) The process of creating new methods
b) The process of replacing a method in a superclass with a new method in a subclass
c) The process of deleting methods from a class
d) The process of renaming methods

Answer:

b) The process of replacing a method in a superclass with a new method in a subclass

Explanation:

Method overriding is the process of providing a new implementation for a method in a subclass that is already defined in its superclass.

9. Which access modifier in Python allows an attribute or method to be accessed only within its class?

a) Public
b) Private
c) Protected
d) Hidden

Answer:

b) Private

Explanation:

Private attributes and methods in Python can only be accessed within their class.

10. Which access modifier in Python allows an attribute or method to be accessed within its class and subclasses?

a) Public
b) Private
c) Protected
d) Hidden

Answer:

c) Protected

Explanation:

Protected attributes and methods in Python can be accessed within their class and subclasses.

11. How can you create a class variable in Python?

a) Define it within a class method
b) Define it outside the class
c) Define it within the constructor
d) Define it within an instance method

Answer:

b) Define it outside the class

Explanation:

Class variables in Python are defined outside the class.

12. What is the purpose of the @staticmethod decorator in Python?

a) To define class methods
b) To define instance methods
c) To define static methods that are not bound to instances
d) To define private methods

Answer:

c) To define static methods that are not bound to instances

Explanation:

The @staticmethod decorator is used to define static methods in Python, which are not bound to instances.

13. Which special method in Python is used to define a custom string representation of an object?

a) str()
b) repr()
c) str_rep()
d) object_str()

Answer:

b) repr()

Explanation:

The __repr__() special method is used to define a custom string representation of an object.

14. In Python, what is the purpose of the super() function?

a) To create a new instance of a class
b) To call a method in the superclass
c) To access private attributes
d) To define class attributes

Answer:

b) To call a method in the superclass

Explanation:

The super() function is used to call a method in the superclass from a subclass.

15. What is the primary advantage of using object-oriented programming (OOP) in Python?

a) Improved performance
b) Code reusability and modularity
c) Reduced memory usage
d) Simplified syntax

Answer:

b) Code reusability and modularity

Explanation:

OOP in Python promotes code reusability and modularity, making it easier to manage and maintain code.

16. How do you create an instance of a class in Python?

a) Using the new keyword
b) Using the create keyword
c) Using the instantiate method
d) Using the class name followed by parentheses

Answer:

d) Using the class name followed by parentheses

Explanation:

An instance of a class in Python is created by using the class name followed by parentheses.

17. What is the purpose of the __del__() special method in Python?

a) To define a custom destructor for a class
b) To define a custom constructor for a class
c) To access private attributes
d) To define a custom string representation of an object

Answer:

a) To define a custom destructor for a class

Explanation:

The __del__() special method is used to define a custom destructor for a class in Python.

18. How do you access an instance variable in a Python class?

a) Using the class name
b) Using the self keyword
c) Using the super keyword
d) Using the this keyword

Answer:

b) Using the self keyword

Explanation:

Instance variables in a Python class are accessed using the self keyword.

19. What is the purpose of the isinstance() function in Python?

a) To check if an object is an instance of a class
b) To check if an object is a subclass of a class
c) To check if an object is a module
d) To check if an object is a function

Answer:

a) To check if an object is an instance of a class

Explanation:

The isinstance() function is used to check if an object is an instance of a specified class.

20. In Python, what is a constructor?

a) A method that creates an instance of a class
b) A method that initializes instance variables
c) A method that defines class variables
d) A method that defines static methods

Answer:

b) A method that initializes instance variables

Explanation:

A constructor in Python is a method that initializes instance variables when an object is created.


Comments