Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 3)
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";
}
}
Answer: Option
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".
Discussion:
30 comments Page 2 of 3.
Monish said:
9 years ago
Why is slip coming in between slipstream and stream?
Can anyone answer this question?
Can anyone answer this question?
Amit katiyar said:
1 decade ago
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.
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.
Saravanan said:
1 decade ago
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.
{
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.
Vinod said:
1 decade ago
I can't understand the flow can anyone explain in detail?
Suman said:
1 decade ago
How is the slip coming between slipstream and stream? I don't understand. Please explain anyone in details?
Krutibashrath said:
1 decade ago
Yaa fix() method is entered, strat()s s1 fix ok iT this oll things proopralis object but i want what can do you ?
Sathis said:
1 decade ago
What is system.out.format(); if I use this function what package should I use?
Nazir said:
1 decade ago
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.
Raj said:
1 decade ago
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.
Then again s1 modified & new object created and stored in s2.
Sriharshaa said:
1 decade ago
In the case of slip stream(string), why is that the change of s1 in stringfix() not reflected on to the s1 in start() ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers