Questions and Answers 100% Pass
What type of loop is best when the number of iterations is known beforehand?
✔✔A) for loop
B) while loop
C) do-while loop
D) infinite loop
Which loop executes at least once, regardless of the condition?
A) for loop
B) while loop
✔✔C) do-while loop
D) foreach loop
What will happen if the condition in a while loop is always true?
A) The loop will execute only once
✔✔B) It will result in an infinite loop
C) The program will not compile
1
,D) Java will automatically stop it
Which statement is used to terminate a loop immediately?
✔✔A) break
B) continue
C) return
D) exit
What is the purpose of the continue statement in a loop?
A) It terminates the loop
B) It skips the entire loop execution
✔✔C) It skips the current iteration and moves to the next
D) It forces the loop to end immediately
Which of the following loops is best for iterating over an array?
A) while loop
✔✔B) for-each loop
C) do-while loop
2
,D) switch loop
What is the key difference between a while loop and a do-while loop?
✔✔A) do-while executes at least once, even if the condition is false
B) while executes at least once before checking the condition
C) while always executes at least twice
D) do-while cannot contain a break statement
Which loop is most suitable when the number of iterations is unknown?
A) for loop
✔✔B) while loop
C) for-each loop
D) switch statement
What is the output of the following code?
```java
int i = 0;
3
, while (i < 3) {
System.out.print(i + " ");
i++;
}
```
A) `3 2 1`
B) `3 3 3`
✔✔C) `0 1 2`
D) `1 2 3`
What will happen if the update statement in a for loop is missing?
✔✔A) It may result in an infinite loop
B) It will execute only once
C) It will cause a compilation error
D) The loop will never execute
Which keyword is used to exit from multiple nested loops?
4