1 Public intFindLast (int[] x,inty) {2 //effects:if x==null thro nullpointerexception3 //else return the index of the last element4 //In x that equals Y.5 //If No such element exists, return-16 for(intI=x.length-1; I> 0;i--)7 {8 if(X[i] = =y)9 {Ten returni; One } A } - return-1; -}<br>//test:x=[2,3,5];y=2<br>//expected = 0<br>
A) Identify the fault.
In the For loop, the condition i>0 should be changed to i>=0, otherwise the first element of the array will not be traversed.
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=[2,3,5]; Y=5
Expected:2; Actually:2 equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[2,3,5]; Y=6
Expected:-1; Actually:-1 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null; Y=1
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, Identifu the first error state. Be sure to describe the complete state.
For a given test case, we can see that the program does not execute to the address labeled 0 in the array, so the final result state gives a value of-1, and the expected 0 inconsistency
f) Fix The fault and verify that the given test now produes the expected output.
To change the i>0 in the For loop to i>=0, the expected result and the actual result are all 0.
1 Public Static intLastzero (int[] x) {2 //effects:if x==null Throw nullpointerexpection3 //else return the index of the last 0 in X4 //Return-1 If 0 does not occur in X5 6 for(inti=0; i<x.length; i++)7 {8 if(X[i] ==0)9 {Ten returni; One } A } - return-1; - } the //test:x=[0,1,0] - //Expected:2
A) Identify the fault.
The fault code is that the for loop is looped back from the previous loop, and a value equal to 0 returns the array subscript, which is the position of the first 0 in the array. Inconsistent with the requirements of the function
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=null;
Expected:nullpointerexception; Actually:nullpointerexception equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[2,3,5];
Expected:-1; Actually:-1 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null;
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, identify the first error state. Be sure to describe the complete state.
For a given test case x=[0,1,0], the function finds equal to 0 when accessing the first element, so it returns the array subscript 0 of the first element directly, and the expected 2 inconsistency.
f) Fix The fault and verify that the given test now produes the expected output.
Just change the For loop to for (int i =x.length-1;i>=0;i--)
So the expected results and the actual results are all 2.
1 Public intCountpositive (int[] x) {2 //effects:if x==null Throw nullpointerexception3 //else return the number of4 //positive elements in x5 6 intCount = 0;7 for(inti = 0; i<x.length; i++)8 {9 if(x[i]==0)Ten { Onecount++; A } - } - returncount; the } - - //test:x=[-4,2,0,2] - //expected = 2
A) Identify the fault.
The function is to calculate the number of positive numbers in an array, and if the x[i]>=0 in the IF condition, the number of non-negative numbers to be judged should be changed to x[i]>0
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=null;
Expected:nullpointerexception; Actually:nullpointerexception equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[-2,3,5];
Expected:2; Actually:2 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null;
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, identify the first error state. Be sure to describe the complete state.
For a given test case x=[-4,2,0,2], when the third element 0 is accessed, the value of Count is incremented by 1, so that the last actual value is 3 instead of the expected 2, which is inconsistent.
f) Fix The fault and verify that the given test now produes the expected output.
If the x[i]>=0, judging the number of non-negative numbers, should be changed to x[i]>0
So the expected results and the actual results are all 2.
1 Public Static intOddorpos (int[] x) {2 //effects:if x==null Throw nullpointerexception3 //else return the number of elements in X that4 //is either off or positive (or both)5 6 intcount= 0;7 for(inti = 0;i < x.length;i++)8 {9 if(X[i]% 2 ==1 | | x[i] >0)Ten { Onecount++; A } - } - returncount; the } - //test:x=[-3,-2,0,1,4] - //expected = 3
A) Identify the fault.
If the condition of X[i]% 2 ==1 will fail, function is to determine the number of positive and odd numbers, and this condition does not take into account negative conditions, so will miss the odd number of negative numbers.
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=null;
Expected:nullpointerexception; Actually:nullpointerexception equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[2,3,5];
Expected:2; Actually:2 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null;
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, identify the first error state. Be sure to describe the complete state.
For the determination of the first number, the remainder 2 is not equal to 1, so the judgment is not odd so that the actual value and the expected value are inconsistent
f) Fix The fault and verify that the given test now produes the expected output.
Change X[i]% 2 ==1 to x[i]% 2! = 0
So the expected results and the actual results are all 2.
Software Test section1.2. Exercises third question