Home / Java Programming / Java.lang Class :: Discussion

Discussion :: Java.lang Class

  1. 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); 

  2. A.
    abcXyZ
    B.
    abcxyz
    C.
    xyzabc
    D.
    XyZabc

    View Answer

    Workspace

    Answer : Option C

    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.


Be The First To Comment