In this post, we have provided Java loops (for loop, while loop, and do-while loop) multiple-choice questions to test your knowledge about loops in Java.
Learn Java loops at https://www.javaguides.net/p/java-tutorial-learn-java-programming.html
We would suggest you, try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of each question). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.
Q1: Which type of loop is best known for its boolean condition that controls entry to the loop?
A. do-while loop
B. for (traditional)
C. for-each
D. while
Answer:
D. while
Explanation:
Q2: Which type of loop is best known for using an index or counter?
A. do-while loop
B. for (traditional)
C. for-each
D. while
Answer:
B. for (traditional)
Explanation:
A traditional for loop is best known for having a loop variable counting up or down as the loop progresses. Therefore, Option B is correct.
Options A and D are incorrect because do-while and while loops are known for their boolean conditions.
Option C is incorrect because the for-each loop iterates through without an index.
Q3: Which type of loop is guaranteed to have the body execute at least once?
A. do-while loop
B. for (traditional)
C. for-each
D. while
Answer:
A. do-while loop
Explanation:
A do-while loop checks the loop condition after execution of the loop body. This ensures it always executes at least once, and Option A is correct.
Option B is incorrect because there are loops you can write that do not ever enter the loop body, such as for (int i=0;i<1;i++).
Similarly, Option D is incorrect because a while loop can be written where the initial loop condition is false.
Option C is incorrect because a for-each loop does not enter the loop body when iterating over an empty list.
Q4: Which of the following can loop through an array without referring to the elements by index?
A. do-while loop
B. for (traditional)
C. for-each
D. while
Answer:
C. for-each
Explanation:
While a traditional for loop often loops through an array, it uses an index to do so, making Option B incorrect.
The for-each loop goes through each element, storing it in a variable. Option C is correct.
Q5: What keyword is used to end the current loop iteration and proceed execution with the next iteration of that loop?
A. break
B. continue
C. end
D. skip
Answer:
B. continue
Explanation:
The continue keyword is used to end the loop iteration immediately and resume execution at the next iteration. Therefore, Option B is correct.
Option A is incorrect because the break statement causes execution to proceed after the loop body. Options C and D are incorrect because these are not keywords in Java.
Q6: What is the output of the following code snippet?
int i = 0;
for(i = 0 ; i < 5; i++){
}
System.out.println(i);
A. 5
B. 0
C. 4
D. Compilation Error
Answer:
A. 5
Explanation:
Q7: What is the output of the following program?
public class Test{
public static void main(String []args){
int i = 0;
for(i = 0; i < 10; i++){
break;
}
System.out.println(i);
}
}
Answer:
B. 0
Explanation:
Q8: What is the output of the following program?
public class Test{
public static void main(String []args){
int i = 0;
for(i = 0; i < 10; i++){
continue;
}
System.out.println(i);
}
}
Answer:
A. 10
Explanation:
Q9: What is the output of the following program?
public class Test{
public static void main(String []args){
for(int i = 0; i < 10; i++){
if(i % 2 == 0){
continue;
}
System.out.println(i);
}
}
}
Answer:
B. Program will print all odd numbers between 0 to 10
Explanation:
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course