What is the output after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
System.out.println("i is " + i + " isPrime is " + isPrime);
a. i is 5 isPrime is true
b. i is 5 isPrime is false
c. i is 6 isPrime is true
d. i is 6 isPrime is false
b The code tests if number is prime by dividing the number with possible divisors 2, 3, 4, and so on. Initially, isPrime is set to true. Once a divisor is found (i.e., number % i == 0 is true), isPrime is set to false and the break statement is then executed to exit the loop. If no divisor is found after the loop is finished, isPrime remains true. Since number is 25, number % i == 0 is true when i is 5 . In this case, isPrime is set to false and the loop exits. So, the output is taht i is 5 and isPrime is true. The correct answer is B.
You might also like to view...
A ________ indicates a significant accomplishment within a project and includes a summary of the completed task along with the target date
a. Milestone b. Value c. Stake d. Scope
List SQL statements allowed and not allowed in a PL/SQL block.
What will be an ideal response?