Commerce

Classes And Objects In Python MCQs

Practice Classes And Objects In Python MCQs for competitive exams.

Classes And Objects In Python MCQs

Practice questions from this topic.

Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?

  1. A. __add__(), __str__()
  2. B. __str__(), __add__()
  3. C. __sum__(), __str__()
  4. D. __str__(), __sum__()
Report Error

Is the following Python code correct? >>> class A: def __init__(self,b): self.b=b def display(self): print(self.b) >>> obj=A("Hello") >>> del obj

  1. A. True
  2. B. False
Report Error

What will be the output of the following Python code? class test: def __init__(self): self.variable = 'Old' self.Change(self.variable) def Change(self, var): var = 'New' obj=test() print(obj.variable)

  1. A. Error because function change can't be called in the __init__ function
  2. B. 'New' is printed
  3. C. 'Old' is printed
  4. D. Nothing is printed
Report Error

Which of the following Python code creates an empty class?

  1. A. class A: return
  2. B. class A: pass
  3. C. class A:
  4. D. It is not possible to create an empty class
Report Error

What will be the output of the following Python code? class change: def __init__(self, x, y, z): self.a = x + y + z x = change(1,2,3) y = getattr(x, 'a') setattr(x, 'a', y+1) print(x.a)

  1. A. 6
  2. B. 7
  3. C. Error
  4. D. 0
Report Error

What is the purpose of the super() function in Python classes?

  1. A. It is used to call a method from the parent class
  2. B. It is used to create an object
  3. C. It is used to delete an object
  4. D. It is used to inherit a class
Report Error

Which of the following is not a class method?

  1. A. Non-static
  2. B. Static
  3. C. Bounded
  4. D. Unbounded
Report Error

Special methods need to be explicitly called during object creation.

  1. A. True
  2. B. False
Report Error

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()

  1. A. Exception is thrown
  2. B. __main__
  3. C. Demo
  4. D. test
Report Error

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()

  1. A. Runs normally, doesn't display anything
  2. B. Displays 0, which is the automatic default value
  3. C. Error as one argument is required while creating the object
  4. D. Error as display function requires additional argument
Report Error

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()

  1. A. Exception is thrown
  2. B. Count.test=25 k=25
  3. C. Count.test=25 k=0
  4. D. Count.test=0 k=0
Report Error

How can you create a private attribute in a Python class?

  1. A. By prefixing the attribute name with an underscore (_)
  2. B. By using the private keyword
  3. C. By using the hidden keyword
  4. D. By using the secure keyword
Report Error