Q. 1

What is the output of the following code ?

class SuperClass:
    def printme(self):
        print('Welcome to python class')

    def referer(self):
        self.do_action()


class SubClass(SuperClass):
    pass

p = SubClass()
p.printme()

Options:

Explanation

Correct answer is : A

SubClass is inheriting the SuperClass.
So all the methods can be accessible by SubClass instance "p"

Discuss It

Q. 2

What is the output of the following code ?

class SuperClass:
    def printme(self):
        print('In SUPER class method')

class SubClass(SuperClass):
    def printme(self):
        print('In SUB class method')

p = SubClass()
p.printme()

Options:

Explanation

Correct answer is : B

SubClass printme method will override the SuperClass printme method

Discuss It

Q. 3

What is the output of the following code ?

class SuperClass:
    def printme(self):
        print('In SUPER class method')

class SubClass(SuperClass):
    def printme(self):
        print('In SUB class method')
        SuperClass.printme(self)

p = SubClass()
p.printme()

Options:

Explanation

Correct answer is : C

When sub class and super class both have a method with same name and the method is invoked using sub class instance, then the sub class method will be executed.
in printme of SubClass , it will print 'In SUB class method'.
Also in the same method we are calling the SuperClass 's printme method.
So 'In SUPER class method' also be printed. The output will be

In SUB class method
In SUPER class method

Discuss It

Q. 4

What is the output of the following code ?
 

class A:
    def __init__(self):
        self.__a = 1
        self.b = 5

    def display(self):
        print(self.__a, self.b)


class B(A):
    def __init__(self):
        super().__init__()
        self.__a = 2
        self.b = 7


p = B()
p.display()

 

Options:

Explanation

Correct answer is : B

__a is a private variable to class A . So self.__a = 2  in class B will nvever change it .

So display() method which is available in A class will only access __a of A which is 1 

As self.b = 7 changes the value of b , the answer will be 1 7 

Discuss It

Q. 5

What is the output of the following code ?

class A:
    def test(self):
        print("test of A is called")
class B(A):
    def test(self):
        print("test of B is called")
        super().test()
class C(A):
    def test(self):
        print("test of C is called")
        super().test()
class D(B, C):
    def test3(self):
        print("test of D is called")

p = D()
p.test()

 

Options:

Explanation

Correct answer is : D

As in B , test() method is called using super() i.e. super().test() , then it call all the test() method of class D in MRO 

so it will be invoked as B -> C -> A.

Remember super() does not invoke super class of B , but of D 

Discuss It

Q. 6

What is the output of the following code ? 

class A:
    def __init__(self, x=47):
        self._x = x


class B(A):
    def __init__(self):
        super().__init__(98)

    def printme(self):
        print(self._x)


obj = B()
obj.printme()

 

Options:

Explanation

Correct answer is : A

super().__init__(98) sets the value of _x to 98 so printme() will print 98 . Note _x is not a private variable

Discuss It

Q. 7

What is the output of the following code ? 

class A:
    def __init__(self, x=47):
        self.__x = x


class B(A):
    def __init__(self):
        super().__init__(98)

    def printme(self):
        print(self.__x)


obj = B()
obj.printme()

 

Options:

Explanation

Correct answer is : D

Subclasses cannot access the private memebers of superclasses.. the __x actually converted to _B__x.

This will print 

Traceback (most recent call last):
  File "C:/pythoneasy/test1.py", line 15, in <module>
    obj.printme()
  File "C:/pythoneasy/test1.py", line 11, in printme
    print(self.__x)
AttributeError: 'B' object has no attribute '_B__x'

 

Discuss It

Q. 8

What is the output of the following code ? 

class A:
    def foo(self):
        return self.bar()

    def bar(self):
        return 'A'


class B(A):
    def bar(self):
        return 'B'


obj1 = A()
obj2 = B()
print(obj2.bar(), obj2.foo())

 

Options:

Explanation

Correct answer is : A

obj2.bar() invokes bar() on class B so it prints B

For obj2.foo(), it invokes class A foo() method which internally returns bar(). for this call bar() of B will be overridden 

Discuss It

Q. 9

What is the output of the following code ? 

class A:
    def __init__(self, x=6):
        self.k = x


class B(A):
    def __init__(self, y=9):
        super().__init__()
        self.l = y


obj = B()
print(obj.k, obj.l)

 

Options:

Explanation

Correct answer is : C

When obj is created super().__init__() sets "k" attribute to value 6.

self.l = y sets "k" attribute value to 9 ans y's default value is 9

So it prints 6 9

 

Discuss It

Q. 10

What is the output of the following code ? 

class A:
    pass

class B(A):
    pass

class C(B):
    pass


obj = C()
print(isinstance(obj, A))

 

Options:

Explanation

Correct answer is : A

isinstance(obj, A) returns True as obj is an instance of A , due to multilevel inheritance

Discuss It
Submit Your Answers

Interview Questions

on
Classes

Tutorial

on
Classes

Programs

on
Classes
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description