Java | Constructors | Question 1
Predict the output?
package main; class T { int t = 20 ; } class Main { public static void main(String args[]) { T t1 = new T(); System.out.println(t1.t); } } |
chevron_right
filter_none
(A) 20
(B) 0
(C) Compiler Error
Answer: (A)
Explanation: In Java, member variables can assigned a value with declaration. In C++, only static const variables can be assigned like this.
Quiz of this Question
Recommended Posts:
- Java | Constructors | Question 5
- Java | Constructors | Question 8
- Java | Constructors | Question 2
- Java | Constructors | Question 6
- Java | Constructors | Question 4
- Java | Constructors | Question 3
- Why Constructors are not inherited in Java?
- Order of execution of Initialization blocks and Constructors in Java
- C++ | Constructors | Question 14
- C++ | Constructors | Question 5
- C++ | Constructors | Question 16
- C++ | Constructors | Question 15
- C++ | Constructors | Question 10
- C++ | Constructors | Question 17
- C++ | Constructors | Question 17