Difficulty: Medium
Correct Answer: 12344321
Explanation:
Introduction / Context:
This program tests your understanding of C++ object lifetime: when constructors run, when destructors run, and the order they print values when a global counter is incremented/decremented. It also includes a temporary object in a nested block to verify scope-bound destruction.
Given Data / Assumptions:
Concept / Approach:
Automatic objects are constructed in declaration order and destroyed in reverse order of construction when their scope ends. An object created inside an inner block is destroyed at the end of that block, before the outer-scope objects are destroyed.
Step-by-Step Solution:
Verification / Alternative check:
Trace val on paper to ensure increments and decrements line up with construction/destruction order.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting that the inner object is destroyed before the outer objects, and that destructors run in reverse construction order.
Final Answer:
12344321
Discussion & Comments