Bird
0
0

Which of the following is a correct way to initialize an integer array with values 1, 2, 3 in Java?

easy📝 Syntax Q3 of 15
Java - Arrays
Which of the following is a correct way to initialize an integer array with values 1, 2, 3 in Java?
Aint[] arr = new int[3]{1, 2, 3};
Bint arr[] = new int{1, 2, 3};
Cint arr[] = {1, 2, 3};
Dint[] arr = {1; 2; 3};
Step-by-Step Solution
Solution:
  1. Step 1: Understand array initialization syntax

    In Java, you can initialize an array with values using curly braces without specifying size explicitly.
  2. Step 2: Check each option

    int arr[] = {1, 2, 3}; uses correct syntax: int arr[] = {1, 2, 3}; int[] arr = new int[3]{1, 2, 3}; incorrectly combines size and initializer. int arr[] = new int{1, 2, 3}; misses brackets after new int. int[] arr = {1; 2; 3}; uses semicolons instead of commas.
  3. Final Answer:

    int arr[] = {1, 2, 3}; -> Option C
  4. Quick Check:

    Array initialization with values = int arr[] = {1, 2, 3}; [OK]
Quick Trick: Use commas inside braces to list array values [OK]
Common Mistakes:
  • Mixing size declaration with initializer list
  • Using semicolons instead of commas
  • Omitting square brackets after new keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes