Vidyalelo
Python · all questions

Classes and Objects in Python
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

67

Questions

3/4

Page

Pick an option on a question to see the right answer and solution.

How can you create a private attribute in a Python class?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Which function overloads the // operator?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Special methods need to be explicitly called during object creation.

Select an option to see the answer and solution.

Which of the following is not a class method?

Select an option to see the answer and solution.

What is the purpose of the super() function in Python classes?

Select an option to see the answer and solution.

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)

Select an option to see the answer and solution.

Which of the following Python code creates an empty class?

Select an option to see the answer and solution.

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)

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is hasattr(obj,name) used for?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

__del__ method is used to destroy instances of a class.

Select an option to see the answer and solution.

Which function overloads the >> operator?

Select an option to see the answer and solution.