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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started