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 the output of the following code: class Parent: def init(self): print('Parent constructor') class Child(Parent): def init(self): print('Child constructor') obj = Child()

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

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

  1. A. It defines a method that belongs to the class and not the instance
  2. B. It defines a static method
  3. C. It defines a constructor
  4. D. It defines a destructor
Report Error

Which of the following Python code will print True? a = foo(2) b = foo(3) print(a < b)

  1. A. class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x < other.x: return False else: return True
  2. B. class foo: def __init__(self, x): self.x = x def __less__(self, other): if self.x > other.x: return False else: return True
  3. C. class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x < other.x: return True else: return False
  4. D. class foo: def __init__(self, x): self.x = x def __less__(self, other): if self.x < other.x: return False else: return True
Report Error

What is a constructor in Python classes?

  1. A. A special method that is automatically called when an object is created
  2. B. A built-in function
  3. C. A method that creates an object
  4. D. A method that destroys an object
Report Error

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

  1. A. ()
  2. B. Exception is thrown
  3. C. Test
  4. D. __main__
Report Error

What will be the output of the following Python code? class stud: 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) stud1 = stud(34, 'S') stud1.age=7 print(hasattr(stud1, 'age'))

  1. A. Error as age isn't defined
  2. B. True
  3. C. False
  4. D. 7
Report Error

Which function overloads the == operator?

  1. A. __eq__()
  2. B. __equ__()
  3. C. __isequal__()
  4. D. none of the mentioned
Report Error

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)

  1. A. It isn't as the object declaration isn't right
  2. B. It isn't as there isn't any __init__ method for initializing class members
  3. C. Yes, this method of calling is called unbounded method call
  4. D. Yes, this method of calling is called bounded method call
Report Error

What is the output of the following code: class Parent: def show(self): print('Parent method') class Child(Parent): pass obj = Child() obj.show()

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

What is the output of the following code: class MyClass: x = 5 obj = MyClass() print(obj.x)

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

What is getattr() 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 is setattr() used for?

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