The Wayback Machine - https://web.archive.org/web/20240204104940/https://www.geeksforgeeks.org/java-arrays-question-3/
Open In App
Related Articles

Java | Arrays | Question 3

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report




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

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads