(10 points) What are the precise outputs of the following program when they are
ID: 3872884 • Letter: #
Question
(10 points) What are the precise outputs of the following program when they are compiled and run.
class ObjCreator {
public static int a, b;
public static void main(String[] args){
ObjCreator obj1 = new ObjCreator();
ObjCreator obj2 = new ObjCreator();
obj1.a=20;
obj1.b=100;
obj2.a=obj1.a+10;
obj2.b=obj1.b+10;
swap(obj1, obj2);
System.out.println(obj1.a+" "+obj1.b);
System.out.println(obj2.a+" "+obj2.b);
}
public static void swap(ObjCreator obj1, ObjCreator obj2){
ObjCreator temp = obj1; obj1=obj2; obj2=temp;
}
}
Explanation / Answer
Obj1.a have 20 and Obj1.b have 100
but while assigning to Obj2.a as Obj1.a+10, Obj2.a and Obj1.a both have the same values as 30.
in the same way Obj2.b and Obj1.b have 110.
after swaping also, both have the same values.
Output: 30 110
30 110
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.