Difficulty: Easy
Correct Answer: 20
Explanation:
Introduction / Context: Unions in C overlay their members in the same memory location, so writing to one member overwrites the storage seen by the others. This question tests that understanding.
Given Data / Assumptions:
Concept / Approach: The last write into the union’s shared storage sets the bits seen by all members. After writing v.b = 20, both a and b see the same bit pattern, which corresponds to 20 when interpreted as int.
Step-by-Step Solution:
Initial write: v.a = 10 stores the bit pattern of 10 Overwrite: v.b = 20 replaces the same storage with the bit pattern of 20 Reading v.a now reads that same storage -> 20Verification / Alternative check: Printing v.b immediately after would also show 20, confirming the shared memory behavior of unions.
Why Other Options Are Wrong:
Common Pitfalls: Assuming unions keep independent values like structs; they do not. Only the most recent write is represented in shared storage.
Final Answer: 20
Discussion & Comments