Consider the findLast method and answer the questions.
You may make a copy of a worksheet and complete this activity, or write your answer on paper(s) or your computer.
/**
* Find last index of element
*
* @param x array to search
* @param y value to look for
* @return last index of y in x; -1 if absent
* @throws NullPointerException if x is null
*/
line 1 public static int findLast(int[] x, int y)
line 2 {
line 3 if (x == null)
line 4 throw new NullPointerException();
line 5 for (int i=x.length-1; i>0; i--)
line 6 {
line 7 if (x[i] == y)
line 8 return i;
line 9 }
line 10 return -1;
line 11 }
// test: x = [2, 3, 5]; y = 2;
// expected = 0
The for-loop should include the 0 index for (int i=x.length-1; i>=0; i--)
Input: x = null; y = 3 Expected output: NullPointerException Actual output: NullPointerException
Input: x = [2, 3, 5]; y = 3 Expected output: 1 Actual output: 1Let's look at the state
< x={2, 3, 5}, y=3, PC=[if (x==null), (line 3)] >
< x={2, 3, 5}, y=3, PC=[i=x.length-1, (line 5)] >
< x={2, 3, 5}, y=3, i=2, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=3, i=2, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=3, i=2, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=3, i=1, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=3, i=1, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=3, i=1, PC=[return i, (line 8)] >
A correct program
< x={2, 3, 5}, y=3, PC=[if (x==null), (line 3)] >
< x={2, 3, 5}, y=3, PC=[i=x.length-1, (line 5)] >
< x={2, 3, 5}, y=3, i=2, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=3, i=2, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=3, i=2, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=3, i=1, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=3, i=1, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=3, i=1, PC=[return i, (line 8)] >
The fault location is reached (i.e., the fault is executed).
The program stops without state infection.
Input: x = [2, 3, 5]; y = 9 Expected output: -1 Actual output: -1Let's look at the state
< x={2, 3, 5}, y=9, PC=[if (x==null), (line 3)] >
< x={2, 3, 5}, y=9, PC=[i=x.length-1, (line 5)] >
< x={2, 3, 5}, y=9, i=2, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=9, i=2, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=9, i=2, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=9, i=1, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=9, i=1, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=9, i=1, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=9, i=0, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=9, PC=[return -1, (line 10)] >
A correct program
< x={2, 3, 5}, y=9, PC=[if (x==null), (line 3)] >
< x={2, 3, 5}, y=9, PC=[i=x.length-1, (line 5)] >
< x={2, 3, 5}, y=9, i=2, PC=[i>=0, (line 5)] >
< x={2, 3, 5}, y=9, i=2, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=9, i=2, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=9, i=1, PC=[i>=0, (line 5)] >
< x={2, 3, 5}, y=9, i=1, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=9, i=1, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=9, i=0, PC=[i>=0, (line 5)] >
< x={2, 3, 5}, y=9, i=0, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=9, i=0, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=9, i=0, PC=[i>=0, (line 5)] >
< x={2, 3, 5}, y=9, i=0, PC=[return -1, (line 10)] >
First error state is just after evaluating 0 > 0 of the second iteration Input: x = [2, 3, 5]; y = 2 Expected output: 0 Actual output: -1Let's look at the state
< x={2, 3, 5}, y=2, PC=[if (x==null), (line 3)] >
< x={2, 3, 5}, y=2, PC=[i=x.length-1, (line 5)] >
< x={2, 3, 5}, y=2, i=2, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=2, i=2, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=2, i=2, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=2, i=1, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=2, i=1, PC=[if (x[i]==y), (line 7)] >
< x={2, 3, 5}, y=2, i=1, PC=[i--, (line 5)] >
< x={2, 3, 5}, y=2, i=0, PC=[i>0, (line 5)] >
< x={2, 3, 5}, y=2, PC=[return -1, (line 10)] >
First error state is just before return -1
CC-BY-NC-SA 4.0 license.