Understanding Python Classes and Objects Quiz

Explore the fundamentals of Python's class and object system with straightforward questions ideal for those beginning backend development. Get familiar with key concepts, syntax, and best practices.

  1. Defining a Class

    Which keyword is used to define a class in Python?

    1. class
    2. def
    3. func

    Explanation: The 'class' keyword is specifically used for defining new classes in Python. 'def' is used to define functions, and 'func' is not a recognized Python keyword for declaring classes or functions.

  2. Creating an Object

    What is the correct way to create an object from a class named Car?

    1. my_car = Car()
    2. my_car == Car
    3. Car(my_car)

    Explanation: 'my_car = Car()' correctly creates an instance of Car and assigns it to my_car. 'my_car == Car' is a comparison, not object creation, and 'Car(my_car)' attempts to pass an argument to Car, which may not be necessary.

  3. Instance Attributes

    Where are instance attributes typically initialized in a Python class?

    1. Inside the __init__ method
    2. Outside any method
    3. In the main program

    Explanation: Instance attributes are usually initialized inside the '__init__' method for each object. Declaring them outside any method makes them class attributes, and defining them in the main program does not associate them with the class instance.

  4. Accessing Object Methods

    How do you call a method named drive on an object car?

    1. car.drive()
    2. drive.car()
    3. car->drive()

    Explanation: 'car.drive()' is the correct syntax for calling a method on an object. 'drive.car()' and 'car->drive()' are invalid in Python; the arrow syntax is common in other languages like C++.

  5. The self Keyword

    Why is self used in method definitions of a Python class?

    1. To reference the current instance
    2. To import modules
    3. To define static methods

    Explanation: 'self' refers to the current instance of the class inside instance methods. It is not used for importing modules, and static methods do not require 'self' as a parameter.

  6. Class vs. Instance Attributes

    Which attribute changes value for every object instance?

    1. Instance attribute
    2. Class attribute
    3. Static attribute

    Explanation: An instance attribute belongs to each object separately, allowing different values per instance. Class attributes are shared across all instances, and 'static attribute' is not a common Python term.

  7. Inheritance Basics

    How do you define a class Bike that inherits from Vehicle?

    1. class Bike(Vehicle):
    2. class Bike = Vehicle
    3. inherit Bike Vehicle

    Explanation: Inheritance in Python is declared using the syntax 'class Bike(Vehicle):'. The other options do not follow Python's inheritance syntax.

  8. Object Deletion

    Which method is called when an object is about to be destroyed in Python?

    1. __del__
    2. __init__
    3. __create__

    Explanation: '__del__' is the destructor method automatically called when an object is about to be deleted. '__init__' is used for initialization, and '__create__' is not a standard Python method.