Loops In Java Flashcards
What is a loop in Java?
A loop is a control structure that allows code to be executed repeatedly based on a condition.
Name the three main types of loops in Java.
The three main types of loops in Java are for loop, while loop, and do-while loop.
True or False: A for loop is used when the number of iterations is known.
True
Fill in the blank: The syntax for a for loop is ‘for (___; ___; ___)’.
initialization; condition; increment/decrement
What is the difference between a while loop and a do-while loop?
A while loop checks the condition before executing the loop body, while a do-while loop executes the loop body at least once before checking the condition.
Which loop would you use to iterate through an array in Java?
You can use a for loop or an enhanced for loop (for-each loop) to iterate through an array.
What keyword is used to exit a loop prematurely?
The keyword ‘break’ is used to exit a loop prematurely.
What keyword can be used to skip the current iteration of a loop?
The keyword ‘continue’ can be used to skip the current iteration of a loop.
True or False: The initialization part of a for loop can be empty.
True
What is an infinite loop?
An infinite loop is a loop that continues to execute indefinitely because its terminating condition is never met.
What is the output of the following code: ‘for (int i = 0; i < 5; i++) { System.out.print(i); }’?
The output will be ‘01234’.
Fill in the blank: In a for-each loop, the syntax is ‘for (___ : ___)’.
dataType variable : array/collection
What is the purpose of the ‘break’ statement in nested loops?
The ‘break’ statement exits the innermost loop in which it is called.
What is the output of the following code: ‘int i = 0; while (i < 3) { System.out.print(i); i++; }’?
The output will be ‘012’.
True or False: You can nest loops in Java.
True
What will happen if you do not update the loop variable in a while loop?
If you do not update the loop variable, it may lead to an infinite loop.
Which loop is preferred for iterating over collections in Java?
The enhanced for loop (for-each loop) is preferred for iterating over collections.
What is the syntax for a while loop?
The syntax for a while loop is ‘while (condition) { // body }’.
Fill in the blank: The condition of a while loop must evaluate to ___ to continue looping.
true
What is the output of the following code: ‘for (int i = 5; i > 0; i–) { System.out.print(i); }’?
The output will be ‘54321’.
True or False: A do-while loop will always execute at least once.
True
What is an example of a scenario where a while loop is more appropriate than a for loop?
A while loop is more appropriate when the number of iterations is not known beforehand, such as waiting for user input.
What is the enhanced for loop also known as?
The enhanced for loop is also known as the for-each loop.
What is the purpose of the ‘continue’ statement in a loop?
The ‘continue’ statement skips the current iteration and proceeds to the next iteration of the loop.