Quiz for Python Classes

Python Quiz for Classes – 01

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%
16

Python Classes – 01

Level: Intermediate

1 / 10

1. Which of the following is correct to define a constructor in Python?

2 / 10

2. What will be the output of the following code?

class MyClass:
  def my_function(self):
    my_dict = {'x': 10, 'y': 20}
    return my_dict['x'] + my_dict['y']

test = MyClass()
print(test.my_function())

3 / 10

3. What will be the output of the following code?

class Test:
  def show(self, p):
    print('Hello '+ p)

t = Test()
t.show('World')

4 / 10

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

5 / 10

5. How do you access a class attribute?

6 / 10

6. How can you check if a key exists in a dictionary attribute of a class?

7 / 10

7. How do you add an item to a list attribute of a class?

8 / 10

8. What will be the output of the following code?

class Test:
  def show(self, lst):
     return sum(lst)

t = Test()
t.show([1, 2, 3])

9 / 10

9. Which of the following is correct for method overloading in Python?

10 / 10

10. What will the following code print?

class Demo:
  def __init__(self):
    self.items = [1, 2, 3]

  def get_first_item(self):
    return self.items[0]

d = Demo()
print(d.get_first_item())

Your score is

0%