Java.lang Class

Q1: What will be the output of the program?
public class StringRef 
{
    public static void main(String [] args) 
    {
        String s1 = 'abc';
        String s2 = 'def';
        String s3 = s2;   /* Line 7 */
        s2 = 'ghi';
        System.out.println(s1 + s2 + s3);
    }
}

A abcdefghi

B abcdefdef

C abcghidef

D abcghighi

ANS:A - abcdefghi

After line 7 executes, both s2 and s3 refer to a String object that contains the value 'def'. When line 8 executes, a new String object is created with the value 'ghi', to which s2 refers. The reference variable s3 still refers to the (immutable) String object with the value 'def'.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.