Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 7)
7.
What will be the output of the program?
String x = "xyz";
x.toUpperCase(); /* Line 2 */
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);
abcXyZ
abcxyz
xyzabc
XyZabc
Answer: Option
Explanation:

Line 2 creates a new String object with the value "XYZ", but this new object is immediately lost because there is no reference to it. Line 3 creates a new String object referenced by y. This new String object has the value "xyz" because there was no "Y" in the String object referred to by x. Line 4 creates a new String object, appends "abc" to the value "xyz", and refers y to the result.

Discussion:
6 comments Page 1 of 1.

RIYAZ said:   3 years ago
D is the right option.

Namitha said:   7 years ago
Sysout statement should display the value of y as xyz.

I feel cos y= y+ abc would be lost.

Manoj said:   9 years ago
As there is no reference created after x=x.toUpperCase(); statement.
So the answer will be xyzabc.

Atika said:   9 years ago
Why not the answer is option D?

Mayur said:   1 decade ago
Ya its correct now.xyx is converted to XYZ and is overwritten in x.nexY is replaced by y and then XyZ is concatenated with abc as + orks as concatenantion operator in Java with string types

Pratik patel said:   1 decade ago
String x = "xyz";
x=x.toUpperCase();
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);
now o/p is XyZabc is it true??

Post your comments here:

Your comments will be displayed after verification.