Exercise :: Java.lang Class - Finding the output
- Java.lang Class - General Questions
- Java.lang Class - Finding the output
- Java.lang Class - Pointing out the correct statements
1. | What will be the output of the program? How many String objects have been created? |
|||||||
Answer: Option C Explanation: Line 1 creates two, one referred to by x and the lost String "xyz". Line 2 creates one (for a total of three). Line 3 creates one more (for a total of four), the concatenated String referred to by x with a value of "xyzabc". |
2. | What will be the output of the program?
|
|||||||
Answer: Option B Explanation: Line 13 fails because == compares reference values, not object values. Line 15 succeeds because both String and primitive wrapper constructors resolve to the same value (except for the Character wrapper). Lines 17, 19, and 21 fail because the equals() method fails if the object classes being compared are different and not in the same tree hierarchy. |
3. | What will be the output of the program?
|
|||||||
Answer: Option D Explanation: Line 10 fails because b1 and b2 are two different objects. Lines 12 and 18 succeed because the Boolean String constructors are case insensitive. Lines 14 and 16 fail because true is not equal to false. |
4. | What will be the output of the program?
|
|||||||
Answer: Option D Explanation: Even though o and oc are reference variables of different types, they are both referring to the same object. This means that == will resolve to true and that the default equals() method will also resolve to true. |
5. | What will be the output of the program?
|
|||||||
Answer: Option B Explanation: Math.round() adds .5 to the argument then performs a floor(). Since the code adds an additional .5 before round() is called, it's as if we are adding 1 then doing a floor(). The values that start out as integer values will in effect be incremented by 1 on the round() side but not on the ceil() side, and the noninteger values will end up equal. |