Python quiz questions

Python interview questions

  • 1.

    What Will Be The Output Of The Following Code Snippet?

    class A:
        def __str__(self):
            return "A"
     
    class B(A):
        def __str__(self):
            return "B"
     
    class C(B):
        def __str__(self):
            return "C"
     
    def main():
        b = B()
        a = A()
        c = C()
        print(c, b, a)
     
    main()

     

    1. A C B

    2. A B C

    3. C B A

    4. B B B

    Answer
  • 2.

    What Will Be The Output Of The Following Code Snippet?

    class A:
        def __init__(self):
            self.x = 1
     
        def func(self):
            self.x = 10
     
    class B(A):
        def func(self):
            self.x += 1
            return self.x
     
    def main():
        b = B()
        print(b.func())
     
    main()

     

    1. 1

    2. 2

    3. 10

    4. x is not accessible from the object of classB.

    Answer
  • 3.

    What Will Be The Output Of The Following Code Snippet?

    class A:
        def __init__(self, num):
            self.x = num
     
    class B(A):
        def __init__(self, num):
            self.y = num
     
    obj = B(11)
    print ("%d %d" % (obj.x, obj.y))

     

    1. None None

    2. None 11

    3. 11 None

    4. 11 11

    5. AttributeError: ‘B’ object has no attribute ‘x’

    Answer
  • 4.

    What Will Be The Output Of The Following Code Snippet?

    class A:
        def __new__(self):
            self.__init__(self)
            print("A's __new__() invoked")
     
        def __init__(self):
            print("A's __init__() invoked")
     
    class B(A):
        def __new__(self):
            print("B's __new__() invoked")
     
        def __init__(self):
            print("B's __init__() invoked")
     
    def main():
        b = B()
        a = A()
     
    main()

     

    1. B’s __new__() invoked A’s __init__() invoked 

    2. B’s __new__() invoked A’s __new__() invoked 

    3. B’s __new__() invoked A’s __init__() invoked A’s __new__() invoked 

    4. A’s __init__() invoked A’s __new__() invoked

    Answer
  • 5.

    What Will Be The Output Of The Following Code Snippet?

    class A:
        def __init__(self, x = 0):
            self.x = x
     
        def func1(self):
            self.x += 1
     
    class B(A):
        def __init__(self, y = 0):
           A.__init__(self, 3)
            self.y = y
     
        def func1(self):
            self.y += 1
     
    def main():
        b = B()
        b.func1()
        print(b.x, b.y)
     
    main()

     

    1. 2 0

    2. 3 1

    3. 4 0

    4. 3 0

    5. 4 1

    Answer
  • 6.

    What Will Be The Output Of The Following Code Snippet?

    class A:
         def __init__(self):
             self.__x = 1
             self.y = 10
     
         def print(self):
             print(self.__x, self.y)
     
    class B(A):
         def __init__(self):
             super().__init__()
             self.__x = 2
             self.y = 20
             
    c = B()
    c.print()

     

    1. 1 10

    2. 1 20

    3. 2 10

    4. 2 20

    Answer
  • 7.

    What Will Be The Output Of The Following Code Snippet?

    class A:
        def __init__(self, x = 1):
            self.x = x
     
    class B(A):
        def __init__(self, y = 2):
            super().__init__()
            self.y = y
     
    def main():
        b = B()
        print(b.x, b.y)
     
    main()

     

    1. 0 0

    2. 0 1

    3. 1 2

    4. 0 2

    5. 2 1

    Answer
  • 8.

    What Code Can We Put At The Third Line Of The Definition Of Class B To Invoke Its Superclass’s Constructor?

    class A:
        def __init__(self, i = 1):
            self.i = i
     
    class B(A):
        def __init__(self, j = 2):
            ___________________
            self.j = j
     
    def main():
        b = B()
        print(b.i, b.j)
     
    main()

     

    1. super().__init__(self) 

    2. super().__init__() 

    3. A.__init__() 

    4. A.__init__(self)

    Answer
  • 9.

    What Will Be The Output Of The Following Code Snippet?

    def main():
        myCounter = Counter()
        num = 0
     
        for i in range(0, 100):
            increment(myCounter, num)
     
        print("myCounter.counter =", myCounter.counter, ", number of times =", num)
     
    def increment(c, num):
        c.counter += 1
        num += 1
     
    class Counter:
        def __init__(self):
            self.counter = 0
        
    main()

     

    1. counter is 101 , number of times is 0

    2. counter is 100, number of times is 0

    3. counter is 100, number of times is 100

    4. counter is 101, number of times is 101

    Answer
  • 10.

    What Will Be The Output Of The Following Code Snippet?

    class Test:
         def __init__(self):
             self.x = 1
             self.__y = 1
     
         def getY(self):
             return self.__y
     
    val= Test()
    val.__y = 45
    print(val.getY())

     

    1. The program has an error because y is private and should not access it from outside the class.

    2. The program has an error because you cannot name a variable using __y.

    3. The code runs fine and prints 1.

    4. The code executes successfully and prints 45.

    Answer
  • 11.

    Which Of The Following Is A Private Data Field In The Given Code Snippet?

    class Test:
         def __init__(self):
         __a = 1
         self.__b = 1
         self.__c__ = 1
         __d__ = 1

     

    1. __a

    2. __b

    3. __c__

    4. __d__

    Answer
  • 12.

    What Will Be The Output Of The Following Code Snippet?

    class Test:
         def __init__(self):
             self.x = 1
             self.__y = 1
     
         def getY(self):
             return self.__y
     
     val = Test()
     val.x = 45
     print(val.x)

     

    1. The program has an error because x is private and should not access it from outside the class.

    2. The program has an error because you cannot name a variable using __y.

    3. The program runs fine and prints 1.

    4. The program runs fine and prints 45.

    Answer
  • 13.

    What Will Be The Output Of The Following Code Snippet?

    class Test:
        def __init__(self):
            self.x = 1
            self.__y = 1
     
        def getY(self):
            return self.__y
     
    val = Test()
    print(val.__y)

     

    1. The program has an error because y is private and should not access it from outside the class.

    2. The program has an error because you cannot name a variable using __y.

    3. The program runs fine and prints 1.

    4. The program runs fine and prints nothing.

    Answer
  • 14.

    What Will Be The Output Of The Following Code Snippet?

    class Test:
        def __init__(self):
            self.x = 1
            self.__y = 1
     
        def getY(self):
            return self.__y
     
    val = Test()
    print(val.x)

     

    1. The program has an error because x is private and cannot be accessed outside of the class.

    2. The program has an error because you cannot name a variable using .

    3. The program runs fine and prints 1.

    4. The program runs fine and prints nothing.

    Answer
  • 15.

    What Will Be The Output Of The Following Code?

    class Test:
         def __init__(self, s = "Welcome"):
             self.s = s
     
         def print(self):
             print(self.s)
     
    msg = Test()
    msg.print()

     

    1. The program has an error because the constructor is not present in class Test. 

    2. The above code produces an error because the definition of print(s) does not contain.

    3. It executes successfully but prints nothing.

    4. The program has an error because of the constructor call is made without an argument. 

    5. The program executes successfully and prints Welcome.

    Answer
  • 16.

    What Will Be The Output Of The Following Code?

    class Test:
         def __init__(self, s):
             self.s = s
     
         def print(self):
             print(self.s)
     
    msg = Test()
    msg.print()

     

    1. The program has an error because class Test does not have a constructor. 

    2. The above code produces an error because the definition of print(s) does not include . 

    3. It executes successfully but prints nothing. 

    4. The program has an error because of the constructor call is made without an argument.

    Answer
  • 17.

    What Will Be The Output Of The Following Code?

    class Test:
         def __init__(self, s):
             self.s = s
     
         def print(self):
             print(s)
     
    a = Test("Python Class")
    a.print()

     

    1. The program gives an error because there is no constructor for class Test. 

    2. Signature for the print method is incorrect, so an error is thrown. 

    3. The correct output is.

    4. The above code will execute correctly on changing print(s) to print(self.s).

    Answer
  • 18.

    What Relationship Is Appropriate For Fruit And Papaya?

    1. association

    2. composition

    3. inheritance

    4. All of the above

    Answer
  • 19.

    What Relationship Is Best Suited For House And Door?

    1. association

    2. composition

    3. inheritance

    4. All of the above

    Answer
  • 20.

    What Relationship Is Best Suited For Employee And Person?

    1. association

    2. composition

    3. inheritance

    4. None of the above

    Answer

© 2017 QuizBucket.org