Question: 1. The variable p holds an array of booleans. A program needs to determine the smallest i such that p[i] is true , or null

1. The variable p holds an array of booleans. A program needs to determine the smallest i such that p[i] is true, or null if p contains no trues. The programmer incorrectly wrote the following:

Integer result = null ;

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

if ( p [ i ]) {

result = i ;

}

}

Describe a situation where this code gives an incorrect result.

2. One definition of a primality is that a integer y is prime if there is no integer x in the range 1 < x < y such that y % x = 0. Write a loop that, given an integer in the variable candidate, sets the boolean variable candidateIsPrime to true if candidate is prime, but to false otherwise.

3. Suppose that assets and liabilities are arrays of doubles, possibly of different lengths. Write code to set outOfDebt to true if the total of the assets array is at least the total of the liabilities, but to false otherwise.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!