Difficulty: Easy
Correct Answer: T
Explanation:
Introduction / Context:
This classic trick relies on the C rule that a[b]
is defined as (a + b)
. Therefore b[a]
is equally valid and means the same thing. The code indexes into a string literal using the integer 7 placed before the brackets.
Given Data / Assumptions:
"CuriousTab"
.
Concept / Approach:
Expand the identity: a[b] → *(a + b). Hence b[a] → *(b + a), which is the same address. Determine the 8th character (index 7) of the literal.
Step-by-Step Solution:
Write the indices: C(0) u(1) r(2) i(3) o(4) u(5) s(6) T(7) a(8) b(9)The character at index 7 is Tprintf with %c prints that single character
Verification / Alternative check:
Replace 7["CuriousTab"] with "CuriousTab"[7]; the program prints the same result.
Why Other Options Are Wrong:
(a) The syntax is valid C. (b) The program prints one character. (d) Prints the numeral 7, which is not the array element. (e) would be index 0, not 7.
Common Pitfalls:
Assuming only a[b]
is legal but not b[a]
; forgetting zero-based indexing for strings.
Final Answer:
T
Discussion & Comments