Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Easy
Question 35
Which of the following statements is correct for the code below?
//LoopCheck.java
public class LoopCheck {
public static void main(String[] args) {
final boolean flag;
flag = true;
while(flag) {
System.out.println("Good Evening!");
}
}
}
-
A
Compilation error.
-
B
It will print "Good Evening!" once.
-
C
Program compiles and executes successfully but produces no output.
-
D
Infinite loop.
Reveal correct answer
Correct answer: C
Explanation
final boolean flag; flag = true; does not make flag a compile-time constant. The compiler does not know flag's value at compile-time, so it allows the syntax. At runtime, the boolean expression of the while loop is true, so the loop executes indefinitely, printing "Good Evening!" repeatedly.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
