Below are four faulty programs. Each includes a test case that results in failure. Answer the following questions (in the next slide) about each program.
Questions
Identify the fault.If possible, identify a test case that does not execute the fault. (Reachability)If possible, identify a test case that executes the fault, but does not result in an error state.If possible identify a test case that results in an error, but not a failure.public intfindLast(int[] x, inty) {//Effects: If x==null throwNullPointerException// else return the index of the last element// in x that equals y.// If no such element exists, return -1for (inti=x.length-1; i> 0; i--){if (x[i] == y){return i;}}return -1;}// test: x=[2, 3, 5]; y = 2// Expected = 0
(a)Fault: i > 0, 导致x[0]无法被遍历,正确应该为i >= 0。
(b)Test: x = null; y = 2
(c)Test: x = [2,3,5]; y = 5. Excepted = 2
(d)Test: x = [2,3,5]; y = 2. Excepted = -1
public static intlastZero(int[] x) {//Effects: if x==null throwNullPointerException// else return the index of the LAST 0 in x.// Return -1 if 0 does not occur in xfor (inti= 0; i< x.length; i++){if (x[i] == 0){return i;}} return -1;}// test: x=[0, 1, 0]// Expected = 2
(a)Fault: 遍历顺序错误,正确结果应该为从后往前遍历,即 for(int i = x.length - 1; i >= 0; i --)
(b)Test: x = null
(c)Test: x = [2,1,0] Excepted = 2
(d)Test: x = [0,1,0] Excepted = 2, but result = 0