Instructions

  • 1. Your final score will reflect your grasp of the concepts—approach each question with precision.
  • 2. Thoroughly review each solution before proceeding to ensure full understanding.
  • 3. Final results will be available after submission to provide insights into areas for further improvement.
  • 4. Maintain academic integrity—plagiarism undermines learning and professional growth.
  • 5. Once submitted, responses are final, so ensure you’re confident in your answers.
  • 6. These challenges are designed to test practical knowledge; apply your skills as you would in real-world scenarios.

All Problems

Question

Action

What is a constructor in Python?

View

Which magic method serves as the constructor in Python?

View

How many times is the __init__() method called?

View

What does the following code output?

View

What is the purpose of a destructor?

View

Which magic method is used as a destructor in Python?

View

When is the __del__() method called?

View

What is the output of this code?

View

Can you override the __init__() method in a child class?

View

What does the following code do?

View

What is a constructor in Python?

A function to delete an object
A function that initializes an object’s state
A method to destroy variables
A keyword to create objects

Which magic method serves as the constructor in Python?

__init__()
__del__()
__new__()
__construct__()

How many times is the __init__() method called?

Once for each class
Once for each object created
Twice during class definition
Depends on the size of the class

What does the following code output?

class A: def __init__(self): print("Constructor called") obj = A()

Error
Constructor called
None
No output

What is the purpose of a destructor?

To create an instance of a class
To initialize instance variables
To clean up resources when the object is deleted
To print object information

Which magic method is used as a destructor in Python?

__del__()
__init__()
__destroy__()
__close__()

When is the __del__() method called?

When the program starts
When the object is explicitly deleted or garbage-collected
Before object creation
During class declaration

What is the output of this code?

class A: def __del__(self): print("Destructor called") obj = A() del obj

Error
Destructor called
No output
Depends on the system

Can you override the __init__() method in a child class?

Yes
No
Only if the parent class is abstract
Only if the child class uses super()

What does the following code do?

class A: def __init__(self): print("A's constructor") class B(A): def __init__(self): super().__init__() print("B's constructor") obj = B()

Only prints "B's constructor"
Prints "A's constructor" and "B's constructor"
Raises an error due to multiple constructors
No output