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.

What is delattr(obj,name) used for?

  1. A. To print deleted attribute
  2. B. To delete an attribute
  3. C. To check if an attribute is deleted or not
  4. D. To set an attribute
Report Error

What will be the output of the following Python code? class test: def __init__(self,a="Hello World"): self.a=a def display(self): print(self.a) obj=test() obj.display()

  1. A. The program has an error because constructor can't have default arguments
  2. B. Nothing is displayed
  3. C. "Hello World" is displayed
  4. D. The program has an error display function doesn't have parameters
Report Error

What is the output of the following code: class MyClass: count = 0 def init(self): MyClass.count += 1 obj1 = MyClass() obj2 = MyClass() print(obj1.count)

  1. A. 2
  2. B. 1
  3. C. An error will occur
  4. D. 0
Report Error

What is the purpose of the @property decorator in Python classes?

  1. A. It allows you to define a method that can be accessed like an attribute
  2. B. It defines a property
  3. C. It defines a method
  4. D. It defines a constructor
Report Error

What will be the output of the following Python code? >>> class demo(): def __repr__(self): return '__repr__ built-in function called' def __str__(self): return '__str__ built-in function called' >>> s=demo() >>> print(s)

  1. A. Error
  2. B. Nothing is printed
  3. C. __str__ called
  4. D. __repr__ called
Report Error

How can you create a static variable in a Python class?

  1. A. By defining a variable within the class but outside any method
  2. B. By using the static keyword
  3. C. By using the class keyword
  4. D. By using the variable keyword
Report Error

__del__ method is used to destroy instances of a class.

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

How can you check if an object belongs to a specific class in Python?

  1. A. By using the isinstance() function
  2. B. By using the belongsto() method
  3. C. By using the classof() method
  4. D. By using the typeof() function
Report Error

What is the output of the following code: class Parent: x = 10 class Child(Parent): pass obj = Child() print(obj.x)

  1. A. 10
  2. B. An error will occur
  3. C. obj
  4. D. Child
Report Error

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

  1. A. Exception is thrown
  2. B. __main__
  3. C. Nothing is displayed
  4. D. Base class for all students
Report Error

What is hasattr(obj,name) used for?

  1. A. To access the attribute of the object
  2. B. To delete an attribute
  3. C. To check if an attribute exists or not
  4. D. To set an attribute
Report Error

What will be the output of the following Python code? class fruits: def __init__(self, price): self.price = price obj=fruits(50) obj.quantity=10 obj.bags=2 print(obj.quantity+len(obj.__dict__))

  1. A. 12
  2. B. 52
  3. C. 13
  4. D. 60
Report Error