Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 13)
13.
What will be the output of the program?
class Two
{
byte x;
}
class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
Two fix(Two tt)
{
tt.x = 42;
return tt;
}
}
Answer: Option
Explanation:
In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0.
Discussion:
21 comments Page 3 of 3.
Siva said:
6 years ago
public class Two
{
byte x;
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);//t2=t
System.out.println(t.x + " " + t2.x);//t.x=42 "" t2=42
}
Two fix(Two tt) //fix(two tt)===> Two tt = new Two();===>x=42
{
tt.x = 42;
return tt;
}
public static void main(String [] args)
{
Two p = new Two();
p.start();
}
}
{
byte x;
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);//t2=t
System.out.println(t.x + " " + t2.x);//t.x=42 "" t2=42
}
Two fix(Two tt) //fix(two tt)===> Two tt = new Two();===>x=42
{
tt.x = 42;
return tt;
}
public static void main(String [] args)
{
Two p = new Two();
p.start();
}
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers