Suppose the input for number is 9 . What is the output from running the following program?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
int i;
boolean isPrime = true;
for (i = 2; i < number && isPrime; i++) {
if (number % i == 0) {
isPrime = false;
}
}
System.out.println("i is " + i);
if (isPrime)
System.out.println(number + " is prime");
else
System.out.println(number + " is not prime");
}
}
a. i is 3 followed by 9 is prime
b. i is 3 followed by 9 is not prime
c. i is 4 followed by 9 is prime
d. i is 4 followed by 9 is not prime
d The input number is 9 . isPrime is initialized to true. The loop tests if number is divisble by i for i from 2, 3, and so on. When i is 3, isPrime is false. The loop continuation condition becomes false. The loop is not finished. Before the loop continuation condition is checked, i++ increments i by 1 . So, output is that i is 4 and following by 9 is not prime. So, the correct answer is is executed n times for i from 1 to n. So, the correct answer is D.
You might also like to view...
Browsers convert percentage widths to ____ values, so some rounding of those values is bound to occur as a result.
A. pixel B. pica C. em D. point
All of the following is a type of shape EXCEPT:
A) starburst. B) symbol. C) arrow. D) text box.
List six objectives the analyst pursues in designing system output.
What will be an ideal response?
The original HTML specifications have been revised several times by the ____.
A. W3C B. B2C C. SSL D. P2P