Python OOPS MCQ : Set 3

Python OOPS MCQ

1). What is the output of the following code?

				
					class MyClass:
    def __init__(self, name):
        self.name = name
    
    def display_name(self):
        print("Name:", self.name)

# Create an object of the class
obj = MyClass("Omkar")
obj.display_name()
				
			

Correct answer is: a) Name: Omkar
Explanation: The code defines a class called `MyClass` with an initializer method (`__init__`) and a `display_name` method. The `__init__` method initializes the instance variable `name` with the value passed as an argument. The value of ‘name’ is printed by the ‘display_name’ method. In the given code, an object `obj` of `MyClass` is created with the name “Omkar”. Then, the `display_name` method is called on `obj`, which prints “Name: Omkar”. Therefore, the output of the code is “Name: Omkar”.

2). What is the output of the following code?

				
					class MyClass:
    def __init__(self):
        self.instance_var = 25

# Create an object of the class
obj = MyClass()
print(obj.instance_var)
				
			

a) 25
b) None
c) AttributeError: ‘MyClass’ object has no attribute ‘instance_var’
d) SyntaxError: invalid syntax

Correct answer is: a) 25
Explanation: The code defines a class named `MyClass` with a constructor method `__init__`. Inside the constructor, an instance variable `instance_var` is assigned the value 25. After that, an object `obj` of the `MyClass` class is created using the constructor. When `obj.instance_var` is printed, it will output the value of the `instance_var` variable, which is 25. Therefore, the output of the code is 25.

3). What is the output of the following code?

				
					class MyClass:
    def __init__(self, name):
        self.name = name
    
    def display_name(self):
        print("Name:", self.name)
    
    def update_name(self, new_name):
        self.name = new_name

# Create an object of the class
obj = MyClass("Omkar")
obj.display_name()

obj.update_name("Ketan")
obj.display_name()
				
			

a) Name: Omkar
Name: Ketan
b) Name: Ketan
Name: Omkar
c) Name: Omkar
Name: Omkar
d) Name: Ketan
Name: Ketan

Correct answer is: a) Name: Omkar, Name: Ketan
Explanation: The code defines a class `MyClass` with an `__init__` method to initialize the `name` attribute, a `display_name` method to print the name, and an `update_name` method to update the name attribute. The object `obj` is created with the name “Omkar” and the `display_name` method is called, which prints “Name: Omkar”. Then, the `update_name` method is called to update the name attribute to “Ketan”. Finally, the `display_name` method is called again, which prints “Name: Ketan”.

4). What is the output of the following code?

				
					class MyClass:
    class_var = "Hello"
print(MyClass.class_var)
				
			

a) “Hello”
b) MyClass.class_var
c) AttributeError: type object ‘MyClass’ has no attribute ‘class_var’
d) SyntaxError: invalid syntax

Correct answer is: a) “Hello”
Explanation: In the given code, a class `MyClass` is defined with a class variable `class_var` assigned the value “Hello”. When the line `print(MyClass.class_var)` is executed, it accesses the class variable `class_var` using the class name `MyClass`. Therefore, the output of the code is “Hello”. The `print()` function displays the value of the class variable, which is “Hello” in this case.

5). What is the output of the following code?

				
					class MyClass:
    @staticmethod
    def static_method():
        print("This is a static method")

MyClass.static_method()
				
			

b) This is an instance method
c) AttributeError: ‘MyClass’ object has no attribute ‘static_method’
d) SyntaxError: invalid syntax

Correct answer is: a) This is a static method
Explanation: The output of the given code will be “This is a static method”. The code defines a class called `MyClass` with a static method named `static_method`. When the method is called using `MyClass.static_method()`, it prints the string “This is a static method” to the console. Static methods can be called on the class itself without creating an instance of the class, and they are not bound to any specific instance.

6). What is the output of the following code?

				
					class MyClass:
    class_var = "Hello"
    
    @classmethod
    def class_method(cls):
        print("Class variable:", cls.class_var)

MyClass.class_method()
				
			

a) Class variable: Hello
b) Class variable: MyClass.class_var
c) Class variable: Class method
d) Error: class_method() cannot be called directly

Correct answer is: a) Class variable: Hello
Explanation: “Class variable: Hello” will be the output of the given code. In the code, the class `MyClass` has a class variable `class_var` initialized with the string “Hello”. The `class_method` is a class method decorated with `@classmethod`. When the `class_method()` is called on the class `MyClass` using `MyClass.class_method()`, it will print the value of the class variable `class_var`, which is “Hello”. Therefore, the output will be “Class variable: Hello”.

