Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

OOPs in Python

Total questions: 20

Worksheet time: 30mins

Name
Class
Date
1.

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)

a)

1 2

b)

Error

c)

2 1

d)

None

2.

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()

a)

Animal speaks

b)

Dog barks

c)

Error

d)

None

3.

Which of the following is true about multiple inheritance in Python?

a)

Python does not support multiple inheritance

b)

A class can inherit from only two parent classes

c)

A class can inherit from multiple parent classes

d)

Multiple inheritance is only possible with abstract classes

4.

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()

a)

Method of class A

b)

Method of class B

c)

Method of class A

   Method of class B

d)

Error

5.

Which of the following is used to implement abstraction in Python?

a)

@abstractmethod decorator

b)

abstract keyword

c)

interface keyword

d)

virtual keyword

6.

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)

a)

2 1 1

b)

2 2 2

c)

2 1 2

d)

Error

7.

Which of the following is true about the `__init__` method in Python?

a)

It must return a value

b)

It is called automatically when a class is instantiated

c)

It can only have one parameter

d)

It is optional in all classes

8.

What is the output of the following code?

class A:

    def str(self):

        return "Class A"

class B(A):

    pass

b = B()

print(b)

a)

Class A

b)

Class B

c)

<__main__.B object at ...>

d)

Error

9.

Which of the following is an example of polymorphism in Python?

a)

Method overloading

b)

Method overriding

c)

Multiple inheritance

d)

All of the above

10.

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)

A's method

   C's method

b)

B's method

   C's method

c)

C's method

d)

A's method

   B's method

   C's method

11.

Which of the following statements is true about encapsulation in Python?

a)

Encapsulation is achieved by using private and protected access modifiers.

b)

Encapsulation is achieved by using public access modifiers only.

c)

Encapsulation is not supported in Python.

d)

Encapsulation is achieved by using the `@abstractmethod` decorator.

12.

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())

a)

10

b)

20

c)

Error

d)

None

13.

Which of the following is an example of polymorphism in Python?

a)

A class inheriting from multiple parent classes.

b)

A method in a child class overriding a method in the parent class.

c)

Using the `@staticmethod` decorator.

d)

Using the `@classmethod` decorator.

14.

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)

A's method

b)

B's method

c)

C's method

d)

Error

15.

In multiple-level inheritance, which method is called first if a method is not found in the child class?

a)

The method in the parent class.

b)

The method in the grandparent class.

c)

The method in the sibling class.

d)

The method in the child class.

16.

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)

a)

1 2 3

b)

3 2 1

c)

1 3 2

d)

Error

17.

Which of the following is true about private attributes in Python?

a)

They can be accessed directly from outside the class.

b)

They can be accessed using the `self` keyword.

c)

They can be accessed using the `super` keyword.

d)

They cannot be accessed or modified.

18.

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)

A's method

b)

B's method

c)

C's method

d)

Error

19.

Which of the following best describes multiple-level inheritance?

a)

A class inherits from multiple parent classes.

b)

A class inherits from a parent class, which in turn inherits from another parent class.

c)

A class has multiple methods with the same name.

d)

A class has multiple attributes with the same name.

20.

What is Instantiation in terms of OOP terminology?

a)

Deleting an instance of class

b)

Modifying an instance of class

c)

Copying an instance of class

d)

Creating an instance of class