C Loops MCQ Questions and Answers
Q. What is the type of loop that is used to execute a block of code as long as a specific condition is true?
A. while loopB. do-while loop
C. for loop
D. All of the above
Q. What is the output of the following code snippet?
int i = 1; while (i <= 5) { System.out.print(i); i++; }A. 12345
B. 1234
C. 123456
D. None of the above
Q. What is the difference between a while loop and a do-while loop?
A. while loop checks the condition before executing the loop body, do-while loop checks the condition after executing the loop body.B. while loop continues until the condition is false, do-while loop continues until the condition is true.
C. There is no difference between them.
D. do-while loop is faster than while loop.
Q. What is the purpose of the break statement in a loop?
A. To exit the loop immediatelyB. To skip the rest of the current iteration and continue with the next iteration
C. To start a new iteration
D. To exit the program
Q. What is the output of the following code snippet?
int i = 1; while (true) { System.out.print(i); if (i == 3) { break; } i++; }A. 123
B. 12
C. 1234
D. An infinite loop
Q. What is the purpose of the continue statement in a loop?
A. To exit the loop immediatelyB. To skip the rest of the current iteration and continue with the next iteration
C. To start a new iteration
D. To exit the program
Q. What is the output of the following code snippet?
for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.print(i); }A. 1245
B. 12345
C. 125
D. An error
Q. What is the purpose of the for loop?
A. To execute a block of code a fixed number of timesB. To execute a block of code as long as a condition is true
C. To iterate over a collection of items
D. All of the above
Q. What is the output of the following code snippet?
for (int i = 1; i <= 5; i++) { System.out.print(i); }A. 12345
B. 1
C. 1234
D. 5
Q. How can you iterate over the elements of an array using a for loop?
A. for (int i = 0; i < array.length; i++) {B. for (int i = 1; i <= array.length; i++) {
C. for (int i = array.length; i > 0; i--) {
D. for (int i = 0; i <= array.length; i++) {