Java While and Do/While Loop MCQ

1. What is the primary function of the while loop in Java?

a) To execute a block of code a fixed number of times
b) To execute a block of code as long as a specified condition is true
c) To iterate over elements of an array
d) To execute a block of code at least once regardless of condition

Answer:

b) To execute a block of code as long as a specified condition is true

Explanation:

The while loop in Java is used to loop through a block of code repeatedly, as long as a specified condition remains true【95†source】.

2. In a while loop, when is the condition checked?

a) After the code block is executed
b) Before the code block is executed
c) Only once at the beginning of the loop
d) At the end of the loop's execution

Answer:

b) Before the code block is executed

Explanation:

In a while loop, the condition is checked before the execution of the code block within the loop【95†source】.

3. How does a do/while loop differ from a while loop?

a) It executes the code block regardless of the condition
b) It checks the condition before executing the code block
c) It executes the code block at least once, even if the condition is false
d) It only runs if the condition is true at the start

Answer:

c) It executes the code block at least once, even if the condition is false

Explanation:

The do/while loop differs from a while loop in that it executes the code block once before checking the condition, ensuring the code block is executed at least once【96†source】.

4. What is the syntax to increment a counter variable in a while loop?

a) i += 1
b) i++
c) count(i)
d) ++i

Answer:

b) i++

Explanation:

Commonly, 'i++' is used within a while loop to increment a counter variable, although 'i += 1' or '++i' are also valid.

5. In a do/while loop, where is the condition written?

a) Inside the do block
b) After the do block, before the while keyword
c) After the while keyword
d) Before the do block

Answer:

c) After the while keyword

Explanation:

In a do/while loop, the condition is written after the while keyword, following the code block enclosed in the do block【96†source】.

6. What is the risk of not updating the loop variable in a while loop?

a) The loop may execute more times than expected
b) The loop might not execute at all
c) The loop could become an infinite loop
d) There is no risk; the loop will terminate correctly

Answer:

c) The loop could become an infinite loop

Explanation:

If the loop variable is not updated within a while loop, it may result in an infinite loop as the condition might never become false【95†source】.

7. Can a while loop be used to iterate over an array?

a) Yes, by using an index variable
b) No, it can only loop a fixed number of times
c) Yes, but only with single-dimensional arrays
d) No, only for loops can iterate over arrays

Answer:

a) Yes, by using an index variable

Explanation:

A while loop can iterate over an array by using an index variable to access each element in sequence.

8. What will be the output of the following code?

int i = 0;
while (i < 3) {
System.out.println(i);
i++;
}
a) 0 1 2 3
b) 0 1 2
c) 1 2 3
d) No output

Answer:

b) 0 1 2

Explanation:

The while loop will print the values 0, 1, and 2, as it increments 'i' from 0 to 2 and stops when 'i' becomes 3【95†source】.

9. In a do/while loop, is the condition checked during the first iteration?

a) Yes, before the code block is executed
b) No, after the first execution of the code block
c) Yes, after the first execution of the code block
d) No, it is checked only in the last iteration

Answer:

b) No, after the first execution of the code block

Explanation:

In a do/while loop, the condition is checked after the first execution of the code block, ensuring the code block runs at least once【96†source】.

10. What is a common use case for a do/while loop?

a) When the number of iterations is known in advance
b) When the code block needs to execute at least once regardless of the condition
c) For iterating over collections
d) For executing multiple conditions

Answer:

b) When the code block needs to execute at least once regardless of the condition

Explanation:

A do/while loop is typically used when the code block needs to be executed at least once, even if the loop condition is false at the start&#8203;“【oaicite:3】“&#8203;.

11. Which loop is guaranteed to execute its code block at least once?

a) For loop
b) While loop
c) Do/while loop
d) Infinite loop

Answer:

c) Do/while loop

Explanation:

The do/while loop is guaranteed to execute its code block at least once because the condition is checked after the execution of the code block&#8203;“【oaicite:2】“&#8203;.

12. How can an infinite loop be created using a while loop?

a) By not including a condition
b) By setting the condition to always be true
c) By omitting the loop variable
d) By using a break statement

Answer:

b) By setting the condition to always be true

Explanation:

An infinite loop can occur in a while loop if the condition is set in such a way that it always evaluates to true, and there's no mechanism within the loop to break out of it&#8203;“【oaicite:1】“&#8203;.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top