Last Updated : 10 Apr, 2024
class GFG 
{ 
   public static void main(String args[]) 
   { 
     int arr[2];   
     System.out.println(arr[0]); 
     System.out.println(arr[1]); 
   } 
}  

Pick the correct option for the above program
(A) 0
0
(B) Garbage value
Garbage value
(C) Compile error
(D) Exception


Answer: (C)

Explanation:
In Java, it is not allowed to put the size of the array in the declaration because an array declaration specifies only the element type and the variable name. The size is specified when you allocate space for the array. Even the following simple program won’t compile.

class GFG {
   public static void main(String args[]) {
     int arr[5];   //Error
   }
}



Share your thoughts in the comments