Test Series - python

Test Number 105/108

Q: What is delattr(obj,name) used for?
A. To print deleted attribute
B. To delete an attribute
C. To check if an attribute is deleted or not
D. To set an attribute
Solution: delattr(obj,name) deletes an attribute in a class.
Q: __del__ method is used to destroy instances of a class.
A. True
B. False
C. none
D. ..
Solution: ___del__ method acts as a destructor and is used to destroy objects of classes.
Q: What will be the output of the following Python code?

class stud:
   ‘Base class for all students’
   def __init__(self, roll_no, grade):
      self.roll_no = roll_no
      self.grade = grade
   def display (self):
      print("Roll no : ", self.roll_no,  ", Grade: ", self.grade)
print(student.__doc__)
A. Exception is thrown
B. __main__
C. Nothing is displayed
D. Base class for all students
Solution: ___doc__ built-in class attribute is used to print the class documentation string or none, if undefined.
Q: What does print(Test.__name__) display (assuming Test is the name of the class)?
A. ()
B. Exception is thrown
C. Test
D. __main__
Solution: __name__ built-in class attribute is used to display the class name.
Q:  Which of the following best describes inheritance?
A. Ability of a class to derive members of another class as a part of its own definition
B. Means of bundling instance variables and methods in order to restrict access to certain class members
C. Focuses on variables and passing of variables to functions
D. Allows for implementation of elegant software that is well designed and easily modified
Solution: If the class definition is class B(A): then class B inherits the methods of class A. This is called inheritance.
Q: Which of the following statements is wrong about inheritance?
A. Protected members of a class can be inherited
B. The inheriting class is called a subclass
C. Private members of a class can be inherited and accessed
D. Inheritance is one of the features of OOP
Solution: Any changes made to the private members of the class in the subclass aren’t reflected in the original members.
Q: What will be the output of the following Python code?

class Test:
    def __init__(self):
        self.x = 0
class Derived_Test(Test):
    def __init__(self):
        self.y = 1
def main():
    b = Derived_Test()
    print(b.x,b.y)
main()
A. 0 1
B. 0 0
C. Error because class B inherits A but variable x isn’t inherited
D. Error because when object is created, argument must be passed like Derived_Test(1)
Solution: Since the invoking method, Test.__init__(self), isn’t present in the derived class, variable x can’t be inherited.
Q: What will be the output of the following Python code?

class A():
    def disp(self):
        print("A disp()")
class B(A):
    pass
obj = B()
obj.disp()
A. Invalid syntax for inheritance
B. Error because when object is created, argument must be passed
C. Nothing is printed
D. A disp()
Solution: 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.

You Have Score    /8