Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Below are four faulty programs. Each includes a test case that results in fai

ID: 3846779 • Letter: 3

Question

3. Below are four faulty programs. Each includes a test case that results in fail- ure. Answer the following questions about each program public static int lastzero (int[] x f public int findLast (int[] x, int y) Effects: If x null throw NullPointerException Effects: if x null throw NullPointerException else return the index of the last element else return the index of the LAST 0 in x. in x that equals y Return -1 if 0 does not occur in x If no such element exists, return -1 for (int i x length-1; i 0; i--) for (int i 0; i x.length; i++) if (x[i] 2- y) if (x 0) return i return i -1 return -1; return. test: x 2, 3, 51; y 2 test: x 10, 1, 01 Expected 0 Expected 2 public int countPositive (int[J x) public static int oddorPos(int[] x) H /Effects: If x null throw NullPointerException Effects: if x null throw NullPointerException. else return the number of else return the number of elements in x that positive elements in x are either odd or positive (or both) int count 0 int count 0 for (int i 0; i

Explanation / Answer

(a) Fault in findLast Program : for(int i=x.length; i>=0; i--) //condition should be >=

Fault in lastZero Program : for loop should be : for(int i=x.length; i>=0; i--)

Fault in countPositive : if condition should be : if(x[i]>0)

Fault in oddOrPos : if condition should be : if(x[i]%2 !=0 || x[i]>0)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote