Java | Class and Object | Question 7
Predict the output of the following program.
class Test { int a = 1; int b = 2; Test func(Test obj) { Test obj3 = new Test(); obj3 = obj; obj3.a = obj.a++ + ++obj.b; obj.b = obj.b; return obj3; } public static void main(String[] args) { Test obj1 = new Test(); Test obj2 = obj1.func(obj1); System.out.println("obj1.a = " + obj1.a + " obj1.b = " + obj1.b); System.out.println("obj2.a = " + obj2.a + " obj1.b = " + obj2.b); } } |
chevron_right
filter_none
(A)
obj1.a = 1 obj1.b = 2 obj2.a = 4 obj2.b = 3
(B)
obj1.a = 4 obj1.b = 3 obj2.a = 4 obj2.b = 3
(C) Compilation error
Answer: (B)
Explanation:
obj1 and obj2 refer to same memory address.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Recommended Posts:
- Java | Class and Object | Question 4
- Java | Class and Object | Question 6
- Java | Class and Object | Question 1
- Java | Class and Object | Question 5
- Java | Class and Object | Question 4
- Java | Class and Object | Question 2
- Object class in Java
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 4
- C++ | Class and Object | Question 6
- C++ | Class and Object | Question 2
- C++ | Class and Object | Question 5
- C++ | Class and Object | Question 3
- Object level and Class level locks in Java
- Java | Abstract Class and Interface | Question 1



