nerdexam
Oracle

1Z0-803 · Question #195

Given the code fragment: What is the result?

The correct answer is E. Values are : [EE, ME, SE, EE]. This question evaluates understanding of ArrayList manipulation, specifically adding elements to the end and inserting elements at a specific index.

Working with Selected Classes from the Java API

Question

Given the code fragment:

What is the result?

Exhibit

1Z0-803 question #195 exhibit

Options

  • AValues are : [EE, ME]
  • BValues are : [EE, EE, ME]
  • CValues are : [EE, ME, EE]
  • DValues are : [SE, EE, ME, EE]
  • EValues are : [EE, ME, SE, EE]

How the community answered

(32 responses)
  • A
    3% (1)
  • B
    9% (3)
  • D
    3% (1)
  • E
    84% (27)

Why each option

This question evaluates understanding of `ArrayList` manipulation, specifically adding elements to the end and inserting elements at a specific index.

AValues are : [EE, ME]

This output would occur if only 'EE' and 'ME' were added, implying fewer operations or elements than the correct answer.

BValues are : [EE, EE, ME]

This output sequence does not match the result of standard `ArrayList` add operations leading to the correct answer; it suggests an extra 'EE' at the beginning.

CValues are : [EE, ME, EE]

This output sequence implies a different order of element additions or insertions, specifically placing the last 'EE' at index 2, which does not match the operations yielding the correct answer.

DValues are : [SE, EE, ME, EE]

This output sequence includes 'SE' at the very beginning and implies a different order or set of operations, which is inconsistent with achieving the correct result.

EValues are : [EE, ME, SE, EE]Correct

The `ArrayList.add(element)` method appends 'EE' then 'ME' to the list. Then, `list.add(2, "SE")` inserts 'SE' at index 2, shifting subsequent elements, resulting in `[EE, ME, SE]`. Finally, `list.add("EE")` appends the last 'EE', producing the final list `[EE, ME, SE, EE]`.

Concept tested: Java ArrayList add operations

Source: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-int-E-

Topics

#ArrayList#collections#array manipulation

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice