while Construct QuizJ8 Home « while Construct Quiz

Fundamentals Quiz 14

The quiz below tests your knowledge of the material learnt in Fundamentals - Lesson 15 - while Construct.

Question 1 : We can nest a while construct in any other construct?
- We CAN nest a <code>while</code> construct in any other construct including another <code>while</code> construct.
Question 2 :A while loop is always tested for true after each iteration of the loop?
- A <code>while</code> loop is always tested for <code>true</code> BEFORE each iteration of the loop.
Question 3 : Which statement will immediately exit a while loop?
- The <code>break</code> statement will immediately exit a <code>while</code> loop.
Question 4 : What will be output from this code snippet?

int i = 1; while (i < 1) { System.out.println("Executing"); } System.out.println("Executing");
- 'Executing' will be output.
Question 5 : Which loop statement is always guaranteed to execute at least once?
- The <code>do while</code> loop is always guaranteed to execute at least once.
Question 6 : What will be output from this code snippet?

boolean a = false; while (a = true) { System.out.println("In the loop"); a = false; }
- This code will compile fine, but when run creates an endless loop that outputs 'In the loop' as each time we hit the <code>while</code> loop we assign <code>true</code> to the conditional expression.
Question 7 : How could we force the next iteration of a while loop?
- The <code>continue</code> statement will force the next iteration of a <code>while</code> loop.
Question 8 : The break statement exits all loops and continues at the next line?
- The <code>break</code> statement only exits the current loop. To exit all loops you would need to use the <code>break</code> statement with a <code>label</code> statement that points to the outermost loop.
Quiz Progress Bar Please select an answer


What's Next?

The next quiz on Java is all about Arrays.