Which one of the following will declare an array and initialize it with five numbers?

  1. Array a = new Array(5);

  2. int [] a = {23,22,21,20,19};

  3. int a [] = new int[5];

  4. int [5] array;


Correct Option: B
Explanation:

To declare an array and initialize it with five numbers, the user needs to know the syntax for declaring and initializing an array in Java.

Option A is incorrect because the Array class does not have a constructor that takes an integer as an argument, and thus this statement will not create an array of five elements.

Option B is correct because it initializes an integer array a with 5 elements and assigns the values 23, 22, 21, 20, and 19 to the array.

Option C is also correct because it declares an integer array a with 5 elements, but it does not initialize the array with any values.

Option D is incorrect because the syntax int [5] array is not valid in Java. The correct syntax should be int[] array = new int[5] which creates an integer array array with 5 elements and initializes all elements to 0.

Therefore, the correct answer is:

The Answer is: B. int [] a = {23,22,21,20,19};

Find more quizzes: