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.
Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
- A. __add__(), __str__()
- B. __str__(), __add__()
- C. __sum__(), __str__()
- D. __str__(), __sum__()
Correct Answer: A
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
- A. True
- B. False
Correct Answer: A
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)
- A. Error because function change can't be called in the __init__ function
- B. 'New' is printed
- C. 'Old' is printed
- D. Nothing is printed
Correct Answer: C
Which of the following Python code creates an empty class?
- A. class A: return
- B. class A: pass
- C. class A:
- D. It is not possible to create an empty class
Correct Answer: B
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)
- A. 6
- B. 7
- C. Error
- D. 0
Correct Answer: B
What is the purpose of the super() function in Python classes?
- A. It is used to call a method from the parent class
- B. It is used to create an object
- C. It is used to delete an object
- D. It is used to inherit a class
Correct Answer: A
Which of the following is not a class method?
- A. Non-static
- B. Static
- C. Bounded
- D. Unbounded
Correct Answer: A
Special methods need to be explicitly called during object creation.
- A. True
- B. False
Correct Answer: B
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()
- A. Exception is thrown
- B. __main__
- C. Demo
- D. test
Correct Answer: B
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()
- A. Runs normally, doesn't display anything
- B. Displays 0, which is the automatic default value
- C. Error as one argument is required while creating the object
- D. Error as display function requires additional argument
Correct Answer: C
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()
- A. Exception is thrown
- B. Count.test=25 k=25
- C. Count.test=25 k=0
- D. Count.test=0 k=0
Correct Answer: C
How can you create a private attribute in a Python class?
- A. By prefixing the attribute name with an underscore (_)
- B. By using the private keyword
- C. By using the hidden keyword
- D. By using the secure keyword
Correct Answer: A