Home / CSE MCQs / Python MCQs :: Python Classes, Objects, Inheritance, Exception Handling

CSE MCQs :: Python MCQs

  1. What is setattr() used for?
  2. 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

  3. What are the methods which begin and end with two underscore characters called?
  4. A.
    Special methods
    B.
    In-built methods
    C.
    User-defined methods
    D.
    Additional methods

  5. Which of these is a private data field?

    def Demo:
    def __init__(self):
        __a = 1
        self.__b = 1
        self.__c__ = 1
        __d__= 1
  6. A.
    __a
    B.
    __b
    C.
    __c__
    D.
    __d__

  7. When will the else part of try-except-else be executed?
  8. A.
    always
    B.
    when an exception occurs
    C.
    when no exception occurs
    D.
    when an exception occurs in to except block

  9. What is the output of the code shown below?

    def f(x):
        yield x+1
        print("test")
        yield x+2
    g=f(9)
  10. A.
    Error
    B.
    test
    C.

    test

    10

    12

    D.
    No output

  11. The output of the code shown below is:

    int('65.43')
  12. A.
    ImportError
    B.
    ValueError
    C.
    TypeError
    D.
    NameError

  13. What is the output of the following piece of code?

    class A():
        def disp(self):
            print("A disp()")
    class B(A):
        pass
    obj = B()
    obj.disp()
  14. A.
    Invalid syntax for inheritance
    B.
    Error because when object is created, argument must be passed
    C.
    Nothing is printed
    D.
    A disp()

  15. Which of the following statements is true?
  16. 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

  17. Which function overloads the == operator?
  18. A.
    __eq__()
    B.
    __equ__()
    C.
    __isequal__()
    D.
    none of the mentioned

  19. What is the output of the following piece of 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()
  20. A.
    11
    B.
    2
    C.
    1
    D.
    An exception is thrown