Difficulty: Medium
Correct Answer: Infinite loop (infinite recursion/stack overflow)
Explanation:
Introduction / Context:
This problem focuses on the difference between pre- and post-decrement when used in function-call arguments, and how this interacts with recursion. Using no-- in the call can lead to unexpected non-termination.
Given Data / Assumptions:
Concept / Approach:
The post-decrement operator yields the current value first and then decrements the variable after the value has been used to form the function argument. Because function arguments are evaluated before the call, the recursive invocation receives the old value, so the parameter does not decrease across recursive levels, causing unbounded recursion.
Step-by-Step Solution:
First call: prints “5,” then calls reverse(5) again due to post-decrement semantics.Within that call, the same pattern repeats indefinitely.The local no gets decremented after the argument is evaluated, but that decrement affects only the caller’s local copy, not the callee’s argument in the next frame.As a result, the base case no == 0 is never reached.
Verification / Alternative check:
Change the recursive call to reverse(--no); or reverse(no - 1); to ensure the argument decreases, producing the expected countdown and termination.
Why Other Options Are Wrong:
A/B/C assume termination with a decreasing sequence to zero, which does not occur here.
Common Pitfalls:
Believing that no-- lowers the value passed into the next call (it does not); forgetting that post-decrement applies after the value is used.
Final Answer:
Infinite loop (infinite recursion/stack overflow)
Discussion & Comments