nerdexam
Oracle

1Z0-819 · Question #53

Given var data = new ArrayList<>(); data.add(10*10); data.add(30); data.add("Market Road"); data.add(1, 2, 3); data.remove(2); data.remove(null); system.out.print(data); What is the output?

The correct answer is D. An exception is thrown at run time. Option D is correct because the code contains two compile-time errors that prevent it from ever running. First, data.add(1, 2, 3) is invalid - ArrayList.add() only accepts one or two arguments (add(element) or add(index, element)), so passing three arguments has no matching…

Working with Arrays and Collections

Question

Given var data = new ArrayList<>(); data.add(10*10); data.add(30); data.add("Market Road"); data.add(1, 2, 3); data.remove(2); data.remove(null); system.out.print(data); What is the output?

Options

  • A[Market Road, 1000]
  • B[100, Market Road]
  • C[Market Road, 100, 1, 2, 3]
  • DAn exception is thrown at run time.

How the community answered

(45 responses)
  • A
    7% (3)
  • B
    4% (2)
  • C
    16% (7)
  • D
    73% (33)

Explanation

Option D is correct because the code contains two compile-time errors that prevent it from ever running. First, data.add(1, 2, 3) is invalid - ArrayList.add() only accepts one or two arguments (add(element) or add(index, element)), so passing three arguments has no matching method signature and fails to compile. Second, system.out.print uses a lowercase s, but System is a class name and must be capitalized in Java.

Options A, B, and C are all distractors built on what the list might look like if certain lines ran correctly - but since the code never compiles, no list state is ever produced or printed.

Memory tip: When you see an ArrayList method call with an unusual number of arguments, immediately check the API signatures - add() only takes 1 or 2 args. Also scan for system vs System; Java is case-sensitive and System (capital S) is the only valid reference to the standard output class. Spotting compile errors before tracing list state will save you time on these questions.

Topics

#ArrayList API#method signatures#method overloading#exception handling

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice