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 the purpose of a constructor in Python?

View

Which method serves as the constructor in Python?

View

When is the __del__() method called?

View

Can a class have multiple constructors in Python?

View

What happens if __init__() is not defined in a class?

View

Which statement about destructors is true?

View

What is the output of this code?

View

What is the role of __new__() in Python?

View

Can you call a destructor explicitly?

View

What happens if you try to call del on an object with references elsewhere?

View

What is the purpose of a constructor in Python?

To delete an object
To initialize an object’s attributes
To inherit from another class
To create private methods

Which method serves as the constructor in Python?

__init__()
__new__()
__del__()
__str__()

When is the __del__() method called?

When an object is created
When an object is deleted or garbage collected
When a class is defined
When the program starts

Can a class have multiple constructors in Python?

Yes, with different method names
No, Python does not allow multiple constructors
Yes, using the @classmethod decorator
Only with operator overloading

What happens if __init__() is not defined in a class?

The program throws an error
Python provides a default constructor
The object cannot be created
All attributes are public by default

Which statement about destructors is true?

Destructors are called automatically upon object creation
Destructors are called when an object is deleted or goes out of scope
Destructors are used to initialize variables
Destructors are used only in multiple inheritance

What is the output of this code?

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

Constructor called
Destructor called
Constructor called\nDestructor called
None

What is the role of __new__() in Python?

To create a new class
To create a new object instance
To initialize a private variable
To call the destructor

Can you call a destructor explicitly?

Yes, using __del__()
No, destructors are automatic only
Only with interfaces
Only through abstract classes

What happens if you try to call del on an object with references elsewhere?

The object is deleted immediately
The reference count decreases, but the object stays alive until zero references
It raises an AttributeError
The program crashes