Python Inheritance MCQ Questions and Answers

1. What is inheritance in Python?

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 defining instance variables

Answer:

a) The process of creating new classes from existing ones

Explanation:

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

2. What is a superclass in Python?

a) A class that inherits from another class
b) A class that is derived from another class
c) A class that contains only instance variables
d) A class that cannot have methods

Answer:

b) A class that is derived from another class

Explanation:

A superclass is a class that is derived from another class (parent class).

3. What is a subclass in Python?

a) A class that inherits from another class
b) A class that is derived from another class
c) A class that contains only class variables
d) A class that cannot have attributes

Answer:

a) A class that inherits from another class

Explanation:

A subclass is a class that inherits from another class (parent class).

4. What keyword is used to specify inheritance in a Python class?

a) inherits
b) base
c) extends
d) class

Answer:

d) class

Explanation:

The class keyword is used to define a Python class that can inherit from another class.

5. What is the primary advantage of using inheritance in Python?

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

Answer:

a) Code reusability and modularity

Explanation:

Inheritance in Python promotes code reusability and modularity.

6. Which method in Python is automatically called when an object is created from a subclass?

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 subclass.

7. 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 renaming methods
d) The process of calling superclass methods directly

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.

8. Which keyword is used to call a method from the superclass in a Python subclass?

a) base
b) super
c) parent
d) sub

Answer:

b) super

Explanation:

The super keyword is used to call a method from the superclass in a Python subclass.

9. What is the purpose of the super() function in Python?

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

Answer:

b) To access the superclass

Explanation:

The super() function is used to access the superclass in Python.

10. Which access modifier in Python allows a subclass to access attributes and methods of its superclass?

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

Answer:

c) Protected

Explanation:

Protected attributes and methods in Python allow a subclass to access attributes and methods of its superclass.

11. What is multiple inheritance in Python?

a) The ability to inherit from multiple subclasses
b) The ability to inherit from multiple superclasses
c) The ability to inherit from multiple instances
d) The ability to inherit from built-in classes

Answer:

b) The ability to inherit from multiple superclasses

Explanation:

Multiple inheritance in Python allows a class to inherit from multiple superclasses.

12. What is the method resolution order (MRO) in Python?

a) The order in which methods are defined in a class
b) The order in which methods are called in a subclass
c) The order in which the base classes are searched to find a method or attribute
d) The order in which instances are created in a program

Answer:

c) The order in which the base classes are searched to find a method or attribute

Explanation:

The Method Resolution Order (MRO) defines the order in which base classes are searched to find a method or attribute.

13. Which keyword is used to check if a class is a subclass of another class in Python?

a) extends
b) inherits
c) subclass
d) issubclass

Answer:

d) issubclass

Explanation:

The issubclass keyword is used to check if a class is a subclass of another class in Python.

14. What is a constructor in Python?

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:

a) A method that creates an instance of a class

Explanation:

A constructor in Python is a method that creates an instance of a class.

15. 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.

16. How can you prevent a method in a superclass from being overridden in a subclass?

a) Make the method private
b) Use the final keyword
c) Use the protected keyword
d) It cannot be prevented from being overridden

Answer:

b) Use the final keyword

Explanation:

You can prevent a method from being overridden in a subclass by using the final keyword.

17. What is an abstract class in Python?

a) A class that cannot be instantiated
b) A class that has only class variables
c) A class that cannot have methods
d) A class that cannot have attributes

Answer:

a) A class that cannot be instantiated

Explanation:

An abstract class in Python is a class that cannot be instantiated and is meant to be subclassed.

18. How do you declare an abstract method in Python?

a) By using the abstract keyword
b) By using the virtual keyword
c) By using the @abstractmethod decorator
d) By using the override keyword

Answer:

c) By using the @abstractmethod decorator

Explanation:

Abstract methods in Python are declared using the @abstractmethod decorator.

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. What is the result of calling a method in a subclass that has the same name as a method in the superclass?

a) An error is raised
b) The method in the superclass is called
c) The method in the subclass is called
d) Both methods are called in order

Answer:

c) The method in the subclass is called

Explanation:

When a method with the same name is defined in both the superclass and subclass, the method in the subclass is called (method overriding).


Comments