Operators and Assignments

Q1: What will be the output of the program?
class PassS 
    public static void main(String [] args) 
        PassS p = new PassS();

    void start() 
        String s1 = 'slip';
        String s2 = fix(s1);
        System.out.println(s1 + ' ' + s2);

    String fix(String s1) 
        s1 = s1 + 'stream';
        System.out.print(s1 + ' ');
        return 'stream';

A slipstream slip stream

B slipstream stream

C slip stream

D stream slip stream

ANS:A - slip stream

When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of 'slip'). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of 'slipstream'). When the program returns to start(), another String object is created, referred to by s2 and with a value of 'stream'.

img not found

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.