Breaking News

python quiz-3 interview questions on advanced topics

 1. What type of inheritance is illustrated in the following Python code?


class A():

    pass

class B(A):

    pass

class C(B):

    pass

a) Multi-level inheritance

b) Multiple inheritance

c) Hierarchical inheritance

d) Single-level inheritance

View Answer


Explanation: In multi-level inheritance, a subclass derives from another class which itself is derived from another class.




2. What does single-level inheritance mean?

a) A subclass derives from a class which in turn derives from another class

b) A single superclass inherits from multiple subclasses

c) A single subclass derives from a single superclass

d) Multiple base classes inherit a single derived class




Explanation: In single-level inheritance, there is a single subclass which inherits from a single superclass. So the class definition of the subclass will be: class B(A): where A is the superclass.


3. What will be the output of the following Python code?


Get Free Certificate of Merit in Python Programming Now!

class A:

     def __init__(self):

         self.__i = 1

         self.j = 5

 

     def display(self):

         print(self.__i, self.j)

class B(A):

     def __init__(self):

         super().__init__()

         self.__i = 2

         self.j = 7  

c = B()

c.display()

a) 2 7

b) 1 5

c) 1 7

d) 2 5

View Answer


Answer: c

Explanation: Any change made in variable i isn’t reflected as it is the private member of the superclass.



4. Which of the following statements isn’t true?

a) A non-private method in a superclass can be overridden

b) A derived class is a subset of superclass

c) The value of a private variable in the superclass can be changed in the subclass

d) When invoking the constructor from a subclass, the constructor of superclass is automatically invoked

View Answer


Answer: c

Explanation: If the value of a private variable in a superclass is changed in the subclass, the change isn’t reflected.



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

a) 3 0

b) 3 1

c) 0 1

d) An exception in thrown

View Answer


Answer: b

Explanation: Initially x=3 and y=0. When obj.count() is called, y=1.



6. What will be the output of the following Python code?


>>> class A:

pass

>>> class B(A):

pass

>>> obj=B()

>>> isinstance(obj,A)

a) True

b) False

c) Wrong syntax for isinstance() method

d) Invalid method for classes

View Answer


Answer: a

Explanation: isinstance(obj,class) returns True if obj is an object class.


7. Which of the following statements is true?

a) The __new__() method automatically invokes the __init__ method

b) The __init__ method is defined in the object class

c) The __eq(other) method is defined in the object class

d) The __repr__() method is defined in the object class

View Answer


Answer: c

Explanation: The __eq(other) method is called if any comparison takes place and it is defined in the object class.



8. Method issubclass() checks if a class is a subclass of another class.

a) True

b) False

View Answer


Answer: a

Explanation: Method issubclass() returns True if a class is a subclass of another class and False otherwise.





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

a) 1

b) 0

c) Error, invalid syntax for object declaration

d) Error, private class member can’t be accessed in a subclass

View Answer


Answer: d

Explanation: Private class members in the superclass can’t be accessed in the subclass.

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

a) Error, invalid syntax for object declaration

b) Nothing is printed

c) 5

d) Error, private class member can’t be accessed in a subclass

View Answer


Answer: c

Explanation: The class member x is protected, not private and hence can be accessed by subclasses.




11. What will be the output of the following Python code?


class A:

    def __init__(self,x=3):

        self._x = x        

class B(A):

    def __init__(self):

        super().__init__(5)

    def display(self):

        print(self._x)

def main():

    obj = B()

    obj.display()

 

main()

a) 5

b) Error, class member x has two values

c) 3

d) Error, protected class member can’t be accessed in a subclass

View Answer


Answer: a

Explanation: The super() method re-assigns the variable x with value 5. Hence 5 is printed.



12. What will be the output of the following Python code?


class A:

    def test1(self):

        print(" test of A called ")

class B(A):

    def test(self):

        print(" test of B called ")

class C(A):

    def test(self):

        print(" test of C called ")

class D(B,C):

    def test2(self):

        print(" test of D called ")        

obj=D()

obj.test()

a)


test of B called

test of C called

b)


test of C called

test of B called

c) test of B called

d) Error, both the classes from which D derives has same method test()

View Answer


Answer: c

Explanation: Execute in Python shell to verify. If class D(B,C): is switched is class D(C,B): test of C is called.



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

a)


test of B called

test of C called

test of A called

b)


test of C called

test of B called

c)


test of B called

