nerdexam
Oracle

1Z0-803 · Question #248

Given the code fragment: List colors = new ArrayList(); colors.add("green"); colors.add("red"); colors.add("blue"); colors.add("yellow"); colors.remove(2); colors.add(3,"cyan"); System.out.print(color

The correct answer is A. [green, red, yellow, cyan]. First the list[green, red,blue, yellow]is build. The blue element is removed: [green, red,yellow] Finally the element cyan is added at then end of the list (index 3). [green, red, yellow, cyan]

Working with Selected Classes from the Java API

Question

Given the code fragment:

List colors = new ArrayList(); colors.add("green"); colors.add("red"); colors.add("blue"); colors.add("yellow"); colors.remove(2); colors.add(3,"cyan"); System.out.print(colors); What is the result?

Options

  • A[green, red, yellow, cyan]
  • B[green, blue, yellow, cyan]
  • C[green, red, cyan, yellow]
  • DAm IndexOutOfBoundsException is thrown at runtime

How the community answered

(47 responses)
  • A
    83% (39)
  • B
    4% (2)
  • C
    11% (5)
  • D
    2% (1)

Explanation

First the list[green, red,blue, yellow]is build. The blue element is removed: [green, red,yellow] Finally the element cyan is added at then end of the list (index 3). [green, red, yellow, cyan]

Topics

#ArrayList#collections#list operations#index-based operations

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice