What will the following code print?

public class A {
  int a;  //--1--
  
  public short getB() {
    short b;  //--2--
    return b;  //--3--
  }
    
  public static void main(String[] args) {
    System.out.print(new A().a); // --4--
    System.out.println(new A().getB());
  }
}
Explanation
Compilation error will occur in line 3 because of the usage of local variable b that is not initialised.
The error will NOT occur in line 4 since the class field is initialised with zero by default.

@vas7779468 thanks! fixed

2018 Feb 2, 5:40:17 PM

there is no line "4". i guess you mean "1"

2018 Jan 17, 7:16:21 PM

Java
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback