Courseiva
Java Basics and SyntaxmediumMultiple SelectObjective-mapped

Legal Ways to Declare and Initialize Arrays in Java

Which TWO of the following are legal ways to declare and initialize an array?

Quick Answer

Java gives two genuinely different ways to bring an array into existence, and recognizing both as legal (rather than assuming only one syntax is correct) is what this question is testing. Writing int[] arr = new int[3]; creates an array of a fixed size with every element set to its type's default value — zero for int — leaving the actual values to be assigned afterward. The alternative, int[] arr = new int[]{1,2,3};, skips specifying a size entirely and instead supplies the initializer list directly; the compiler infers the array's length from how many values appear inside the braces, and each element is populated with the given value immediately rather than a default. Both are legal, complete statements that declare and initialize an array in one line — they just serve different situations, one for when the size is known but values will be filled in later, the other for when the actual values are already known upfront. A shorthand form, int[] arr = {1,2,3};, is also legal but only at the point of declaration, not in a standalone assignment to an already-declared variable — a nuance worth knowing since it's a common source of a syntax error if used incorrectly.

⚠ Common exam trap

Oracle often tests the distinction between declaration and initialization, and the trap here is that candidates may think `int arr[];` (Option B) is sufficient because it declares an array, but the question explicitly requires both declaration and initialization, making it incomplete.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

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

It uses the valid syntax `int[] arr = new int[]{1,2,3};` which declares an array variable of type `int[]`, creates a new array object with an initializer list, and assigns it to the variable. This is a legal way to both declare and initialize an array in a single statement, as the array size is inferred from the number of elements in the initializer.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • int arr = {1,2,3};

    Why it's wrong here

    Invalid. Cannot assign array to int variable.

  • int arr[];

    Why it's wrong here

    Invalid as initialization. This is only a declaration.

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

    Why this is correct

    Valid. Array initializer syntax.

  • int[] arr = new int[3];

    Why this is correct

    Valid. Creates array with default zeros.

  • int[3] arr;

    Why it's wrong here

    Invalid syntax. Array size not allowed in declaration.

About these practice questions

One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on 1Z0-811

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer uses the following array initialization: int[] nums = new int[]{1, 2, 3}; Which of the following is true?

hard
  • A.This is invalid syntax.
  • B.The array is initialized with default values.
  • C.The array has length 3.
  • D.The array can only store numbers up to 3.

Why C: The array initialization `int[] nums = new int[]{1, 2, 3};` explicitly creates an array of length 3, containing the elements 1, 2, and 3. The syntax is valid and the array is not initialized with default values; it is initialized with the specified literal values.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.