Question: Consider the following method definition. The method isReversed is intended to return true if firstList and secondList contain the same elements but in reverse order,

Consider the following method definition. The method isReversed is intended to return true if firstList and secondList contain the same elements but in reverse order, and to return false otherwise.

/** Precondition: firstList.size() == secondList.size() */

public static boolean isReversed(ArrayList firstList,

ArrayList secondList)

{

for (int j = 0; j < firstList.size() / 2; j++)

{

if (firstList.get(j) != secondList.get(secondList.size() - 1 - j))

{

return false;

}

}

return true;

}

The method does not always work as intended. For which of the following inputs does the method NOT return the correct value?

A. When firstList is {1, 3, 3, 1} and secondList is {1, 3, 3, 1}

B. When firstList is {1, 3, 3, 1} and secondList is {3, 1, 1, 3}

C. When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1}

D. When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 1}

E. When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 3}

2)In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList.

int j = 0;

while (j < myList.size())

{

if (myList.get(j) == 0)

{

myList.remove(j);

}

j++;

}

The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result?

A. {0, 1, 2, 3}

B. {0, 1, 0, 2}

C. {1, 0, 0, 2}

D. {1, 2, 3, 0}

E. {1, 2, 3, 4}

Step by Step Solution

3.47 Rating (163 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

answer for 1 E answer for 2 ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related General Management Questions!