Multiple choice technology programming languages

What will happen if you try to compile and run the following code? public class Q { public static void main(String argv[]){ int anar[]=new int[5]; System.out.println(anar[0]); } }

  1. Error: anar is referenced before it is initialized

  2. null

  3. 0

  4. 5

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

In Java, when you create an int array using 'new int[5]', all elements are automatically initialized to 0 (the default value for int primitives). Therefore, anar[0] prints 0. This is not an error, not null, and not 5 (the array length, not a value).

AI explanation

In Java, array elements are always automatically initialized to a default value when the array is created with 'new' — for numeric primitive types like int, that default is 0 (not null, which is only for reference types, and not left undefined). Here 'new int[5]' allocates 5 ints all set to 0, so anar[0] is 0 and the program compiles and runs cleanly, printing '0'. The 'referenced before initialized' error only applies to local variables, not array elements, so option A is wrong; null (a reference-type default) and 5 (the array's length, not an element's value) are also incorrect.