QUESTION 64
1. Which XXX will generate the following output?
LIST 3 5 8 11 12 33 46 88
Enter a value: 0 was not found.
public class LinearSearch {
public static int linearSearch(int[] list, int listSize, int key) {
int i;
for (i=0; i<listSize; ++i) {
if (list[i]== key) {
return i;
}
}
return XXX;
}
public static void main(String[] args) {
Scanner scnr= new Scanner(System.in);
int list(3, 5, 8, 11, 12, 33, 46, 88);
final int LIST_SIZE = 8;
int i;
int key;
int keyIndex;
System.out.print("LIST");
for (i=0; i < LIST_SIZE; ++i) {
System.out.print(list[i] + " ");
}
System.out.println();
System.out.print("Enter a value: ");
key = scnr.nextInt();
keyIndex = linearSearch(list, LIST_SIZE, key);
if (keyIndex == -1) {
System.out.println(key + " was not found.");
} else {
System.out.println("Found " + key + " at index " + keyIndex + ".");
}
}
}
1
-1
0
2
4 points