98-372 · Question #33
Which of the following sample code segments demonstrates boxing?
The correct answer is C. int it = 12345;. Boxing is used to convert a value type to a reference type. In this sample code segment, the Integer object is a value type, and the instance of the Object class is a reference type. Converting from the value type to the reference type requires boxing. Boxing is an implicit conve
Question
Which of the following sample code segments demonstrates boxing?
Options
- Adouble dou = 12345;
- Bobject obj = 12345;
- Cint it = 12345;
- Dint it = 12345;
How the community answered
(35 responses)- A3% (1)
- B6% (2)
- C77% (27)
- D14% (5)
Explanation
Boxing is used to convert a value type to a reference type. In this sample code segment, the Integer object is a value type, and the instance of the Object class is a reference type. Converting from the value type to the reference type requires boxing. Boxing is an implicit conversion of a value type to a reference type. Consider the following declaration of a value type variable: The following statement implicitly applies the boxing operation on the variable test: object obj=test; This statement creates a reference to an object obj (on the stack) that references a value of the type int (on the heap). This value is a copy of the value-type value assigned to the variable test. It is not necessary to perform the boxing explicitly, as in the following example: object obj=(Object) test; Answer: A and D are incorrect. Converting between two value types requires neither boxing nor Answer: B is incorrect. Converting from a reference type to a value type is unboxing.
Topics
Community Discussion
No community discussion yet for this question.