Java Programming - Operators and Assignments - Discussion

3. 

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";
    }
}

[A]. slip stream
[B]. slipstream stream
[C]. stream slip stream
[D]. slipstream slip stream

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


Krutibashrath said: (Jun 12, 2011)  
Yaa fix() method is entered, strat()s s1 fix ok iT this oll things proopralis object but i want what can do you ?

Shashanka said: (Aug 15, 2011)  
I am not getting how to calculated this type of questions please any one can explain me detailly.

Remya said: (Aug 17, 2011)  
In the case of array, if fix method refers to same element, how it changes in the case of boolean?

Karthik said: (Mar 9, 2012)  
What is the difference between system.out.print and system.out.println?

Anand H.R said: (Jun 26, 2012)  
System.out.print()-it will print the message in the same line.

where as

System.out.println()-it will print the message in the next line.

Shweta said: (Sep 21, 2012)  
String is immutable then why s1 = s1 + "stream" is not giving any error.

Sridhar Salluri said: (Oct 30, 2012)  
It will not change the value of s1, it will create a new object of that string. So string immutable.

Rajesh Jaiswal said: (Jan 7, 2013)  
Is java support multiple inheritance?

Yuvaraj said: (Jan 20, 2013)  
No Java Does Not Support multiple inheritance.

Dilip Kumar said: (Jan 21, 2013)  
Here in this s1 in fix has value "slipstream" how come it changed to "slip" when control returned to start()? Can any one help me?

Neva said: (Feb 20, 2013)  
@Remya : Remember array is an Object in Java whereas Boolean is just a variable.

Sriharshaa said: (Mar 9, 2013)  
In the case of slip stream(string), why is that the change of s1 in stringfix() not reflected on to the s1 in start() ?

Raj said: (May 22, 2014)  
Because of the s1 object created with new string means s1 gets modified value in fix() and it return to start(),

Then again s1 modified & new object created and stored in s2.

Nazir said: (Jul 20, 2014)  
I am not getting the point. And can't understand how to solve this kind of problem. Its answer is right but output is logically different from the previous question.

Sathis said: (Dec 31, 2014)  
What is system.out.format(); if I use this function what package should I use?

Anjali Sadhukhan said: (Apr 7, 2015)  
Option B should be the answer, as it does not give us the explanation about the slip placed in between. And even before that my logic say fix method return the value of s1 as slip stream (s1 = s1 + "stream";) an return statement return stream to s2. And then the o/p is shown.

Suman said: (Jun 6, 2015)  
How is the slip coming between slipstream and stream? I don't understand. Please explain anyone in details?

Vinod said: (Jun 13, 2015)  
I can't understand the flow can anyone explain in detail?

Saravanan said: (Jun 28, 2015)  
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); //line 13
}

String fix(String s1)
{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}

See line 13.

System.out.println(s1 + " " + s2);

Slipstream slip stream.

The word slip in between is due to the s1 variable in line 13.

There are two print statements:

System.out.print(s1 + " "); //prints slipstream.
System.out.println(s1 + " " + s2); // prints slip stream.

Amit Katiyar said: (Aug 30, 2015)  
String Class is Immutable in Java.

So whenever we are concatenate a string object a new String object is created.

s1>>>>>>>>>Slip.

A method local s1 in fix method s1>>>>>>Slip Stream.

s2>>>>>>stream.

Monish said: (Aug 19, 2016)  
Why is slip coming in between slipstream and stream?

Can anyone answer this question?

Poorani Joe said: (Aug 22, 2016)  
In Java, the string is immutable that is we can't change the value.


Explantion:

s1 = s1 + "stream";

So, the local object is created s1 = slipstream, The first s1 value is slip.

return "stream";

The value is return to s2.

Nandinisharma said: (Sep 20, 2016)  
s1 = s1 + "stream"; we override the rid stored in s1 then why it is still pointing slip?

Siddu said: (Dec 25, 2016)  
String S1=slip means s1 is declared and pointing to slip, now it's passed to fix's s1 which is declared in method parameter and pointing to same object slip.

Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.

Siddu said: (Dec 25, 2016)  
String S1=slip means s1 is declared and pointing to slip, now it's passed to fix's s1 which is declared in method parameter and pointing to same object slip.

Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.

Siddu said: (Dec 25, 2016)  
String S1=slip means s1 is declared and pointing to slip, now it's passed to fix's s1 which is declared in method parameter and pointing to same object slip.

Now fix's s1 is changed to slipstream, as string is immutable fix's s1 acts as different variable declared in method parameter and pointing to slipstream. But starts s1 remains unchanged.

Likki said: (Mar 8, 2017)  
Thanks you all for giving the explanation.

Subhashini said: (May 22, 2019)  
Thank you @Siddu.

Mohanapriya said: (Aug 5, 2019)  
Thank you all.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.