test of C called

d) Error, all the three classes from which D derives has same method test()

View Answer


Answer: a

Explanation: Since the invoking method, super().test() is called in the subclasses, all the three methods of test() in three different classes is called.


1. _____ represents an entity in the real world with its identity and behavior.

a) A method

b) An object

c) A class

d) An operator

View Answer


Answer: b

Explanation: An object represents an entity in the real world that can be distinctly identified. A class may define an object.



2. _____ is used to create an object.

a) class

b) constructor

c) User-defined functions

d) In-built functions

View Answer


Answer: b

Explanation: The values assigned by the constructor to the class members is used to create the object.


3. What will be the output of the following Python code?


Subscribe Now: Python Newsletter | Important Subjects Newsletters

advertisement


class test:

     def __init__(self,a="Hello World"):

         self.a=a

 

     def display(self):

         print(self.a)

obj=test()

obj.display()

a) The program has an error because constructor can’t have default arguments

b) Nothing is displayed

c) “Hello World” is displayed

d) The program has an error display function doesn’t have parameters

View Answer


Answer: c

Explanation: The program has no error. “Hello World” is displayed. Execute in python shell to verify.

Get Free Certificate of Merit in Python Programming Now!

4. What is setattr() used for?

a) To access the attribute of the object

b) To set an attribute

c) To check if an attribute exists or not

d) To delete an attribute

View Answer


Answer: b

Explanation: setattr(obj,name,value) is used to set an attribute. If attribute doesn’t exist, then it would be created.


5. What is getattr() used for?

a) To access the attribute of the object

b) To delete an attribute

c) To check if an attribute exists or not

d) To set an attribute

View Answer


Answer: a

Explanation: getattr(obj,name) is used to get the attribute of an object.

6. What will be the output of the following Python code?


class change:

    def __init__(self, x, y, z):

        self.a = x + y + z

 

x = change(1,2,3)

y = getattr(x, 'a')

setattr(x, 'a', y+1)

print(x.a)

a) 6

b) 7

c) Error

d) 0

View Answer


Answer: b

Explanation: First, a=1+2+3=6. Then, after setattr() is invoked, x.a=6+1=7.




7. What will be the output of the following Python code?


 class test:

     def __init__(self,a):

         self.a=a

 

     def display(self):

         print(self.a)

obj=test()

obj.display()

a) Runs normally, doesn’t display anything

b) Displays 0, which is the automatic default value

c) Error as one argument is required while creating the object

d) Error as display function requires additional argument

View Answer


Answer: c

Explanation: Since, the __init__ special method has another argument a other than self, during object creation, one argument is required. For example: obj=test(“Hello”)

8. Is the following Python code correct?


>>> class A:

def __init__(self,b):

self.b=b

def display(self):

print(self.b)

>>> obj=A("Hello")

>>> del obj

a) True

b) False

View Answer


Answer: a

Explanation: It is possible to delete an object of the class. On further typing obj in the python shell, it throws an error because the defined object has now been deleted.

9. What will be the output of the following Python code?


class test:

    def __init__(self):

        self.variable = 'Old'

        self.Change(self.variable)

    def Change(self, var):

        var = 'New'

obj=test()

print(obj.variable)

a) Error because function change can’t be called in the __init__ function

b) ‘New’ is printed

c) ‘Old’ is printed

d) Nothing is printed

View Answer


Answer: c

Explanation: This is because strings are immutable. Hence any change made isn’t reflected in the original string.

10. 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

View Answer


Answer: d

Explanation: Instantiation refers to creating an object/instance for a class.



11. What will be the output of the following Python code?


class fruits:

    def __init__(self, price):

        self.price = price

obj=fruits(50)

 

obj.quantity=10

obj.bags=2

 

print(obj.quantity+len(obj.__dict__))

a) 12

b) 52

c) 13

d) 60

View Answer


Answer: c

Explanation: In the above code, obj.quantity has been initialised to 10. There are a total of three items in the dictionary, price, quantity and bags. Hence, len(obj.__dict__) is 3.




12. What will be the output of the following Python code?


 class Demo:

    def __init__(self):

        pass

 

    def test(self):

        print(__name__)

 

obj = Demo()

obj.test()

a) Exception is thrown

b) __main__

c) Demo

d) test

View Answer


Answer: b

Explanation: Since the above code is being run not as a result of an import from another module, the variable will have value “__main__”.






No comments