Quiz for Python Classes

Python Quiz : Python Classes – 03

Python Quiz for Classes

Welcome to the Python Quiz for Classes! As one of the foundational concepts in object-oriented programming, understanding classes is essential for anyone looking to master the Python language. This quiz is designed to test your knowledge and grasp of Python classes, their attributes, methods, inheritance, and more. This quiz promises to be both challenging and enlightening for all levels of developers. Dive in and see how well you understand the intricacies of Python classes!

Prepare Yourself: Python Classes

0%
9

Python Classes – 03

Level: Intermediate

1 / 10

1. What will the following code print?

class Student:
  courses = []
  def add_course(self, course):
    self.courses.append(course)

s1 = Student()
s1.add_course('Math')
s2 = Student()
s2.add_course('Physics')
print(s1.courses)

2 / 10

2. What is the output of the following code?

class MyClass:
  my_list = [1, 2, 3]
  def add_element(self, elem):
    self.my_list.append(elem)

test = MyClass()
test.add_element(4)
print(MyClass.my_list)

3 / 10

3. How can you access the first element of a list attribute in a class?

4 / 10

4. What does the ‘@property’ decorator do in Python classes?

5 / 10

5. How do you call a static method in Python for a class “MyClass”?

6 / 10

6. How do you remove a key-value pair from a dictionary attribute in a Python class?

7 / 10

7. What does the following code do?

class MyClass:
  def __del__(self):
    print('Destructor called')

obj = MyClass()
del obj

8 / 10

8. How do you define a method inside a class?

9 / 10

9. What will the following code return?

class Student:
  def __init__(self, name):
    self.name = name
    self.marks = {}

  def add_mark(self, subject, mark):
    self.marks[subject] = mark

s = Student('John')
s.add_mark('Math', 90)
print(s.marks)

10 / 10

10. What is the ‘__init__’ method in Python?

Your score is

0%