75.2k views
1 vote
Which of the following is not a correct way to initialize an array?

1) int[] arr = new int[5];
2) int[] arr = {1, 2, 3, 4, 5};
3) int[] arr = new int[]{1, 2, 3, 4, 5};
4) int[] arr = new int[5]{1, 2, 3, 4, 5};

User Phunehehe
by
8.4k points

1 Answer

3 votes

Final answer:

The correct options for initializing an array in Java explained.

Step-by-step explanation:

The correct way to initialize an array in Java is:

  1. int[] arr = new int[5];
  2. int[] arr = {1, 2, 3, 4, 5};
  3. int[] arr = new int[]{1, 2, 3, 4, 5};

The incorrect way to initialize an array is:

  • int[] arr = new int[5]{1, 2, 3, 4, 5};

Option 4 is not valid because when using the new keyword to initialize an array, you cannot provide explicit values inside the square brackets.

User Effreety
by
7.9k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.