Java - Chapter 5 Quiz – Questions and
Answers| Latest Update
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
Analyze the following code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100000; i++) {
Scanner input = new Scanner(System.in);
sum += input.nextInt();
}
}
}
A. The program does not compile because the Scanner input = new Scanner(System.in);
statement is inside the loop.
B. The program compiles, but does not run because the Scanner input = new
Scanner(System.in); statement is inside the loop.
C. The program compiles and runs, but it is not efficient and unnecessary to execute the
Scanner input = new Scanner(System.in); statement inside the loop. You should move the
statement before the loop.
, D. The program compiles, but does not run because there is not prompting message for
entering the input ✔️✔️C
What is the output of the following code?
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4 ✔️✔️E
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}
A. 6 3 0
B. 6 3
C. 3 0
D. 3 0 -3
Answers| Latest Update
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
Analyze the following code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100000; i++) {
Scanner input = new Scanner(System.in);
sum += input.nextInt();
}
}
}
A. The program does not compile because the Scanner input = new Scanner(System.in);
statement is inside the loop.
B. The program compiles, but does not run because the Scanner input = new
Scanner(System.in); statement is inside the loop.
C. The program compiles and runs, but it is not efficient and unnecessary to execute the
Scanner input = new Scanner(System.in); statement inside the loop. You should move the
statement before the loop.
, D. The program compiles, but does not run because there is not prompting message for
entering the input ✔️✔️C
What is the output of the following code?
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4 ✔️✔️E
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}
A. 6 3 0
B. 6 3
C. 3 0
D. 3 0 -3