Difficulty: Medium
Correct Answer: hhe!
Explanation:
Introduction / Context:
This problem explores array indexing equivalence, pre/post-increment behavior, and modification of characters through lvalues. The expression i[s] is the same as s[i]; both rely on pointer arithmetic. Understanding evaluation order and side effects is essential to predict the output.
Given Data / Assumptions:
Concept / Approach:
Track i carefully and know what character each operation reads or writes. The last operation ++i[s] increments the stored character at position i and yields the incremented character, altering the array content.
Step-by-Step Solution:
ch = s[++i]; i becomes 1; s[1] = 'h' → prints h.ch = s[i++]; uses i = 1 then increments to 2; s[1] = 'h' → prints h.ch = i++[s]; same as s[i++]; uses i = 2 then increments to 3; s[2] = 'e' → prints e.ch = ++i[s]; i is 3; i[s] is s[3] (space). ++ applied to the character increments space (ASCII 32) to '!' (ASCII 33), stores back, and yields '!' → prints !.
Verification / Alternative check:
Insert debugging prints of i and character codes; you will see i evolve as 1, 2, 3 and the final modification at index 3. After execution, s becomes "The!cocaine man" due to the mutated space.
Why Other Options Are Wrong:
'he c', 'The c', 'Hhec': do not match the exact sequence of characters and increments produced by the operations.
Common Pitfalls:
Thinking i[s] is different from s[i]; forgetting that ++ on a char lvalue changes the stored character; mixing up pre vs post increment.
Final Answer:
hhe!
Discussion & Comments