Search Tutorials


Java Loops MCQ - Multiple Choice Questions And Answers | JavaInUse

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 0
B. 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 4
B. 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 5
B. 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 5
B. 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 5
B. 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 loop
B. 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 one
B. 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");
}





Q. Which of the following code snippets demonstrates the correct syntax for a do-while loop in Java?

A.
int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 10)
B.
int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 10);
C.
do (int i = 0; i < 10; i++) {
    System.out.println(i);
} while (i < 10);
D.
int i = 0;
do {
    System.out.println(i);
    i++;
}
while(i < 10);

Q. Which of the following code snippets demonstrates the correct usage of a while loop in Java?

A.
int i = 0;
while (i < 10) {
    System.out.println(i);
    i++;
}
B.
int i = 0;
while (i < 10) {
    System.out.println(i);
}
C.
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
D.
int i = 0;
while (true) {
    System.out.println(i);
    i++;
    if (i >= 10) {
        break;
    }
}

Q. Which of the following code snippets demonstrates the correct usage of a nested for loop in Java?

A.
int rows = 5;
int columns = 5;
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        System.out.print("* ");
    }
    System.out.println();
}
B.
int rows = 5;
int columns = 5;
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}
C.
int rows = 5;
int columns = 5;
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        if (i == j) {
            System.out.print("* ");
        }
    }
    System.out.println();
}
D.
int rows = 5;
int columns = 5;
for (int i = rows; i >= 0; i--) {
    for (int j = columns; j >= 0; j--) {
        if (i >= j) {
            System.out.print("* ");
        }
    }
    System.out.println();
}

Q. Which of the following code snippets demonstrates the correct usage of a break statement in a loop?

A.
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}
B.
for (int i = 0; i < 10; i++) {
    System.out.println(i);
    if (i == 5) {
        break;
    }
}
C.
int i = 0;
while (i < 10) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
    i++;
}
D.
int i = 0;
do {
    System.out.println(i);
    if (i == 5) {
        break;
    }
    i++;
} while (i < 10);

Q. Which of the following code snippets demonstrates the correct usage of a continue statement in a loop?

A.
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        continue;
    }
    System.out.println(i);
}
B.
for (int i = 0; i < 10; i++) {
    System.out.println(i);
    if (i == 5) {
        continue;
    }
}
C.
int i = 0;
while (i < 10) {
    i++;
    if (i == 5) {
        continue;
    }
    System.out.println(i);
}
D.
int i = 0;
do {
    i++;
    if (i == 5) {
        continue;
    }
    System.out.println(i);
} while (i < 10);

Q. Which of the following code snippets demonstrates the correct usage of a nested while loop in Java?

A.
int i = 0;
int j = 0;
while (i < 10) {
    while (j < 5) {
        System.out.println(i + " " + j);
        j++;
    }
    i++;
}
B.
int i = 0;
while (i < 10) {
    int j = 0;
    while (j < 5) {
        System.out.println(i + " " + j);
        j++;
    }
    i++;
}
C.
int i = 0;
int j = 0;
while (i < 10) {
    while (j < 5) {
        System.out.println(i + " " + j);
        i++;
    }
    j++;
}
D.
int i = 0;
int j = 0;
while (i < 10) {
    while (j < 5) {
        System.out.println(i + " " + j);
        j++;
    }
    i++;
}

Q. Which of the following code snippets demonstrates the correct usage of a for-each loop in Java?

A.
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}
B.
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    System.out.println(number);
}
C.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
for (int i = 0; i < numbers.size(); i++) {
    System.out.println(numbers.get(i));
}
D.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
for (Integer number : numbers) {
    System.out.println(number);
}

Q. Which of the following code snippets demonstrates the correct usage of a nested do-while loop in Java?

A.
int i = 0;
do {
    int j = 0;
    do {
        System.out.println(i + " " + j);
        j++;
    } while (j < 5);
    i++;
} while (i < 10);
B.
int i = 0;
do {
    int j = 0;
    do {
        System.out.println(j);
        j++;
    } while (j < 5);
    i++;
} while (i < 10);
C.
int i = 0;
int j = 0;
do {
    do {
        System.out.println(j);
        j++;
    } while (j < 5);
    i++;
} while (i < 10);
D.
int i = 0;
int j = 0;
do {
    do {
        System.out.println(i + " " + j);
        j++;
    } while (j < 5);
    i++;
} while (i < 10);