Java Loops Quiz - Multiple Choice Questions (MCQ)

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:

while loop has a condition that returns a boolean that controls the loop. It appears at the beginning and is checked before entering the loop. Therefore, Option D is correct. 

A traditional for loop also has a boolean condition that is checked before entering the loop. However, it is best known for having a counter variable, making Option B incorrect. 

Option A is incorrect because the boolean condition on a do-while loop is at the end of the loop. Option C is incorrect because there is no condition as part of the loop construct.

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:

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:

The integer variable i declared before using it in for loop and can be accessible after for loop execution completes. In for loop, the i value will be incremented until the condition fails ( i < 5) so i value is 5.

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);	
     }
}
A. 1

B. 0

C. 10

D. 9

Answer:

B. 0

Explanation:

When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

The Java break statement is used to break the loop or switch statements. 

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);	
	}
}
A. 10

B. 0

C. Compilation error

D. 9

Answer:

A. 10

Explanation:

Java continue keyword makes for loop to skip the current iteration and continue with the next iteration. There will be total 10 iterations after which the value of variable i becomes 10 and that would make the for loop condition false. So finally the value of variable i is 10 after the loop hence Option A is correct.

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);
		}
	}
}
A.  Program will print all even numbers between 0 to 10

B.  Program will print all odd numbers between 0 to 10

C.  Program gives a compilation error

D.  None of the above

Answer:

B.  Program will print all odd numbers between 0 to 10

Explanation:

Option B is the correct choice. For loop starts with 0 and goes up to 9 after that the condition becomes false. Inside the loop, if condition checks if the current value of variable i is divisible by 2 by checking the remainder. If it is 0, the current iteration is skipped using the continue statement. If not, the number is odd (not divisible by 2) and the value is printed.

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. JDBC Quiz
  11. Java Lambda Expressions Quiz
  12. Java Functional Interfaces Quiz
  13. Java Streams API Quiz
  14. Java Date Time Quiz
  15. Java 8 Quiz

Comments