Home / Java Programming / Operators and Assignments :: Discussion

Discussion :: Operators and Assignments

  1. What will be the output of the program?

    class PassS 
    {     
       public static void main(String [] args)      
       {       
           PassS p = new PassS();        
           p. start();    
       }     
    
       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";   
       } 
    } 
    

  2. A.

    slip stream

    B.

    slipstream stream

    C.

    stream slip stream

    D.

    slipstream slip stream

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    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".


Be The First To Comment