Home /
Expert Answers /
Computer Science /
q2-study-the-shown-code-below-then-answer-the-following-questions-public-static-int-oddorpos-in-pa123
(Solved):
Q2) Study the shown code below then answer the following questions. public static int oddOrPos (in ...
Q2) Study the shown code below then answer the following questions. public static int oddOrPos (int[] x) { // Effects: if x is null throw NullPointerException // else return the number of elements in x that. are either odd or positive (or both) int count = 0; for (int i = 0; i < x.length; i++) { if (x[i]%2 == 1 || x[i] > 0) { count++; } } return count; // test: x= [-3, -2, 0, 1, 4] Expected = 3 1) Explain what is wrong with the given code. Describe the fault precisely by proposing a modification to the code. 2) If possible, give a test case that does not execute the fault. If not, briefly explain why not. 3) If possible, give a test case that executes the fault, but does not result in an error state. If not, briefly explain why not. 4) If possible give a test case that results in an error, but not a failure. If not, briefly explain why not. Hint: Don't forget about the program counter. 5) For the given test case, describe the first error state. Be sure to describe the complete state. 6) Write the proper JUnit test methods for the previous questions.
Ans1) there is no fault in left code but there is fault in second code for (int i =0;ifault is in this line it should not return when first 0 is found , i it should