Courseiva
Arrays and MethodseasyMultiple ChoiceObjective-mapped

1Z0-811 Arrays and Methods Practice Question

A method is declared as 'public void printElements(int... numbers)'. Which invocation will cause a compilation error?

⚠ Common exam trap

The trap here is that candidates mistakenly think an array initializer like `{1,2,3}` can be used anywhere an array is expected, but in Java it is only valid in declarations or with `new` — not as a standalone method argument.

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

printElements({1,2,3});

The syntax `{1,2,3}` is an array initializer that can only be used in a variable declaration or as part of an array creation expression (e.g., `new int[]{1,2,3}`). It cannot be passed directly as an argument to a varargs method. The varargs parameter `int... numbers` expects either a sequence of `int` values or an `int[]` array reference, but not an anonymous array 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.

  • printElements({1,2,3});

    Why this is correct

    The provided invocation `printElements({1,2,3});` will cause a compilation error. Array initializer syntax, such as `{1,2,3}`, is exclusively permitted during the declaration of an array (e.g., `int[] arr = {1,2,3};`). It cannot be used directly as a standalone expression or as an argument within a method call. Although the `int... numbers` varargs parameter internally accepts an `int[]`, the compiler requires either individual `int` values or an already instantiated `int[]` object. The given syntax is not a valid way to create an anonymous array for method arguments.

  • printElements(1, 2, 3);

    Why it's wrong here

    Valid varargs invocation with multiple ints.

  • printElements();

    Why it's wrong here

    Valid varargs invocation with zero arguments.

  • printElements(new int[]{1,2,3});

    Why it's wrong here

    Valid varargs invocation with an array.

About these practice questions

This 1Z0-811 question is part of Courseiva's 481-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.