Open In App
Related Articles

Java | Class and Object | Question 2

Improve Article
Save Article
Like Article

Predict the output of following Java program

class Test {
  int i;
class Main {
  public static void main(String args[]) { 
      Test t = new Test(); 

(A) garbage value
(B) 0
(C) compiler error
(D) runtime error

Answer: (B)

Explanation: In Java, fields of classes and objects that do not have an explicit initializer and elements of arrays are automatically initialized with the default value for their type (false for boolean, 0 for all numerical types, null for all reference types). Local variables in Java must be definitely assigned to before they are accessed, or it is a compile error.

Quiz of this Question

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads