Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Java | Arrays | Question 3

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article




class Test {
   public static void main(String args[]) {
     int arr[2];  
     System.out.println(arr[0]);
     System.out.println(arr[1]);
   }
}

(A)

0
0

(B)

garbage value
garbage value

(C) Compiler 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 Test {
   public static void main(String args[]) {
     int arr[5];   //Error
   }
}


Quiz of this Question

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads