C While and Do/While Loop MCQ Questions and Answers
Q. What is the difference between a 'while' loop and a 'do-while' loop in Java?
A. The 'while' loop checks the condition at the beginning, the 'do-while' loop checks it at the end.B. The 'while' loop requires the condition to be true initially, the 'do-while' loop does not.
C. The 'while' loop executes as long as the condition is true, the 'do-while' loop executes at least once.
D. The 'while' loop is used for iterative tasks, the 'do-while' loop is used for conditional tasks.
Q. What is the output of the following 'while' loop?
int i = 1; while (i < 5) { System.out.print(i + " "); i++; }A. 1 2 3 4
B. 1 2 3
C. 2 3 4 5
D. 1 1 1
Q. What is the purpose of the 'break' statement in a 'while' loop?
A. To exit the loop prematurelyB. To skip the next iteration
C. To continue to the next iteration
D. To restart the loop
Q. What is the output of the following 'do-while' loop?
int i = 1; do { System.out.print(i + " "); i++; } while (i < 5);A. 1 2 3 4
B. 1 2 3
C. 2 3 4 5
D. 1 1 1
Q. What is the purpose of the 'continue' statement in a 'while' loop?
A. To exit the loop prematurelyB. To skip the rest of the current iteration and move to the next one
C. To restart the loop
D. To execute the loop indefinitely
Q. How can you create an infinite loop using a 'while' loop?
A. while (true) {B. while (false) {
C. do { while (true) } while (false);
D. for(;;) {
Q. What is the output of the following code snippet?
int count = 0; while (count < 5) { count++; if (count == 3) { continue; } System.out.print(count + " "); }A. 1 2 4 5
B. 1 2 3 4 5
C. 1 2 3
D. 2 3 4
Q. What is the purpose of the 'while' loop in Java?
A. To repeat a block of code as long as a certain condition is trueB. To execute a block of code a fixed number of times
C. To exit a program prematurely
D. To skip a block of code if a certain condition is true
Q. How can you nest a 'do-while' loop inside a 'while' loop?
A. By placing the 'do-while' loop inside the body of the 'while' loopB. By using a break statement to exit the inner loop
C. By using a continue statement to skip the inner loop
D. By placing the 'while' loop inside the body of the 'do-while' loop
Q. What is the output of the following code snippet?
int num = 1; while (num <= 5) { System.out.print(num); num++; }A. 12345
B. 1234
C. 123456
D. 1