Sail E0 Webinar

MCQs

Total Questions : 10
Question 1. The output of the code shown below is:int('65.43')
  1.    ImportError
  2.    ValueError
  3.    TypeError
  4.    NameError
 Discuss Question
Answer: Option B. -> ValueError


The snippet of code shown above results in a value error. This is because there is an invalid literal for int() with base 10: '65.43'.


Question 2. What is the output of the following piece of code?class A():    def disp(self):        print("A disp()")class B(A):    passobj = B()obj.disp()
  1.    Invalid syntax for inheritance
  2.    Error because when object is created, argument must be passed
  3.    Nothing is printed
  4.    A disp()
 Discuss Question
Answer: Option D. -> A disp()


Class B inherits class A hence the function disp () becomes part of class B's definition. Hence disp() method is properly executed and the line is printed.


Question 3. Which of the following statements is true?
  1.    The __new__() method automatically invokes the __init__ method
  2.    The __init__ method is defined in the object class
  3.    The __eq(other) method is defined in the object class
  4.    The __repr__() method is defined in the object class
 Discuss Question
Answer: Option C. -> The __eq(other) method is defined in the object class


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


Question 4. What is the output of the following piece of code?class Demo:    def __init__(self):        self.x = 1    def change(self):        self.x = 10class Demo_derived(Demo):    def change(self):        self.x=self.x+1        return self.xdef main():    obj = Demo_derived()    print(obj.change()) main()
  1.    11
  2.    2
  3.    1
  4.    An exception is thrown
 Discuss Question
Answer: Option D. -> An exception is thrown


The derived class method change() overrides the base class method.


Question 5. Which function overloads the == operator?
  1.    __eq__()
  2.    __equ__()
  3.    __isequal__()
  4.    none of the mentioned
 Discuss Question
Answer: Option A. -> __eq__()


The other two do not exist.


Question 6. What is setattr() used for?
  1.    To access the attribute of the object
  2.    To set an attribute
  3.    To check if an attribute exists or not
  4.    To delete an attribute
 Discuss Question
Answer: Option B. -> To set an attribute


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


Question 7. Which of these is a private data field?def Demo:def __init__(self):    __a = 1    self.__b = 1    self.__c__ = 1    __d__= 1
  1.    __a
  2.    __b
  3.    __c__
  4.    __d__
 Discuss Question
Answer: Option B. -> __b


Variables such as self.__b are private members of the class.


Question 8. What is the output of the code shown below?def f(x):    yield x+1    print("test")    yield x+2g=f(9)
  1.    Error
  2.    test
  3.    test1012
  4.    No output
 Discuss Question
Answer: Option D. -> No output


The code shown above will not yield any output. This is because when we try to yield 9, and there is no next(g), the iteration stops. Hence there is no output.


Question 9. When will the else part of try-except-else be executed?
  1.    always
  2.    when an exception occurs
  3.    when no exception occurs
  4.    when an exception occurs in to except block
 Discuss Question
Answer: Option C. -> when no exception occurs


The else part is executed when no exception occurs.


Question 10. What are the methods which begin and end with two underscore characters called?
  1.    Special methods
  2.    In-built methods
  3.    User-defined methods
  4.    Additional methods
 Discuss Question
Answer: Option A. -> Special methods


Special methods like __init__ begin and end with two underscore characters.


Latest Videos

Latest Test Papers