0% found this document useful (0 votes)
16 views16 pages

MCQ Set 2

Uploaded by

nithiinkumar20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

MCQ Set 2

Uploaded by

nithiinkumar20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Course: Mastering Object-Oriented Concepts in Python & CSE3216

Multiple Choice Questions

Q1: Which statement about classes in Python is true?

- A) A class is an instance of an object.


- B) A class is a blueprint for creating objects.
- C) A class is a type of function.
- D) A class cannot contain methods.

Answer: B) A class is a blueprint for creating objects.

Q2: What is an object in Python?

- A) A container for storing functions only.


- B) A container for storing classes.
- C) An instance of a class.
- D) A collection of functions and variables only.

Answer: C) An instance of a class.

Q3: The init method is automatically called when:

- A) A class is defined
- B) An object is created
- C) A method is called
- D) A variable is created

Answer: B) An object is created

Q4: In which of the following scenarios is the constructor useful?

- A) When initializing default values for instance variables


- B) When defining static methods
- C) When importing a module
- D) When printing a list

Answer: A) When initializing default values for instance variables

Q5: What is the global namespace in Python?

- A) A namespace that exists only within functions


- B) A namespace accessible from anywhere in the code

- C) A namespace that stores private variables


- D) A temporary namespace within loops

Answer: B) A namespace accessible from anywhere in the code


Q6: Which namespace is created when a function is called?

- A) Global namespace
- B) Built-in namespace
- C) Local namespace
- D) Module namespace

Answer: C) Local namespace

Q7: How is encapsulation achieved in Python?

- A) By using public variables


- B) By using functions only
- C) By defining methods and using private or protected variables
- D) By using classes without methods

Answer: C) By defining methods and using private or protected variables

Q8: Which syntax is used to define a private variable in Python?

- A) varName
- B) varName
- C) public varName
- D) _varName

Answer: B) varName

Q9: Abstraction is typically achieved using:

- A) Static methods only


- B) Public variables
- C) Abstract classes and interfaces
- D) Constructors only

Answer: C) Abstract classes and interfaces

Q10: Which of the following is an example of abstraction?

- A) Hiding data by marking it as private


- B) Hiding implementation details and showing only the necessary features
- C) Providing access to all methods
- D) Defining a single-level inheritance

Answer: B) Hiding implementation details and showing only the necessary features

Q11: In Python, the class keyword is used for:

- A) Defining variables
- B) Defining lists
- C) Creating a new class
- D) Writing comments

Answer: C) Creating a new class

Q12: Which of the following is NOT a valid class name in Python?

- A) MyClass
- B) 1stClass
- C) ClassOne
- D) _HiddenClass

Answer: B) 1stClass

Q13: The self parameter is required in:

- A) Static methods
- B) Instance methods
- C) Class methods
- D) Functions outside classes

Answer: B) Instance methods

Q14: In a class, the self variable refers to:

- A) The method itself


- B) The class itself
- C) The instance of the class
- D) The module name

Answer: C) The instance of the class

Q15: What is the correct way to define a class method?

- A) By using @static
- B) By using @class
- C) By using @classmethod
- D) By using @instancemethod

Answer: C) By using @classmethod

Q16: What parameter is typically used in a class method to represent the class?

- A) self
- B) cls
- C) class
- D) instance

Answer: B) cls
Q17: Which of the following statements about static methods is true?

- A) Static methods cannot be called on the class itself.


- B) Static methods cannot access or modify class or instance state.
- C) Static methods are only used within instance methods.
- D) Static methods require a self parameter.

Answer: B) Static methods cannot access or modify class or instance state.

Q18: How can you access the attributes of one class in another class?

- A) By using the global keyword


- B) By passing the instance of one class as an argument to the other
- C) By defining all variables as public
- D) By importing it as a module

Answer: B) By passing the instance of one class as an argument to the other

Q19: Inheritance allows:

- A) Code reuse by inheriting properties from another class


- B) Access to private variables directly
- C) Only one class to inherit from another
- D) Multiple instances of a class to exist

- A) Code reuse by inheriting properties from another class

Answer: A) Code reuse by inheriting properties from another class

Q20: In a single inheritance hierarchy, a derived class inherits from:

- A) Multiple base classes


- B) Exactly two base classes
- C) One base class
- D) All classes in the module

Answer: C) One base class

Q1: The dir() function when called on an object returns:

- A) All attributes and methods of the object

- B) Only instance attributes


- C) Only class variables

- D) Only methods

Answer: A) All attributes and methods of the object

Q2: What will happen if you try to access an attribute that doesn't exist in the class?

- A) A NameError will be raised

- B) The program will crash

- C) An AttributeError will be raised

- D) None of the above

Answer: C) An AttributeError will be raised

Q3: Which of the following methods is called automatically when you try to print an object?

- A) __str__()

- B) __init__()

- C) __new__()

- D) __repr__()

Answer: A) __str__()

Q4: Which access modifier would you use to define an attribute accessible only within the class?

- A) Protected

- B) Public

- C) Private

- D) Static

Answer: C) Private

Q5: The method __len__() is used to:

- A) Define the size of an object


- B) Initialize an object

- C) Represent the length of an object

- D) All of the above

Answer: C) Represent the length of an object

Q6: If a class has the method __call__() , the object of that class can be:

- A) Called like a function

- B) Printed directly

- C) Used in a loop

- D) None of the above

Answer: A) Called like a function

Q7: If you try to modify a read-only property of a class, which error is raised?

- A) TypeError

- B) ValueError

- C) AttributeError

- D) SyntaxError

Answer: C) AttributeError

Q8: Which of the following is true about method overriding?

- A) Overriding requires the super() function

- B) Overriding is defining a new method with the same name in the child class

- C) Only private methods can be overridden

- D) None of the above

Answer: B) Overriding is defining a new method with the same name in the child class

Q9: In case of multiple inheritance, which algorithm does Python use to resolve method names?
- A) Depth-first search

- B) Method Resolution Order (MRO)

- C) Breadth-first search

- D) First-Come, First-Serve (FCFS)

Answer: B) Method Resolution Order (MRO)

Q10: A setter method in Python allows us to:

- A) Change a read-only property

- B) Access a property

- C) Delete a property

- D) Override a private method

Answer: A) Change a read-only property

Q11: To create a setter for a property, we use which decorator?

- A) @property

- B) @property_name.setter

- C) @setter

- D) @property_name.getter

Answer: B) @property_name.setter

Q12: In Python, if a property’s getter method is defined but no setter is defined, the property is:

- A) Read-write

- B) Write-only

- C) Read-only

- D) Write-accessible only for child classes

Answer: C) Read-only
Q13: When two methods in a class have the same name but different parameters, it is called:

- A) Overloading

- B) Overriding

- C) Abstracting

- D) Encapsulating

Answer: A) Overloading

Q14: Python does not support method overloading directly, but similar behavior can be achieved using:

- A) Default arguments

- B) @staticmethod

- C) Abstract methods

- D) None of the above

Answer: A) Default arguments

Q15: What is inheritance in Python?

- A) A way to reuse code by creating subclasses from existing classes.

- B) A process of encapsulating methods in classes.

- C) A method to initialize objects in Python.

- D) A function to define attributes.

Answer: A) A way to reuse code by creating subclasses from existing classes.

Q16: Which of the following is not a type of inheritance in Python?

- A) Single Inheritance

- B) Multiple Inheritance

- C) Linear Inheritance

- D) Multilevel Inheritance

Answer: C) Linear Inheritance


Q17: In Python, a subclass is also known as:

- A) Derived class

- B) Parent class

- C) Base class

- D) Abstract class

Answer: A) Derived class

Q18: Which inheritance type allows a class to inherit from multiple parent classes?

- A) Single Inheritance

- B) Multiple Inheritance

- C) Hierarchical Inheritance

- D) Multilevel Inheritance

Answer: B) Multiple Inheritance

Q19: A class derived from a single base class is an example of:

- A) Multiple Inheritance

- B) Single Inheritance

- C) Hierarchical Inheritance

- D) Hybrid Inheritance

Answer: B) Single Inheritance

Q20: In Python, which built-in function returns the base classes of a class?

- A) super()

- B) base()

- C) __bases__

- D) mro()
Answer: C) __bases__

Q21: What is Hybrid Inheritance?

- A) Inheriting from a single base class only.

- B) Combining two or more types of inheritance.

- C) Inheriting methods without overriding.

- D) Avoiding the use of MRO.

Answer: B) Combining two or more types of inheritance.

Q22: Which method is used to call the superclass constructor in Python?

- A) __init__()

- B) super()

- C) base()

- D) parent()

Answer: B) super()

Q23: Which of these statements is true about inheritance?

- A) A subclass can’t override methods from the superclass.

- B) A subclass can have multiple superclasses in Python.

- C) Inheritance cannot have private methods.

- D) Inheritance is only used with classes that have __init__() methods.

Answer: B) A subclass can have multiple superclasses in Python.

Q24: What is hierarchical inheritance?

- A) One base class with multiple derived classes

- B) Multiple base classes with a single derived class

- C) Derived classes inheriting from each other


- D) None of the above

Answer: A) One base class with multiple derived classes

Q25: Single inheritance allows a derived class to inherit from:

- A) Multiple base classes

- B) A single base class

- C) Itself

- D) All the above

Answer: B) A single base class

Q26: In single inheritance, which class is known as the child class?

- A) Base class

- B) Parent class

- C) Derived class

- D) None of the above

Answer: C) Derived class

Q27: Which of the following is an example of single inheritance?

- A) class A(B):

- B) class A(B, C):

- C) class A(B, C, D):

- D) class A(B) and class B(C):

Answer: A) class A(B):

Q28: Single inheritance allows for the reuse of code in:

- A) A single derived class

- B) Multiple classes at once


- C) Only in parent classes

- D) None of the above

Answer: A) A single derived class

Q29: In single inheritance, which class does not inherit any features?

- A) Base class

- B) Derived class

- C) Child class

- D) None of the above

Answer: A) Base class

Q30: Multiple inheritance means inheriting from:

- A) One base class

- B) Two or more base classes

- C) No base classes

- D) Only abstract classes

Answer: B) Two or more base classes

Q31: What is the main challenge with multiple inheritance?

- A) It is always efficient.

- B) Confusion in method resolution.

- C) Reduced reusability.

- D) Limited

access to attributes.

Answer: B) Confusion in method resolution.

Q32: Which Python feature resolves the order of calling methods in multiple inheritance?
- A) Encapsulation

- B) Polymorphism

- C) MRO

- D) None of the above

Answer: C) MRO

Q33: In multiple inheritance, the method resolution order (MRO) is determined using:

- A) Depth-first search

- B) Linearization of the hierarchy

- C) Breadth-first search

- D) FCFS rule

Answer: B) Linearization of the hierarchy

Q34: Hybrid inheritance is a combination of:

- A) Single and Multiple inheritance

- B) Hierarchical and Single inheritance

- C) Multilevel and Hierarchical inheritance

- D) All the above

Answer: D) All the above

Q35: Which inheritance allows a class to inherit attributes of more than one class?

- A) Multiple Inheritance

- B) Hybrid Inheritance

- C) Single Inheritance

- D) None

Answer: A) Multiple Inheritance


Q36: What keyword is used in Python to inherit a class?

- A) inherit

- B) extends

- C) inherits

- D) None (inheritance happens directly with class names)

Answer: D) None (inheritance happens directly with class names)

Q37: The MRO is used for:

- A) Defining the parent class

- B) Defining the base class

- C) Defining the class resolution order

- D) None

Answer: C) Defining the class resolution order

Q38: Which Python tool helps to analyze MRO?

- A) super()

- B) mro()

- C) __dict__

- D) isinstance()

Answer: B) mro()

Q39: What is the main purpose of encapsulation in OOP?

- A) To define attributes only

- B) To restrict access to certain attributes and methods

- C) To inherit attributes only

- D) To overload methods

Answer: B) To restrict access to certain attributes and methods


Q40: A method that can be accessed outside a class is known as:

- A) Private method

- B) Public method

- C) Encapsulated method

- D) None of the above

Answer: B) Public method

Q41: Which syntax is used for inheritance in Python?

- A) class Child(Parent):

- B) class Parent->Child:

- C) inherit Child from Parent

- D) Parent: Child

Answer: A) class Child(Parent):

Q42: Which of these will call the base class method?

- A) super().__init__()

- B) base()

- C) super()

- D) super.method()

Answer: A) super().__init__()

Q43: To make a read-only attribute, define a:

- A) Setter

- B) Property

- C) None of the above

- D) staticmethod
Answer: B) Property

Q44: How can Python prevent method conflicts in multiple inheritance?

- A) Using encapsulation

- B) Using Method Resolution Order (MRO)

- C) By defining more subclasses

- D) By deleting parent classes

Answer: B) Using Method Resolution Order (MRO)

You might also like