What is the output after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number && isPrime; i++) {
if (number % i == 0) {
isPrime = false;
}
}
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
d 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. The loop continuation condition now becomes false. The loop ends. 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 i++ increments i by 1 in the action-after-each-iteration. So, the output is that i is 6 and isPrime is false. The correct anwer is D.
You might also like to view...
Assuming that Grades.h is found in the current directory and the iostream header file is found in the C++ Standard Library header file directory, which of the following preprocessor directives will fail to find its desired header file?
a. #include
Access data CANNOT be copied directly into PowerPoint; it must be linked
Indicate whether the statement is true or false
A(n) ____________________ tunnel is a communications path that makes use of Internet-based hosts and servers to conduct data from one network station to another, just like any other TCP/IP data transmission.
Fill in the blank(s) with the appropriate word(s).
Under which pane can you find triggers?
A) View B) Format C) Animation D) Slide Show