Difficulty: Easy
Correct Answer: 1, 4, 5
Explanation:
Introduction / Context:
Boxing and unboxing are core CLR mechanisms that allow value types to be treated as objects. This question also touches on nullability of primitive value types like bool.
Given Data / Assumptions:
Concept / Approach:
Boxing is the conversion of a value type instance to a reference type (object), which allocates a new object on the managed heap and copies the value. Unboxing is the reverse: extracting the value type from an object reference, requiring a cast. A non-nullable bool cannot be null; only bool? can. Because object is the ultimate base type for all managed types, an object variable can hold a reference to any type instance.
Step-by-Step Solution:
Verification / Alternative check:
Write a snippet: int x = 5; object o = x; int y = (int)o; Observe the boxing (x → o) and unboxing (o → y).
Why Other Options Are Wrong:
Options that include statements 2 or 3 invert the definitions of boxing and unboxing.
Common Pitfalls:
Confusing the direction of boxing/unboxing; overlooking that non-nullable primitives cannot hold null.
Final Answer:
1, 4, 5
Discussion & Comments