7). What is the output of the following code?

				
					class MyClass:
    pass

obj = MyClass()
print("Class name:", obj.__class__.__name__)
print("Module name:", obj.__module__)
				
			

a) Class name: MyClass, Module name: __main__
b) Class name: MyClass, Module name: MyClass
c) Class name: object, Module name: __main__
d) Class name: object, Module name: MyClass

Correct answer is: a) Class name: MyClass, Module name: __main__
Explanation: In the given code, an instance of the class `MyClass` is created using the statement `obj = MyClass()`. The `__class__.__name__` attribute is used to retrieve the class name of the object, which in this case is “MyClass”. The `__module__` attribute is used to retrieve the name of the module in which the class is defined. When running the code directly, without importing the class from another module, the module name is set to “__main__”.

8). What is the output of the following code?

				
					    def __init__(self, name):
        self.name = name
    
    def display_name(self):
        print("Name:", self.name)

if __name__ == '__main__':
    obj = MyClass("Jason")
    obj.display_name()
				
			

a) Name: Jason
b) Name: MyClass
c) MyClass(“Jason”)
d) Name:

Correct answer is: a) Name: Jason
Explanation: The code defines a class `MyClass` with an `__init__` method that takes a parameter `name` and assigns it to the instance variable `self.name`. It also has a method `display_name` that prints the value of `self.name` preceded by the string “Name:”. In the `if __name__ == ‘__main__’:` block, an instance `obj` of `MyClass` is created with the argument “Jason”. Then, the `display_name` method of `obj` is called, which prints “Name: Jason”. Hence, the output of the code will be “Name: Jason”.

9). What is the output of the following code?

				
					class ParentClass:
    def parent_method(self):
        print("Parent method")

class ChildClass(ParentClass):
    def child_method(self):
        print("Child method")

# Create an object of the child class
obj = ChildClass()
obj.parent_method()
obj.child_method()

				
			

a) Parent method
Child method
b) Child method
Parent method
c) Error: ChildClass does not have a parent_method()
d) Error: ChildClass does not have a child_method()

Correct answer is: a) Parent method
Child method
Explanation: The code defines two classes: `ParentClass` and `ChildClass`. The `ChildClass` inherits from `ParentClass`. We create an object `obj` of the `ChildClass`. When we call `obj.parent_method()`, it invokes the `parent_method()` defined in the `ParentClass`, which prints “Parent method”. Similarly, when we call `obj.child_method()`, it invokes the `child_method()` defined in the `ChildClass`, which prints “Child method”.

10). What is the output of the following code?

				
					class ParentClass1:
    def parent_method1(self):
        print("Parent method 1")

class ParentClass2:
    def parent_method2(self):
        print("Parent method 2")

class ChildClass(ParentClass1, ParentClass2):
    def child_method(self):
        print("Child method")

# Create an object of the child class
obj = ChildClass()
obj.parent_method1()
obj.parent_method2()
obj.child_method()
				
			

a) Parent method 1
Parent method 2
Child method
b) Parent method 2
Parent method 1
Child method
c) Parent method 1
Child method
Parent method 2
d) Parent method 2
Child method
Parent method 1

Correct answer is: a) Parent method 1, Parent method 2, Child method
Explanation: The `ChildClass` inherits from `ParentClass1` and `ParentClass2`. When an object of `ChildClass` is created and its methods are called, the output will be “Parent method 1”, “Parent method 2”, and “Child method” in that order. This is because the inherited methods can be accessed and called using the object of the child class.

11). What is the output of the following code?

				
					class GrandparentClass:
    def grandparent_method(self):
        print("Grandparent method")

class ParentClass(GrandparentClass):
    def parent_method(self):
        print("Parent method")

class ChildClass(ParentClass):
    def child_method(self):
        print("Child method")

# Create an object of the child class
obj = ChildClass()
obj.grandparent_method()
obj.parent_method()
obj.child_method()
				
			

a) Grandparent method, Parent method, Child method
b) Child method, Parent method, Grandparent method
c) Grandparent method
d) Parent method, Child method

Correct answer is: a) Grandparent method, Parent method, Child method
Explanation: The given code defines three classes: `GrandparentClass`, `ParentClass`, and `ChildClass`. The `ChildClass` inherits from `ParentClass`, which in turn inherits from `GrandparentClass`.

12). What is the output of the following code?

				
					class ParentClass:
    def parent_method(self):
        print("Parent method")

class ChildClass1(ParentClass):
    def child_method1(self):
        print("Child 1 method")

class ChildClass2(ParentClass):
    def child_method2(self):
        print("Child 2 method")

# Create objects of the child classes
obj1 = ChildClass1()
obj2 = ChildClass2()

obj1.parent_method()
obj1.child_method1()

obj2.parent_method()
obj2.child_method2()
				
			

a) Parent method, Child 1 method, Parent method, Child 2 method
b) Child 1 method, Parent method, Child 2 method, Parent method
c) Parent method, Child 1 method, Child 2 method, Parent method
d) Child 1 method, Parent method, Parent method, Child 2 method

Correct answer is: c) Parent method, Child 1 method, Child 2 method, Parent method
Explanation: The code defines three classes: `ParentClass`, `ChildClass1`, and `ChildClass2`. `ChildClass1` and `ChildClass2` inherit from `ParentClass`. Two objects, `obj1` and `obj2`, are created using the child classes.
– The following methods are called:
1. `obj1.parent_method()`: This calls the `parent_method()` of the `ParentClass`, which prints “Parent method”.
2. `obj1.child_method1()`: This calls the `child_method1()` of `ChildClass1`, which prints “Child 1 method”.
3. `obj2.parent_method()`: This calls the `parent_method()` of the `ParentClass`, which prints “Parent method”.
4. `obj2.child_method2()`: This calls the `child_method2()` of `ChildClass2`, which prints “Child 2 method”.

13). What is the output of the following code?

				
					class ParentClass:
    def parent_method(self):
        print("Parent method")

class ChildClass(ParentClass):
    def parent_method(self):
        print("Child method (overrides parent)")

# Create an object of the child class
obj = ChildClass()
obj.parent_method()
				
			

a) “Parent method”
b) “Child method (overrides parent)”
c) Error: The code will raise an exception
d) No output: The code will not execute

Correct answer is: b) “Child method (overrides parent)”
Explanation: The code defines two classes, `ParentClass` and `ChildClass`, where `ChildClass` is a subclass of `ParentClass`. The `ChildClass` overrides the `parent_method` of the parent class. When an object of the `ChildClass` is created and the `parent_method()` is called on that object, the output will be “Child method (overrides parent)”. This is because the method in the child class overrides the method in the parent class, and the overridden method is executed when called on an instance of the child class.

14). What is the output of the following code?

				
					class MyClass:
    def method(self, param1, param2=None):
        if param2 is None:
            print("Single parameter:", param1)
        else:
            print("Two parameters:", param1, param2)

# Create an object of the class
obj = MyClass()

# Call the method with different number of arguments
obj.method("Hello")
obj.method("Hello", "World")
				
			

a) Single parameter: Hello, Two parameters: Hello World
b) Single parameter: Hello
Two parameters: Hello World
c) Two parameters: Hello
Two parameters: Hello World
d) Two parameters: Hello World

Correct answer is: b) Single parameter: Hello
Two parameters: Hello World
Explanation: The code defines a class `MyClass` with a method `method` that takes two parameters, `param1` and `param2`. The `param2` parameter is optional and has a default value of `None`. In the main part of the code, an object `obj` of the class `MyClass` is created. The method `method` is then called twice with different arguments. When `obj.method(“Hello”)` is called, it matches the method signature with a single argument. Inside the method, the condition `param2 is None` is true because no value is provided for `param2`. Therefore, the output is “Single parameter: Hello”. When `obj.method(“Hello”, “World”)` is called, it matches the method signature with two arguments. Inside the method, the condition `param2 is None` is false because a value is provided for `param2`. Therefore, the output is “Two parameters: Hello World”.

15). What is the output of the following code?

				
					from abc import ABC, abstractmethod
class AbstractClass(ABC):
    @abstractmethod
    def abstract_method(self):
        pass

class ConcreteClass(AbstractClass):
    def abstract_method(self):
        print("Implemented abstract method")

# Create an object of the concrete class
obj = ConcreteClass()
obj.abstract_method()
				
			

a) “Implemented abstract method”
b) This code will produce an error.
c) “AbstractClass object has no attribute ‘abstract_method'”
d) “ConcreteClass object has no attribute ‘abstract_method'”

Correct answer is: a) “Implemented abstract method”
Explanation: In the given code, we define an abstract base class `AbstractClass` that inherits from the `ABC` class. The `AbstractClass` contains an abstract method `abstract_method` decorated with the `@abstractmethod` decorator. The `ConcreteClass` inherits from `AbstractClass` and implements the `abstract_method` by printing “Implemented abstract method”.

