?
Determine output:
Determine output:
public class Test{
int i = 34;
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.i = 65;
System.out.print(t1.i);
System.out.print(t2.i);
}
}
public class Test{
int i = 34;
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.i = 65;
System.out.print(t1.i);
System.out.print(t2.i);
}
}
Answer & Solution
Correct Answer:
Option
B
Instance variable is unique for their object, that means each copy of memory for variable i will be available for each object. So, changing value of i from one object will not affect the value of i accessed by another object.
1. Two objects of the Test class, t1 and t2, are created using the new keyword.
2. The default value of the instance variable i is 34 as defined in the class Test.
3. The instance variable i of t1 is then set to 65 using
4. When we print
5. When we print
Therefore, the correct output will be 6534.
1. Two objects of the Test class, t1 and t2, are created using the new keyword.
2. The default value of the instance variable i is 34 as defined in the class Test.
3. The instance variable i of t1 is then set to 65 using
t1.i = 65;4. When we print
t1.i, it will output 65.5. When we print
t2.i, it will output 34. This is because t2 is a separate instance of the Test class, and its i remains the default value of 34.Therefore, the correct output will be 6534.
Join the Discussion
Login to post a comment or share your explanation.
Login