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

2/4

Page

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

The assignment of more than one function to a particular operator is . . . . . . . .

Select an option to see the answer and solution.

What is the difference between a class variable and an instance variable in Python?

Select an option to see the answer and solution.

How can you access a class variable in Python?

Select an option to see the answer and solution.

. . . . . . . . represents an entity in the real world with its identity and behaviour.

Select an option to see the answer and solution.

. . . . . . . . is used to create an object.

Select an option to see the answer and solution.

What are the methods which begin and end with two underscore characters called?

Select an option to see the answer and solution.

What is setattr() used for?

Select an option to see the answer and solution.

What is getattr() used for?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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)

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is a constructor in Python classes?

Select an option to see the answer and solution.

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

Options are not available for this question.

Select an option to see the answer and solution.

Which function overloads the + operator?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.