Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

Java_Loops_MCQs

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)
44 views

Java_Loops_MCQs

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
You are on page 1/ 5

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++)


{ System.out.println(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) {
System.out.print(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;
System.out.print(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) {
System.out.print(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++) {
System.out.print(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;
System.out.print(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;
}
System.out.print(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++;

System.out.println(count);

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

20. What is the output of this code?


int i = 0;
do {
System.out.print(i);
i++;
} while (i < 3);

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

You might also like