Consider the following method definition. The method isReversed is intended to return true if firstList and secondList
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, 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}
Introduction to Data Mining
ISBN: 978-0321321367
1st edition
Authors: Pang-Ning Tan, Michael Steinbach, Vipin Kumar