Advertisement

Python Quiz - CLASSES AND OBJECTS

 



1.    All classes have a function called?


A.    __init__

B.    __init__()

C.    init

D.    init()


Ans :    __init__()                                     


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


Ans :    Creating an instance of class          


3.     _____ is used to create an object.


A.    class

B.    constructor

C.    User-defined functions

D.    In-built functions


Ans :    constructor                                     


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


Ans :    To set an attribute                    


5.    To create a class, use the keyword?


A.    new

B.    except 

C.    class

D.    object


Ans :    Class                                       


6.    You can delete properties on objects by using the ______ keyword.


A.    delete

B.    dedl

C.    del

D.    drop


Ans :    del                                        


7.    A variable that is defined inside a method and belongs only to the current instance of a class is known as?


A.    Inheritance 

B.    Instance variable

C.    Function overloading

D.    Instantiation 


Ans :    Instance variable                  


8.     _____ represents an entity in the real world with its identity and behaviour.


A.    A method

B.    An object

C.    A class

D.    An operator


Ans :    An object                            


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


Ans :    To access the attribute of the object    


10.   The assignment of more than one function to a particular operator is _______


A.    Operator over-assignment

B.    Operator overriding

C.    Operator overloading

D.    Operator instance


Ans :    Operator overloading             


11.   What are the methods which begin and end with two underscore characters called?


A.    Special methods

B.    In-built methods

C.    User-defined methods

D.    Additional methods


Ans :    Special methods                    


12.   What is hasattr(obj,name) 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


Ans :    To check if an attribute exists or not    


13.   What does print(Test.__name__) display (assuming Test is the name of the class)?


A.    ()

B.    Exception is thrown

C.    Test

D.    __main__


Ans :    Test                                    


14.    __del__ method is used to destroy instances of a class.


A.    True

B.    False


Ans :    True                                  


15.   Special methods need to be explicitly called during object creation.


A.    True

B.    False


Ans :    False                                


16.    Which of the following is not a class method?


A.    Non-static

B.    Static

C.    Bounded

D.    Unbounded


Ans :    Non-static                        


17.    The class has a documentation string, which can be accessed via?


A.    ClassName

B.    ClassName __doc__

C.    __doc__

D.    ClassName.__doc__


Ans :    ClassName.__doc__    


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


Ans :    Private members of a class can be inherited and accessed    


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


Ans :     Error as one argument is required while creating the object  


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


Ans :    __main__                              

Post a Comment

1 Comments