Vidyalelo
Python · all questions

Concept of Object Oriented Programs in Python
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

87

Questions

2/5

Page

Pick an option on a question to see the right answer and solution.

What is the use of duck typing?

Select an option to see the answer and solution.

What is the purpose of the constructor (__init__ method) in a class in Python?

Select an option to see the answer and solution.

What will be the output of the following Python code?
>>> class A:
	pass
>>> class B(A):
	pass
>>> obj=B()
>>> isinstance(obj,A)

Select an option to see the answer and solution.

What will be the output of the following Python code?
class Demo:
    def __init__(self):
        self.x = 1
    def change(self):
        self.x = 10
class Demo_derived(Demo):
    def change(self):
        self.x=self.x+1
        return self.x
def main():
    obj = Demo_derived()
    print(obj.change())
 
main()

Select an option to see the answer and solution.

What is the purpose of using abstraction in object-oriented programming?

Select an option to see the answer and solution.

What will be the output of the following Python code?
class A:
    def __str__(self):
        return '1'
class B(A):
    def __init__(self):
        super().__init__()
class C(B):
    def __init__(self):
        super().__init__()
def main():
    obj1 = B()
    obj2 = A()
    obj3 = C()
    print(obj1, obj2,obj3)
main()

Select an option to see the answer and solution.

What will be the output of the following Python code?
class Demo:
    def __init__(self):
        self.a = 1
        self.__b = 1
 
    def display(self):
        return self.__b
obj = Demo()
print(obj.a)

Select an option to see the answer and solution.

A class in which one or more methods are only implemented to raise an exception is called an abstract class.

Select an option to see the answer and solution.

How does inheritance promote code reusability in object-oriented programming?

Select an option to see the answer and solution.

What is the main advantage of using abstraction in object-oriented programming?

Select an option to see the answer and solution.

Which principle of OOP is related to creating new classes by extending existing classes?

Select an option to see the answer and solution.

What will be the output of the following Python code?
class A:
    def test(self):
        print("test of A called")
class B(A):
    def test(self):
        print("test of B called")
        super().test()  
class C(A):
    def test(self):
        print("test of C called")
        super().test()
class D(B,C):
    def test2(self):
        print("test of D called")      
obj=D()
obj.test()

Select an option to see the answer and solution.

Which of the following statements is true?

Select an option to see the answer and solution.

What is the purpose of the __str__ method in Python classes?

Select an option to see the answer and solution.

What will be the output of the following Python code?
class A:
    def one(self):
        return self.two()
 
    def two(self):
        return 'A'
 
class B(A):
    def two(self):
        return 'B'
obj1=A()
obj2=B()
print(obj1.two(),obj2.two())

Select an option to see the answer and solution.

What type of inheritance is illustrated in the following Python code?
class A():
    pass
class B():
    pass
class C(A,B):
    pass

Select an option to see the answer and solution.

What will be the output of the following Python code?
class A:
    def __init__(self):
        self._x = 5       
class B(A):
    def display(self):
        print(self._x)
def main():
    obj = B()
    obj.display()
main()

Select an option to see the answer and solution.

What will be the output of the following Python code?
class objects:
    def __init__(self):
        self.colour = None
        self._shape = "Circle" 
 
    def display(self, s):
        self._shape = s
obj=objects()
print(obj._objects_shape)

Select an option to see the answer and solution.

What will be the output of the following Python code?
class A:
    def __init__(self):
        self.__x = 1
class B(A):
    def display(self):
        print(self.__x)
def main():
    obj = B()
    obj.display()
main()

Select an option to see the answer and solution.

What will be the output of the following Python code?
class A:
    def __init__(self,x):
        self.x = x
    def count(self,x):
        self.x = self.x+1
class B(A):
    def __init__(self, y=0):
        A.__init__(self, 3)
        self.y = y
    def count(self):
        self.y += 1     
def main():
    obj = B()
    obj.count()
    print(obj.x, obj.y)
main()

Select an option to see the answer and solution.