Learn with Yasir

Share Your Feedback

Python Classes True or False Practice.

Test your knowledge of Python classes and objects with these fill-in-the-blank exercises. Learn key concepts like attributes, methods, and the __init__ method with answers provided for self-assessment.

Difficulty Levels: Beginner βœ…

Topic: Classes and Objects


πŸ” True or False

  1. In Python, you create a class using the class keyword.
  2. The `__init__` method is called automatically when a new object is created.
  3. Instance attributes are shared across all objects of a class.
  4. The `self` parameter is used to refer to the current instance of the class
  5. Class attributes are defined inside the constructor using the `self` parameter.
  6. Methods in a class operate on the object's data.

🧠 Example-Based True or False

  1. class Car:
        def __init__(self, make, model):
            self.make = make
            self.model = model
    my_car = Car("Toyota", "Corolla")
    

    The `__init__` method is used to initialize the attributes of the `Car` class.

  2. class Student:
        school = "High School"
    student1 = Student()
    student2 = Student()
    student1.school = "Middle School"
    

    Changing the `school` attribute of `student1` will also change it for `student2`.

  3. class Dog:
        def bark(self):
            print("Woof!")
    my_dog = Dog()
    my_dog.bark()
    

    The `bark` method is an instance method of the `Dog` class.

  4. class Circle:
        pi = 3.14
    print(Circle.pi)
    

    The `pi` attribute is a class attribute of the `Circle` class.


πŸ—οΈ Answer Key

βœ… Answers for True or False

  1. βœ… True.
  2. βœ… True.
  3. ❌ False. Instance attributes are unique to each object. It’s class attributes that are shared.
  4. βœ… True.
  5. ❌ False. Class attributes are defined outside the constructor, directly in the class body.
  6. βœ… True.

πŸ” Answers for Example-Based True or False

  1. βœ… True.
  2. ❌ False. Because assigning student1.school creates an instance variable that doesn't affect the class variable or other instances.
  3. βœ… True.
  4. βœ… True.

πŸ“š Related Resources