What are the main stages of an object's lifecycle in C++?
The main stages are: creation (when the object is made), usage (when the object is used), and destruction (when the object is removed and its memory freed).
Click to reveal answer
beginner
What happens during the construction of an object?
During construction, the object's memory is allocated and its constructor function runs to set up initial values or states.
Click to reveal answer
beginner
What is the role of a destructor in C++?
A destructor is a special function that runs automatically when an object is destroyed. It cleans up resources like memory or files the object used.
Click to reveal answer
intermediate
How does scope affect an object's lifecycle?
An object created inside a scope (like inside a function) is destroyed automatically when the scope ends, meaning its destructor is called then.
Click to reveal answer
intermediate
What is the difference between automatic and dynamic object lifecycles?
Automatic objects are created and destroyed automatically based on scope. Dynamic objects are created with new and must be manually destroyed with delete.
Click to reveal answer
When is an object's destructor called in C++?
AOnly when the program ends
BWhen the object is created
CWhen the program starts
DWhen the object goes out of scope or is deleted
✗ Incorrect
The destructor runs when the object is no longer needed, either when it goes out of scope or when deleted if created dynamically.
Which keyword is used to create a dynamic object in C++?
Anew
Bcreate
Cmalloc
Dconstruct
✗ Incorrect
The new keyword allocates memory and calls the constructor for dynamic objects.
What happens if you forget to delete a dynamic object?
AProgram crashes immediately
BObject is destroyed automatically
CMemory leak occurs
DDestructor runs twice
✗ Incorrect
Not deleting dynamic objects causes memory to stay allocated, leading to memory leaks.
Which function is automatically called when an object is created?
ACopy
BConstructor
CMain
DDestructor
✗ Incorrect
The constructor sets up the object when it is created.
If an object is declared inside a function, when is it destroyed?
AWhen the function ends
BAt program start
CWhen the object is used
DNever
✗ Incorrect
Objects declared inside a function are destroyed when the function finishes and the scope ends.
Explain the lifecycle of an object in C++ from creation to destruction.
Think about what happens when you make an object and when it disappears.
You got /6 concepts.
Describe the difference between automatic and dynamic object lifecycles in C++.
Consider how and when objects are created and destroyed.
You got /5 concepts.
Practice
(1/5)
1. Which of the following best describes when a destructor is called for an automatic (local) object in C++?
easy
A. When the object is created
B. When the object goes out of scope
C. When the program starts
D. Only when the object is deleted manually
Solution
Step 1: Understand automatic object lifecycle
Automatic objects are created when their scope starts and destroyed when their scope ends.
Step 2: Identify destructor call timing
The destructor is called automatically when the object goes out of scope to clean up resources.
Final Answer:
When the object goes out of scope -> Option B
Quick Check:
Destructor called at scope end = When object goes out of scope [OK]
Hint: Destructor runs automatically at scope end for local objects [OK]
Common Mistakes:
Thinking destructor runs at object creation
Believing manual deletion is needed for local objects
Confusing program start with object destruction
2. Which of the following is the correct syntax to declare a destructor for a class named Car in C++?
easy
A. void Car();
B. destructor Car();
C. Car::~Car();
D. ~Car();
Solution
Step 1: Recall destructor syntax
Destructor syntax uses a tilde (~) followed by the class name and parentheses.
Step 2: Match correct option
~Car(); matches the correct syntax: ~Car();
Final Answer:
~Car(); -> Option D
Quick Check:
Destructor syntax = ~ClassName() [OK]
Hint: Destructor always starts with ~ followed by class name [OK]
Common Mistakes:
Using void return type for destructor
Writing destructor with class scope resolution in declaration
Using keyword 'destructor' which doesn't exist
3. What will be the output of the following C++ code?
class Test {
public:
Test() { std::cout << "Created "; }
~Test() { std::cout << "Destroyed "; }
};
int main() {
Test t1;
{
Test t2;
}
return 0;
}
medium
A. Created Created Destroyed Destroyed
B. Created Destroyed Created Destroyed
C. Created Created Destroyed
D. Destroyed Created Created Destroyed
Solution
Step 1: Trace object creation
First, t1 is created: prints "Created ". Then inside inner scope, t2 is created: prints another "Created ".
Step 2: Trace object destruction
When inner scope ends, t2 is destroyed: prints "Destroyed ". Then at program end, t1 is destroyed: prints "Destroyed ".
Final Answer:
Created Created Destroyed Destroyed -> Option A
Quick Check:
Constructor and destructor calls match output [OK]
Hint: Objects destroyed in reverse order of creation at scope end [OK]
Common Mistakes:
Assuming destruction happens immediately after creation
Forgetting inner scope destruction before outer
Mixing order of constructor and destructor prints
4. Consider the following code snippet:
class Sample {
public:
Sample() { std::cout << "Start "; }
~Sample() { std::cout << "End "; }
};
int main() {
Sample* p = new Sample();
// Missing delete p;
return 0;
}
What is the problem with this code?
medium
A. Syntax error due to missing delete
B. Destructor is called twice causing error
C. Memory leak because destructor is not called
D. No problem, code runs fine
Solution
Step 1: Identify dynamic object creation
The object is created with new, so it lives on the heap and needs manual deletion.
Step 2: Check for destructor call
Since delete p; is missing, destructor is never called, causing a memory leak.
Final Answer:
Memory leak because destructor is not called -> Option C
Quick Check:
Missing delete on new object = memory leak [OK]
Hint: Always delete dynamically allocated objects to call destructor [OK]
Common Mistakes:
Assuming destructor runs automatically for new objects
Thinking missing delete causes syntax error
Believing destructor is called twice
5. You want to track how many objects of class Box currently exist during program execution. Which approach correctly uses constructors and destructors to maintain this count?
hard
A. Increment a static counter in constructor and decrement it in destructor
B. Increment a global variable in constructor only
C. Decrement a static counter in constructor and increment in destructor
D. Use constructor to print count and destructor to reset count
Solution
Step 1: Understand object counting logic
To track live objects, increase count when object is created and decrease when destroyed.
Step 2: Use static counter in class
A static variable shared by all objects can hold the count, incremented in constructor and decremented in destructor.
Final Answer:
Increment a static counter in constructor and decrement it in destructor -> Option A