Question

In: Computer Science

1. Using the following code identify the fault. 2. Using the following code indentify a test...

1. Using the following code identify the fault.

2. Using the following code indentify a test case that results in an error, but not a failure.

public static int lastZero (int[] x) {

//Effects: if x == null throw NullPointerException

//else return the index of the last 0 in x.

//Return -1 if 0 does not occur in x

for (int i = 0; i < x.length; i++)

{

if (x[i] == 0)

{

return i;

}

}

return -1;

}

//test: x = [0, 1, 0]

// Expected = 2

Solutions

Expert Solution

Here is code:

public static int lastZero (int[] x) {
//Effects: if x == null throw NullPointerException
//else return the index of the last 0 in x.
//Return -1 if 0 does not occur in x
if(x == null)
throw new NullPointerException("The x is null");
else
{
//loops from last index to 0th index
for (int i = x.length - 1; i >= 0; i--)
{
if (x[i] == 0)
{
return i;
}
}
return -1;
}
}

Sample test case to run:

public class testCase {
public static void main(String []args){
int[] a = {0,1,0};
System.out.println(lastZero(a));
}
public static int lastZero (int[] x) {
//Effects: if x == null throw NullPointerException
//else return the index of the last 0 in x.
//Return -1 if 0 does not occur in x
if(x == null)
throw new NullPointerException("The x is null");
else
{
//loops from last index to 0th index
for (int i = x.length - 1; i >= 0; i--)
{
if (x[i] == 0)
{
return i;
}
}
return -1;
}
}
}

Output:

2


Related Solutions

In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis, 4. test...
In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis, 4. test statistic, 5. p-value, and 6. conclusion. Researchers examined the effect of phosphate supplementation on bone formation in six healthy adult dogs. For each dog, bone formation was measured twice: once after 12 weeks of phosphate supplementation and once after a 12-week control period. The results in percent bone growth are shown below. Do the data provide evidence that phosphate supplementation significantly stimulates bone formation?...
Using the values that you found in numbers 1 and 2, perform the following hypothesis test....
Using the values that you found in numbers 1 and 2, perform the following hypothesis test. At a 10% significance level, test the claim that the women’s mean completion time is greater than ___________ (the men’s mean completion time). Ho: ___µ ≤ 27.914________   Ha: __µ > 27.914 (Claim)___ Label the claim. (6pts) What type of test will you use? (Z-Test, T-test, or 1-ProZTest) ______ (4 pts) Where is the rejection region? (Left-tailed, Right-tailed, Two-tailed test) (4 pts) Find the critical...
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t...
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t b[]={b0, b1, b2}; // They are the last three digits of your A# uint8_t* c=b; uint8_t* d=&a;
Essay: Using the following problem, 1) apply the Pillow Method to view the issue. 2) Identify...
Essay: Using the following problem, 1) apply the Pillow Method to view the issue. 2) Identify each position and explain each position clearly in relation to the scenario. 3) Explain how an expanded view of the situation might affect the communication in this relationship. Problem:You and friend have talked about moving to another city together after college graduation. While planning where to live during your last year, you discover that your friend is insisting on living in the expensive parts...
Edit question In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis,...
Edit question In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis, 4. test statistic, 5. p-value, and 6. conclusion. A researcher took a sample of the grade point averages for students in his class. For 25 students, the standard deviation of grade points was 0.65, and the mean was 2.89. In the previous semester his class had an average of 2.95. Is there evidence that his current class is significantly different from his previous class?...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine you have a rotary combination lock with many dials. Each dial has the digits 0 - 9. At any point in time, one digit from each dial is visible. Each dial can be rotated up or down. For some dial, if a 4 is currently visible then rotating the dial up would make 5 visible; rotating the dial down would make 3 visible. When...
Calculate the test statistic, x^(2), for a chi-squared test for association using the following table. The...
Calculate the test statistic, x^(2), for a chi-squared test for association using the following table. The following data represents the observed values and expected values of the eating habits of runners and swimmers for a random sample of adult athletes. Prefer to eat before workout. Prefer to eat after workout. Total Runner 68 121 189 Swimmer 73 128 201 Total 141 249 390
Test the following hypotheses by using the χ 2 goodness of fit test. H 0: p...
Test the following hypotheses by using the χ 2 goodness of fit test. H 0: p A = 0.2, p B = 0.4, and p C = 0.4 Ha: The population proportions are not p A = 0.2 , p B = 0.4 , and p C = 0.4 A sample of size 200 yielded 60 in category A, 120 in category B, and 20 in category C. Use  = .01 and test to see whether the proportions are as stated...
Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them...
Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them from a simple interactive main() function using a menu, with different values used to select the choice of function. Overall, you should have one C program (call it Lab1.c) containing one main() function and 5 other functions, where the functions are called based on an interactive user menu. The program should contain a loop that permits users to enter a new choice of function...
Identify four benefits required by no-fault laws. Identify two types of programs that provide automobile insurance...
Identify four benefits required by no-fault laws. Identify two types of programs that provide automobile insurance for high-risk drivers. How do the activities of insurers of high-risk drivers in the voluntary market differ from the activities of insurers in the residual market? What role does a state joint underwriting association (JUA) serve with regard to rates, policy forms and claim settlement for high-risk drivers? Explain the relationship between competition and regulatory monitoring of insurance rates and how that monitoring is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT