78.6k views
1 vote
Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause runtime errors?

(choose more than one)

A. (1)
B. (2, "New York");
C. (2)
D. (2)
E. ()

User Gbjbaanb
by
9.1k points

1 Answer

1 vote

Final answer:

The methods (2, "New York") and (1) will cause runtime errors.

Step-by-step explanation:

The method (2, "New York") will cause a runtime error because there is no index 2 in the ArrayList x. The correct way to add an element to an ArrayList is by using the add() method. For example, to add the string "New York" at index 2, you would use x.add(2, "New York").

The method (1) will also cause a runtime error because parentheses are not used to call a method in Java. Instead of (1), it should be written as x.get(1) to retrieve the element at index 1 in the ArrayList x.

User Markus Michel
by
7.3k points