Examveda
Examveda

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

C. 65 65

D. 34 65

Answer: Option B

Solution(By Examveda Team)

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.

This Question Belongs to Java Program >> Data Types And Variables

Join The Discussion

Comments ( 1 )

  1. Mallikarjun Guggari
    Mallikarjun Guggari :
    9 months ago

    answer is c option(65 65); because first given x value as 34 it was stored in the heap statement then we assign the x was is 65 in the main method then it goes to the heap statement and remove the 34 in to 65 then we print the test1 and test2 then output is 65 65

Related Questions on Data Types and Variables