0% found this document useful (0 votes)
420 views5 pages

Java Loops MCQs Practice Quiz

Uploaded by

Nidhi Bhatia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
420 views5 pages

Java Loops MCQs Practice Quiz

Uploaded by

Nidhi Bhatia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Loops: Multiple Choice Questions

1. Which of the following is NOT a type of loop in Java?

a) for loop
b) while loop
c) do-while loop
d) foreach loop

2. What is the correct syntax for a for loop in Java?

a) for (initialization; condition; update)


b) for (condition; initialization; update)
c) for (initialization; update; condition)
d) for (update; initialization; condition)

3. How many times will the loop execute?

for (int i = 0; i < 5; i++)


{ [Link](i); }
a) 4
b) 5
c) 6
d) 0

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


int i = 0;
while (i < 3) {
[Link](i);
i++;
}

a) 012
b) 123
c) 345
d) Error

5. Which of these statements is true for a do-while loop?


a) The loop is executed at least once.
b) The condition is checked before the loop executes.
c) It’s the same as a while loop.
d) It is a pre-test loop.

6. What is the purpose of the break statement in loops?

a) To skip the current iteration


b) To stop the execution of the loop entirely
c) To restart the loop
d) To stop the program

7. What does the continue statement do in a loop?

a) Ends the program


b) Terminates the loop
c) Skips the rest of the code for the current iteration
d) Restarts the loop

8. What is the output of the following code?


for (int i = 0; i < 3; i++) {
if (i == 1) continue;
[Link](i);
}

a) 01
b) 02
c) 012
d) 02

9. What will happen if the condition in a while loop is always true?

a) The program will stop automatically.


b) It will cause an infinite loop.
c) It will run once and stop.
d) It will cause a compilation error.

10. Which loop is ideal for situations where the number of iterations is known?
a) while loop
b) do-while loop
c) for loop
d) None of the above

11. What will be the output?


int i = 5;
while (i > 0) {
[Link](i);
i--;
}

a) 54321
b) 12345
c) 55555
d) Error

12. Can a for loop contain multiple initialization statements?

a) Yes
b) No

13. What is the correct way to write an infinite loop using a while statement?

a) while (true) { }
b) while (false) { }
c) while (1) { }
d) while (0) { }

14. Which of the following loops is guaranteed to execute at least once?

a) for loop
b) while loop
c) do-while loop
d) None of the above

15. What is the output of the following?


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
[Link](i + "," + j + " ");
}
}

a) 0,0 0,1 1,0 1,1 2,0 2,1


b) 0,0 1,0 2,0 0,1 1,1 2,1
c) 0,1 1,1 2,1
d) Error

16. What happens if you use break in a nested loop?

a) It exits all loops.


b) It exits the current loop only.
c) It causes a compilation error.
d) It restarts the program.

17. What is the output of the following?


for (int i = 0; i < 5; i++) {
if (i == 3) break;
[Link](i);
}

a) 0123
b) 012
c) 01
d) 0

18. for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (i == j) {
continue;
}
[Link](i + "" + j + " ");
}
}
a) 01 02 10 12 20 21
b) 01 02 10 12 20
c) 01 02 12 21
d) No output (loop skips all iterations due to continue)
19. What is the output of this code?

int count = 0;

for (int i = 0; i < 5; i++) {

for (int j = i; j < 5; j++) {

count++;

[Link](count);

a) 10
b) 15
c) 20
d) 25

20. What is the output of this code?


int i = 0;
do {
[Link](i);
i++;
} while (i < 3);

a) 123
b) 012
c) 345
d) Error

Common questions

Powered by AI

Switching the positions of the condition and update in a for-loop disrupts the logical flow of the loop process. The standard format (initialization; condition; update) ensures that the loop condition is checked before each iteration and the loop updates occur after each iteration. Adhering to this format is crucial for expected results and to prevent syntax errors or logical bugs, as improperly structured loops might behave unpredictably or fail to execute correctly .

Understanding the differences is crucial because 'do-while' guarantees a minimum of one execution, while 'while' and 'for' loops only execute upon condition satisfaction. This assures that the 'do-while' loop executes the code block at least once, which can be useful in scenarios requiring initial processing or prompting user action. Recognizing these differences helps in preventing errors in execution designs where zero iterations are necessary, or a guaranteed single run is not ideal .

The code uses nested loops with a 'continue' statement, causing certain combinations to be skipped. The output of the code is '01 02 10 12 20 21', as the 'continue' bypasses the print statement when the loop variables i and j are equal, preventing the printing of '00', '11', and '22' .

A 'do-while' loop in Java executes its block of code at least once before evaluating its condition, because the condition check comes after the loop body. This guarantees at least one execution of the loop body, even if the condition is false initially. This requires developers to consider at least one iteration's effect when planning logic and validation, unlike other loops which may not execute if their conditions are not met .

A 'for' loop is preferred for predetermined iterations because it integrates initialization, condition-setting, and iteration updates in a single line, which improves readability and reduces the potential for logic errors associated with these tasks being separated in a 'while' loop. However, this concise structure can be a drawback if the loop requires complex logic changes mid-execution, as modifications are less straightforward compared to 'while' loops .

Multiple initialization statements in a 'for' loop are permissible and can enhance execution flexibility by allowing more complex starting conditions, such as initializing multiple counter variables at once. However, this can reduce readability by overcomplicating the loop header, obscuring individual variable roles, and increasing the cognitive load required to understand loop setup. Clear commenting and encapsulation strategies might be required to mitigate these issues .

Using an always true condition in a 'while' loop results in an infinite loop, which could cause the program to hang or consume resources indefinitely unless there is an explicit break condition placed within the loop body. This is particularly problematic if the loop runs critical operations that are not meant to execute endlessly .

In a nested loop, the 'break' statement causes the control to exit only the current loop where the 'break' is called, not all the loops. This allows breaking out of the immediate loop without affecting any upper loops .

Managing variable scope in Java loops involves declaring loop control variables within the loop constructs themselves to limit their accessibility to the loop iteration block. This prevents unintended errors by ensuring that inner loop variables do not interfere with outer loops or other parts of the program. Additionally, constants or independently scoped variables should be declared outside nested loops to ensure their values are consistent and unchanged by loop iterations .

The 'continue' statement within a loop immediately skips the remaining code inside the loop for the current iteration and jumps to the next iteration of the loop. In contrast, 'break' exits the loop entirely. 'Continue' is strategically used to bypass certain code executions based on conditions without terminating the loop, which can be useful for filtering unwanted data or skipping error-prone states .

You might also like