Java | Class and Object | Question 1
Predict the output of following Java program?
class Test { int i; } class Main { public static void main(String args[]) { Test t; System.out.println(t.i); } |
chevron_right
filter_none
(A) 0
(B) garbage value
(C) compiler error
(D) runtime error
Answer: (C)
Explanation: t is just a reference, the object referred by t is not allocated any memory. Unlike C++, in Java all non-primitive objects must be explicitly allocated and these objects are allocated on heap. The following is corrected program.
class Test { int i; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.i); } |
chevron_right
filter_none
Recommended Posts:
- Java | Class and Object | Question 7
- Java | Class and Object | Question 6
- Java | Class and Object | Question 5
- Java | Class and Object | Question 2
- Java | Class and Object | Question 4
- Java | Class and Object | Question 4
- Object class in Java
- C++ | Class and Object | Question 4
- C++ | Class and Object | Question 6
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 5
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 2
- Object level and Class level locks in Java
- Java | Abstract Class and Interface | Question 2


