Java Loops MCQ - Multiple Choice Questions And Answers
Q. What is the output of the following code snippet?
for(int i=0; i< i++){ System.out.println(i); }A. 0 1 2 3 4
B. 1 2 3 4 5
C. 1 2 3 4
D. 0 1 2 3 4 5
Q. How many times will the following do-while loop execute?
int i = 1; do { System.out.println(i); i++; } while(i <= 5);A. 4
B. 5
C. 6
D. It will run indefinitely
Q. What is the output of the following code snippet?
int sum = 0; for(int i=1; i<=10; i+=2){ sum += i; } System.out.println(sum);A. 45
B. 25
C. 30
D. 55
Q. What is the output of the following code snippet?
int i = 10;
while(i >= 0){
System.out.println(i);
i -= 2;
}
A. 10 8 6 4 2 0B. 10 9 8 7 6 5 4 3 2 1 0
C. 0 2 4 6 8 10
D. 0 1 2 3 4 5 6 7 8 9 10
Q. What is the output of the following code snippet?
for(int i=0; i<5; i++){
System.out.println(i);
if(i == 2){
break;
}
}
A. 0 1 2 3 4B. 0 1 3 4
C. 0 1 2
D. It will result in a compile-time error
Q. What is the output of the following code snippet?
int i = 0;
do {
System.out.println(i);
if(i == 2){
continue;
}
i++;
} while(i <= 5);
A. 0 1 2 3 4 5B. 0 1 3 4 5
C. 0 1 2 3 4
D. It will result in a compile-time error
Q. What is the output of the following code snippet?
int i = 0;
while(i < 5){
i++;
if(i == 2){
continue;
}
System.out.println(i);
}
A. 1 2 3 4 5B. 1 3 4 5
C. 1 2 3 4
D. It will result in an infinite loop
Q. What is the output of the following code snippet?
int i = 0;
do {
i++;
if(i == 2){
break;
}
System.out.println(i);
} while(i < 5);
A. 1 2 3 4 5B. 1 3 4 5
C. 1 2 3 4
D. It will result in an infinite loop
Q. Which loop is ideal for situations where the number of iterations is known beforehand?
A. for loopB. while loop
C. do-while loop
D. It depends on the specific situation
Q. What is the purpose of the break statement in a loop?
A. To skip the current iteration and proceed to the next oneB. To terminate the loop prematurely
C. To repeat the current iteration from the beginning
D. It depends on the specific situation
Q. Which of the following code snippets correctly demonstrates the use of a for loop in Java?
A.for (int i = 0; i < 10; i++) { System.out.println(i); }B.
int i = 0; while (i < 10) { System.out.println(i); i++; }C.
int i = 10; do { System.out.println(i); i--; } while (i >= 0);D.
int i = 0; while (true) { if (i >= 10) { break; } System.out.println(i); i++; }
Q. Which of the following code snippets demonstrates an infinite loop in Java?
A.int i = 0; while (i < 10) { System.out.println(i); }B.
int i = 0; do { System.out.println(i); } while (i < 10);C.
for (int i = 0; i < 10; i++) { System.out.println(i); i--; }D.
while (true) { System.out.println("Infinite loop"); }