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 delattr(obj,name) used for?
- A. To print deleted attribute
- B. To delete an attribute
- C. To check if an attribute is deleted or not
- D. To set an attribute
Correct Answer: B
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()
- A. The program has an error because constructor can't have default arguments
- B. Nothing is displayed
- C. "Hello World" is displayed
- D. The program has an error display function doesn't have parameters
Correct Answer: C
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)
- A. 2
- B. 1
- C. An error will occur
- D. 0
Correct Answer: D
What is the purpose of the @property decorator in Python classes?
- A. It allows you to define a method that can be accessed like an attribute
- B. It defines a property
- C. It defines a method
- D. It defines a constructor
Correct Answer: A
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)
- A. Error
- B. Nothing is printed
- C. __str__ called
- D. __repr__ called
Correct Answer: C
How can you create a static variable in a Python class?
- A. By defining a variable within the class but outside any method
- B. By using the static keyword
- C. By using the class keyword
- D. By using the variable keyword
Correct Answer: A
__del__ method is used to destroy instances of a class.
- A. True
- B. False
Correct Answer: A
How can you check if an object belongs to a specific class in Python?
- A. By using the isinstance() function
- B. By using the belongsto() method
- C. By using the classof() method
- D. By using the typeof() function
Correct Answer: A
What is the output of the following code: class Parent: x = 10 class Child(Parent): pass obj = Child() print(obj.x)
- A. 10
- B. An error will occur
- C. obj
- D. Child
Correct Answer: A
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__)
- A. Exception is thrown
- B. __main__
- C. Nothing is displayed
- D. Base class for all students
Correct Answer: D
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
Correct Answer: C
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__))
- A. 12
- B. 52
- C. 13
- D. 60
Correct Answer: C