Test Series - python

Test Number 104/108

Q: 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
Solution: The assignment of more than one function to a particular operator is called as operator overloading.
Q: Which of the following is not a class method?
A. Non-static
B. Static
C. Bounded
D. Unbounded
Solution: The three different class methods in Python are static, bounded and unbounded methods.
Q: What will be the output of the following Python code?

def add(c,k):
    c.test=c.test+1
    k=k+1
class A:
    def __init__(self):
        self.test = 0
def main():
    Count=A()
    k=0
 
    for i in range(0,25):
        add(Count,k)
    print("Count.test=", Count.test)
    print("k =", k)
main()
A. Exception is thrown
B. Count.test=25 k=25
C. Count.test=25 k=0
D. Count.test=0 k=0
Solution: The program has no error. Here, test is a member of the class while k isn’t. Hence test keeps getting incremented 25 time while k remains 0.
Q: Which of the following Python code creates an empty class?
A. class A: return
B. class A: pass
C. class A:
D. It is not possible to create an empty class
Solution: Execute in python shell to verify.
Q: Is the following Python code valid?

class B(object):
  def first(self):
    print("First method called")
  def second():
    print("Second method called")
ob = B()
B.first(ob)
A. It isn’t as the object declaration isn’t right
B. It isn’t as there isn’t any __init__ method for initializing class members
C. Yes, this method of calling is called unbounded method call
D. Yes, this method of calling is called bounded method call
Solution: The method may be created in the method demonstrated in the code as well and this is called as the unbounded method call. Calling the method using obj.one() is the bounded method call.
Q: 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
Solution: Special methods like __init__ begin and end with two underscore characters.
Q: Special methods need to be explicitly called during object creation.
A. True
B. False
C. none
D. ..
Solution: Special methods are automatically called during object creation.
Q: 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
Solution: hasattr(obj,name) checks if an attribute exists or not and returns True or False.

You Have Score    /8