59.5k views
4 votes
(TCOS 1-6) Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?

A. x.add("Chicago")
B. x.add(2, "Chicago")
C. x.add(0, "Chicago")
D. x.add(1, "Chicago")

User Aiguo
by
7.1k points

1 Answer

4 votes

Answer:

The correct method that will cause the list to become [Beijing, Chicago, Singapore] is B.

Step-by-step explanation:

x.add("Chicago") will add "Chicago" to the end of the list, resulting in [Beijing, Singapore, Chicago]

x.add(2, "Chicago") will add "Chicago" at index 2, resulting in [Beijing, Singapore, Chicago]

x.add(0, "Chicago") will add "Chicago" at the beginning of the list, resulting in [Chicago, Beijing, Singapore]

x.add(1, "Chicago") will add "Chicago" at index 1, resulting in [Beijing, Chicago, Singapore]

Therefore, option B is the correct one.

User Nico Rikken
by
8.0k points