16). What is the output of the following code?

				
					    def __init__(self):
        self.__private_var = 25
    
    def __private_method(self):
        print("Private method")

    def public_method(self):
        print("Public method")
        self.__private_method()

# Create an object of the class
obj = MyClass()

# Access public method and variable
obj.public_method()
print(obj._MyClass__private_var)
				
			

a) “Public method” followed by an error
b) “Public method” followed by “Private method” and then 25
c) “Private method” followed by an error
d) “Private method” followed by “Public method” and then 25

Correct answer is: b) “Public method” followed by “Private method” and then 25
Explanation: The code defines a class `MyClass` with a private variable `__private_var` and a private method `__private_method`. The `public_method` is a public method that prints “Public method” and calls the private method `__private_method()`. When an object `obj` of the class is created, and the `public_method()` is called, it will print “Public method” and then call the private method `__private_method()`, which will print “Private method”. Finally, the statement `print(obj._MyClass__private_var)` accesses the private variable `__private_var` using name mangling syntax and prints its value, which is 25.

17). What is the output of the following code?

				
					class Employee:
    def __init__(self, emp_id, name, salary):
        self.emp_id = emp_id
        self.name = name
        self.salary = salary
    
    def display_details(self):
        print("Employee ID:", self.emp_id)
        print("Name:", self.name)
        print("Salary:", self.salary)

class EmployeeManagementApp:
    def __init__(self):
        self.employees = []
    
    def add_employee(self, employee):
        self.employees.append(employee)
    
    def remove_employee(self, employee):
        if employee in self.employees:
            self.employees.remove(employee)
    
    def display_all_employees(self):
        for employee in self.employees:
            employee.display_details()

# Create an instance of the EmployeeManagementApp class
app = EmployeeManagementApp()

# Add employees to the application
employee1 = Employee(1, "Robert Yates", 50000)
employee2 = Employee(2, "Kelly Smith", 60000)
employee3 = Employee(3, "Michael Potter", 55000)

app.add_employee(employee1)
app.add_employee(employee2)
app.add_employee(employee3)

# Display all employees in the application
app.display_all_employees()
				
			

a) Employee ID: 3, Name: Robert Yates, Salary: 50000
Employee ID: 2, Name: Kelly Smith, Salary: 60000
Employee ID: 1, Name: Michael Potter, Salary: 55000
b) Employee ID: 1, Name: Robert Yates, Salary: 50000,
Employee ID: 2, Name: Kelly Smith, Salary: 60000,
Employee ID: 3, Name: Michael Potter, Salary: 55000,
c) Employee ID: 1, Name: Robert Yates, Salary: 50000
Employee ID: 3, Name: Kelly Smith, Salary: 60000
Employee ID: 2, Name: Michael Potter, Salary: 55000,
d) This code will result in an error.

Correct answer is: b) Employee ID: 1, Name: Robert Yates, Salary: 50000,
Employee ID: 2, Name: Kelly Smith, Salary: 60000,
Employee ID: 3, Name: Michael Potter, Salary: 55000,
Explanation: The code defines two classes, `Employee` and `EmployeeManagementApp`. The `Employee` class has an `__init__` method to initialize employee attributes and a `display_details` method to print the employee details. The `EmployeeManagementApp` class is responsible for managing a list of employees. In the code, an instance of the `EmployeeManagementApp` class is created, and three instances of the `Employee` class are created with different employee details. These employee instances are added to the `EmployeeManagementApp` instance using the `add_employee` method. Finally, the `display_all_employees` method is called on the `EmployeeManagementApp` instance, which iterates through the list of employees and calls the `display_details` method for each employee. This results in printing the details of all employees.

18). What is the output of the following code?

				
					class MyClass:
    def __init__(self):
        self._my_property = None
    
    @property
    def my_property(self):
        return self._my_property
    
    @my_property.setter
    def my_property(self, value):
        self._my_property = value

# Create an object of the class
obj = MyClass()

# Access the property
obj.my_property = "Hello"
print(obj.my_property)
				
			

a) “Hello”
b) None
c) Error: AttributeError
d) Error: SyntaxError

