0 votes
in Education by (54.6k points)
What is the output of the following Java program?

class Test   

{  

    int i;   

}  

public class Main   

{  

    public static void main (String args[])   

    {  

        Test test = new Test();   

        System.out.println(test.i);  

    }  

}

1 Answer

0 votes
by (54.6k points)

What is the output of the following Java program?

class Test   

{  

    int i;   

}  

public class Main   

{  

    public static void main (String args[])   

    {  

        Test test = new Test();   

        System.out.println(test.i);  

    }  

}  

The output of the program is 0 because the variable i is initialized to 0 internally. As we know that a default constructor is invoked implicitly if there is no constructor in the class, the variable i is initialized to 0 since there is no constructor in the class.

...