CommerceClasses 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()
- A. Child constructor
- B. Parent constructor
- C. An error will occur
- D. Child Parent
Correct Answer: A
What is the purpose of the @classmethod decorator in Python classes?
- A. It defines a method that belongs to the class and not the instance
- B. It defines a static method
- C. It defines a constructor
- D. It defines a destructor
Correct Answer: A
Which of the following Python code will print True? a = foo(2) b = foo(3) print(a < b)
- A. class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x < other.x: return False else: return True
- B. class foo: def __init__(self, x): self.x = x def __less__(self, other): if self.x > other.x: return False else: return True
- C. class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x < other.x: return True else: return False
- D. class foo: def __init__(self, x): self.x = x def __less__(self, other): if self.x < other.x: return False else: return True
Correct Answer: C
What is a constructor in Python classes?
- A. A special method that is automatically called when an object is created
- B. A built-in function
- C. A method that creates an object
- D. A method that destroys an object
Correct Answer: A
What does print(Test.__name__) display (assuming Test is the name of the class)?
- A. ()
- B. Exception is thrown
- C. Test
- D. __main__
Correct Answer: C
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'))
- A. Error as age isn't defined
- B. True
- C. False
- D. 7
Correct Answer: B
Which function overloads the == operator?
- A. __eq__()
- B. __equ__()
- C. __isequal__()
- D. none of the mentioned
Correct Answer: A
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
Correct Answer: C
What is the output of the following code: class Parent: def show(self): print('Parent method') class Child(Parent): pass obj = Child() obj.show()
- A. Parent method
- B. An error will occur
- C. Child method
- D. obj
Correct Answer: A
What is the output of the following code: class MyClass: x = 5 obj = MyClass() print(obj.x)
- A. 5
- B. An error will occur
- C. obj
- D. MyClass
Correct Answer: C
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
Correct Answer: A
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
Correct Answer: B