30.7k views
2 votes
What is the output of below..

default constructor

// Main.java
class Test {
int i;
Test t;
boolean b;
byte bt;
float ft;
}

public class Main {
public static void main(String args[]) {
Test t = new Test(); // default constructor is called.
System.out.println(t.i);
System.out.println(t.t);
System.out.println(t.b);
System.out.println( );
System.out.println( );
}
}

1 Answer

5 votes

Final answer:

The output of the Java program with default constructor will be '0', 'null' and 'false', corresponding to the default initialization values for int, class, and boolean types respectively, followed by two blank lines.

Step-by-step explanation:

The student is asking about the output of a Java program when values are printed from an instance of a class with uninitialized fields. In Java, when an instance of a class is created using the default constructor, class attributes are given default values if they are not explicitly initialized. For numerical types like int and float, this default value is 0 and 0.0, respectively. For a class type, the default value is null, and for a boolean, it is false. Given this, the output of the program would be:

  • 0
  • null
  • false

A couple of System.out.println() calls are left blank, which would simply result in printing empty lines in the output.

User Abbas Jafari
by
7.8k points