Q46
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);
A.
34 34
B.
65 34
AnswerC.
65 65
D.
34 65
Answer: Option B
Solution
Answer: Option B
Solution:
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
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.