Correct answer is: a) “Hello”
Explanation: The code defines a class `MyClass` with a private instance variable `_my_property` and a property `my_property` that allows getting and setting the value of `_my_property`. After creating an object `obj` of the class `MyClass`, the line `obj.my_property = “Hello”` sets the value of `my_property` to “Hello” using the property setter. Finally, when `print(obj.my_property)` is executed, it accesses the value of `my_property`, which is “Hello” at that point, and prints it as the output.

19). What is the output of the following code?

				
					import math
class Circle:
    def __init__(self, radius):
        self.radius = radius
    
    def calculate_area(self):
        return math.pi * self.radius**2

# Create an object of the class
circle = Circle(10)
area = circle.calculate_area()
print("Area of the circle:", area)
				
			

a) Area of the circle: 31.41592653589793
b) Area of the circle: 314.1592653589793
c) Area of the circle: 3141.592653589793
d) Area of the circle: 0.3141592653589793

Correct answer is:
b) Area of the circle: 314.1592653589793
Explanation: The given code defines a class `Circle` with an `__init__` method and a `calculate_area` method. The `__init__` method initializes the `radius` attribute, and the `calculate_area` method calculates the area of the circle using the formula `math.pi * self.radius**2`. In the code, an object `circle` is created with a radius of 10. The `calculate_area` method is then called on the `circle` object, and the result is stored in the `area` variable. Finally, the calculated area is printed using the `print` statement.

20). What is the output of the following code?

				
					class Class1:
    def method1(self):
        print("Method 1 of Class 1")

class Class2(Class1):
    def method2(self):
        print("Method 2 of Class 2")

class Class3(Class2):
    def method3(self):
        print("Method 3 of Class 3")

class Class4(Class3):
    def method4(self):
        print("Method 4 of Class 4")

class Class5(Class4):
    def method5(self):
        print("Method 5 of Class 5")

# Create an object of the final class and access methods
obj = Class5()
obj.method1()
obj.method2()
obj.method3()
obj.method4()
obj.method5()
				
			

a) Method 1 of Class 1
Method 2 of Class 2
Method 3 of Class 3
Method 4 of Class 4
Method 5 of Class 5
b) Method 1 of Class 1
c) Method 1 of Class 1
Method 2 of Class 2
d) Method 1 of Class 1
Method 2 of Class 2
Method 3 of Class 3

Correct answer is: a) Method 1 of Class 1
Method 2 of Class 2
Method 3 of Class 3
Method 4 of Class 4
Method 5 of Class 5
Explanation: The given code defines a hierarchy of classes: Class1, Class2, Class3, Class4, and Class5. Each class has a method specific to its class name. The code creates an object of Class5 and calls various methods on it. Since Class5 is the final class in the inheritance hierarchy, it inherits all the methods from its superclass, Class4, which in turn inherits methods from Class3, Class2, and Class1.

21). What is the output of the following code?

				
					class MyClass:
    def __init__(self):
        pass
# Create an object of the class
obj = MyClass()

# Set instance variable dynamically
setattr(obj, "variable", "Value")

# Get instance variable dynamically
value = getattr(obj, "variable")
print(value)
				
			

a) “Value”
b) None
c) AttributeError: ‘MyClass’ object has no attribute ‘variable’
d) SyntaxError: invalid syntax

Correct answer is: a) “Value”
Explanation: The code creates an instance of the `MyClass` class using `obj = MyClass()`. Then, the `setattr` function is used to dynamically set an instance variable named “variable” with the value “Value”. Next, the `getattr` function retrieves the value of the “variable” instance variable. Finally, the value is printed, which will be “Value”. This is because `setattr` sets the instance variable “variable” to “Value”, and `getattr` retrieves its value successfully. Therefore, the output of the code will be “Value”.

22). What is the output of the following code?

				
					class MyClass:
    def __init__(self):
        self.__private_var = 10
    
    def __private_method(self):
        print("Private method")

    def public_method(self):
        print("Public method")
        self.__private_method()

# Create an object of the class
obj = MyClass()

# Access public method and variable
obj.public_method()
				
			

a) “Public method”
b) “Private method”
c) “Public method” followed by “Private method”
d) This code will result in an error.

Correct answer is: c) “Public method” followed by “Private method”
Explanation: The code creates a class `MyClass` with a private variable `__private_var` and a private method `__private_method`. It also has a public method `public_method` that prints “Public method” and calls the private method `__private_method`. When an object `obj` of the class `MyClass` is created, and the `public_method` is invoked using `obj.public_method()`, it first prints “Public method”. Then, within the `public_method`, the private method `__private_method` is called using `self.__private_method()`, which results in printing “Private method”.

Leave a Comment