WorksheetsOOPs in Python
Total questions: 20
Worksheet time: 30mins
What is the output of the following code?
class A:
def init(self):
self.x = 1
class B(A):
def init(self):
super().__init__()
self.y = 2
b = B()
print(b.x, b.y)
1 2
Error
2 1
None
What is the output of the following code?
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
d = Dog()
d.speak()
Animal speaks
Dog barks
Error
None
Which of the following is true about multiple inheritance in Python?
Python does not support multiple inheritance
A class can inherit from only two parent classes
A class can inherit from multiple parent classes
Multiple inheritance is only possible with abstract classes
What is the output of the following code?
class A:
def init(self):
self.x = 1
def method(self):
print("Method of class A")
class B(A):
def method(self):
super().method()
print("Method of class B")
b = B()
b.method()
Method of class A
Method of class B
Method of class A
Method of class B
Error
Which of the following is used to implement abstraction in Python?
@abstractmethod decorator
abstract keyword
interface keyword
virtual keyword
What is the output of the following code?
class A:
x = 1
a1 = A()
a2 = A()
a1.x = 2
print(a1.x, a2.x, A.x)
2 1 1
2 2 2
2 1 2
Error
Which of the following is true about the `__init__` method in Python?
It must return a value
It is called automatically when a class is instantiated
It can only have one parameter
It is optional in all classes
What is the output of the following code?
class A:
def str(self):
return "Class A"
class B(A):
pass
b = B()
print(b)
Class A
Class B
<__main__.B object at ...>
Error
Which of the following is an example of polymorphism in Python?
Method overloading
Method overriding
Multiple inheritance
All of the above
What is the output of the following code?
class A:
def method(self):
print("A's method")
class B(A):
def method(self):
print("B's method")
class C(B):
def method(self):
super().method()
print("C's method")
c = C()
c.method()
A's method
C's method
B's method
C's method
C's method
A's method
B's method
C's method
Which of the following statements is true about encapsulation in Python?
Encapsulation is achieved by using private and protected access modifiers.
Encapsulation is achieved by using public access modifiers only.
Encapsulation is not supported in Python.
Encapsulation is achieved by using the `@abstractmethod` decorator.
What is the output of the following code?
class A:
def init(self):
self.__x = 10
def get_x(self):
return self.__x
class B(A):
def init(self):
super().__init__()
self.__x = 20
b = B()
print(b.get_x())
10
20
Error
None
Which of the following is an example of polymorphism in Python?
A class inheriting from multiple parent classes.
A method in a child class overriding a method in the parent class.
Using the `@staticmethod` decorator.
Using the `@classmethod` decorator.
What is the output of the following code?
class A:
def method(self):
print("A's method")
class B(A):
def method(self):
print("B's method")
class C(B):
pass
c = C()
c.method()
A's method
B's method
C's method
Error
In multiple-level inheritance, which method is called first if a method is not found in the child class?
The method in the parent class.
The method in the grandparent class.
The method in the sibling class.
The method in the child class.
What is the output of the following code?
class A:
def init(self):
self.x = 1
class B(A):
def init(self):
super().__init__()
self.y = 2
class C(B):
def init(self):
super().__init__()
self.z = 3
c = C()
print(c.x, c.y, c.z)
1 2 3
3 2 1
1 3 2
Error
Which of the following is true about private attributes in Python?
They can be accessed directly from outside the class.
They can be accessed using the `self` keyword.
They can be accessed using the `super` keyword.
They cannot be accessed or modified.
What is the output of the following code?
class A:
def method(self):
print("A's method")
class B(A):
def method(self):
print("B's method")
class C(A):
def method(self):
print("C's method")
class D(B, C):
pass
d = D()
d.method()
A's method
B's method
C's method
Error
Which of the following best describes multiple-level inheritance?
A class inherits from multiple parent classes.
A class inherits from a parent class, which in turn inherits from another parent class.
A class has multiple methods with the same name.
A class has multiple attributes with the same name.
What is Instantiation in terms of OOP terminology?
Deleting an instance of class
Modifying an instance of class
Copying an instance of class
Creating an instance of class
