nerdexam
Oracle

1Z0-811 · Question #18

Given the code fragment: // line n1 num = new int[10]; Which code fragment can be inserted at line n1 to enable the code to compile?

The correct answer is B. int[] num. Option B (int[] num;) is correct because Java requires a variable to be declared before it can be assigned a value - num = new int[10]; is an assignment statement, so num must already exist as an int[] type reference, which int[] num; provides. Option A (new int num[]) is…

Arrays and Logic

Question

Given the code fragment: // line n1 num = new int[10]; Which code fragment can be inserted at line n1 to enable the code to compile?

Options

  • Anew int num[];
  • Bint[] num;
  • Cint[10] num;
  • Dint num[10];

How the community answered

(27 responses)
  • A
    4% (1)
  • B
    96% (26)

Explanation

Option B (int[] num;) is correct because Java requires a variable to be declared before it can be assigned a value - num = new int[10]; is an assignment statement, so num must already exist as an int[] type reference, which int[] num; provides.

Option A (new int num[]) is invalid syntax entirely - new is used for object instantiation, not variable declaration. Option C (int[10] num) is wrong because Java does not allow a size inside the brackets during declaration; the size is only specified during instantiation (the new side). Option D (int num[10]) is similarly invalid - while int num[] is legal C-style syntax in Java, placing a size (10) inside the brackets on the declaration side is never allowed.

Memory tip: Think of it as a two-step process - declare the type, then allocate the memory. The brackets in the declaration (int[] num) are always empty; the size only appears on the new side (new int[10]).

Topics

#Array declaration#Variable declaration syntax#Type system#Data types

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice