Search Tutorials


C While and Do/While Loop MCQ Questions and Answers | JavaInUse

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 prematurely
B. 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 prematurely
B. 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 true
B. 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' loop
B. 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





Q. What is the purpose of the 'while' loop in the following code snippet?

    int sum = 0;
    int num = 1;
    while (num <= 5) {
        sum += num;
        num++;
    }
    System.out.println("Sum = " + sum);
    
A. To calculate the sum of the first 5 numbers
B. To print the numbers from 1 to 5
C. To find the largest number in an array
D. To check if a number is even or odd

Q. What is the output of the following 'do-while' loop?

    int i = 1;
    do {
        System.out.print(i);
        i++;
    } while (i <= 5);
    
A. 12345
B. 1234
C. 123456
D. 1

Q. How can you use a 'while' loop to read input from the user until a specific value is entered?

A. Use a break statement to exit the loop when the specific value is entered
B. Use a continue statement to skip the current iteration when the specific value is entered
C. Use a flag variable to control the loop until the specific value is entered
D. Use a nested loop to repeatedly read input until the specific value is entered

Q. What is the output of the following code snippet that uses a 'do-while' loop?

    int num = 1;
    do {
        System.out.print(num);
        num++;
    } while (num < 5);
    
A. 1
B. 1234
C. 12345
D. 2345

Q. How can you use a 'while' loop to find the sum of all even numbers between 1 and 10?

A. Use a flag variable to keep track of even numbers and add them to the sum
B. Use a nested loop to iterate through the numbers and add the even ones to the sum
C. Use a break statement to exit the loop when an odd number is encountered
D. Use a continue statement to skip odd numbers and add even numbers to the sum

Q. What is the purpose of the 'else' statement in a 'while' loop?

A. To execute a block of code if the loop condition is true
B. To execute a block of code if the loop condition is false
C. To exit the loop prematurely
D. To skip the next iteration

Q. What is the output of the following code snippet that uses a 'while' loop and an 'if' statement?

    int i = 1;
    while (i <= 5) {
        if (i % 2 == 0) {
            System.out.print(i + " ");
        }
        i++;
    }
    
A. 2 4
B. 1 2 3 4 5
C. 1 3 5
D. 1 2 3

Q. How can you use a 'do-while' loop to read input from the user until a specific character is entered?

A. Use a break statement to exit the loop when the specific character is entered
B. Use a continue statement to skip the current iteration when the specific character is entered
C. Use a flag variable to control the loop until the specific character is entered
D. Use a nested loop to repeatedly read input until the specific character is entered

Q. What is the purpose of the 'flag' variable in the following code snippet that uses a 'while' loop?

    boolean flag = true;
    int num = 1;
    while (flag) {
        System.out.print(num + " ");
        if (num == 5) {
            flag = false;
        }
        num++;
    }
    
A. To control the loop and terminate it when num becomes 5
B. To skip the next iteration when num is equal to 5
C. To exit the loop prematurely when num is greater than 5
D. To restart the loop when num becomes 5

Q. What is the output of the following code snippet that uses a 'do-while' loop and an 'if-else' statement?

    int i = 1;
    do {
        if (i % 2 == 0) {
            System.out.print("Even ");
        } else {
            System.out.print("Odd ");
        }
        i++;
    } while (i <= 5);
    
A. Odd Odd Even Odd Even
B. Odd Even Odd Even Odd
C. Even Odd Even Odd Even
D. Odd Odd Odd Odd Odd

Q. How can you use a 'while' loop to find the largest number in an array?

A. Iterate through the array and update the largest number if a larger value is found
B. Use a nested loop to compare each element with every other element in the array
C. Use a break statement to exit the loop when the largest number is found
D. Use a continue statement to skip smaller numbers and store the largest number

Q. What is the purpose of the 'label' in a labeled 'while' loop?

A. To provide a name for the loop
B. To specify the loop condition
C. To control the flow of the loop
D. To nest one loop inside another

Q. What is the output of the following code snippet that uses a 'while' loop and a 'switch' statement?

    int i = 1;
    while (i <= 3) {
        switch (i) {
            case 1:
                System.out.print("One ");
                break;
            case 2:
                System.out.print("Two ");
                break;
            case 3:
                System.out.print("Three ");
                break;
        }
        i++;
    }
    
A. One Two Three
B. One
C. One Two
D. One Three

Q. How can you use a 'do-while' loop to calculate the factorial of a number?

A. Use a break statement to exit the loop when the number becomes 0
B. Use a continue statement to skip the current iteration when the number is even
C. Use a flag variable to control the loop and calculate the factorial
D. Use a nested loop to calculate the factorial recursively