49.4k views
4 votes
Consider the following method that is intended to modify its parameter nameList by replacing all occurrences of name with newValue.

public void replace (ArrayList nameList, String name,
String newValue)
{
for (int j = 0; j < nameList.size(); j++)
{
if (/*expression */)
{
nameList.set(j, newValue);
}
}
}

Which of the following can be used to replace /* expression */ os that replace will work as intended?

a. nameList[j].equals(name)
b. nameList.get(j).equals(name)
c. nameList.get(j) == name
d. nameList[j] == name
e. nameList.remove(j)

User Thlim
by
4.6k points

1 Answer

3 votes

Answer:

The answer is "Option b"

Step-by-step explanation:

In this question, only the choice b is correct because, in the replace method, it holds the value of two string variables and an array list, in its parameter and defined a for loop, that counts array list value and uses if block to check an array list value equal to a string value. If the condition is true, it uses the set method to hold a value.

User Jurgispods
by
5.1k points