Sunday, July 28, 2024

Do while loop MCQ

What is the primary difference between a do-while loop and a while loop in Java?

a) do-while executes its body at least once even if the condition is false initially.

b) do-while always executes its body at least once.

c) There is no difference between do-while and while loops.

d) while loop executes its body only if the condition is true initially.


What is the syntax to create a do-while loop in Java?

a) do { } while ();

b) while { } do ();

c) do { } while {};

d) do { } while ();


In a do-while loop, when is the condition checked?

a) Before executing the loop body

b) After executing the loop body

c) Before and after executing the loop body

d) Only once at the start


Which of the following statements is true regarding the termination condition in a do-while loop?

a) It is optional.

b) It must always be placed before the loop body.

c) It must be enclosed in parentheses after the do keyword.

d) It must end with a semicolon after the condition.


What happens if the condition in a do-while loop is initially false?

a) The loop body is executed at least once.

b) The loop body is skipped entirely.

c) It results in a compilation error.

d) The loop becomes an infinite loop.


What statement is used to exit a do-while loop prematurely in Java?

a) exit

b) break

c) terminate

d) skip


Which part of the do-while loop contains the condition that determines whether the loop body should execute again?

a) Initialization

b) Condition

c) Iteration

d) Termination


What is the primary purpose of the do-while loop?

a) To execute a block of code repeatedly as long as a specified condition is true

b) To perform a one-time check of a condition

c) To execute a block of code a fixed number of times

d) To execute a block of code once regardless of the condition


How many times will a do-while loop execute if the condition is false from the beginning?

a) It depends on the loop structure.

b) Once

c) Twice

d) Indefinitely


In a do-while loop, where is the loop condition evaluated?

a) Before executing the loop body

b) After executing the loop body

c) Both before and after executing the loop body

d) Only once at the start


Answers:


a) do-while executes its body at least once even if the condition is false initially.

d) do { } while ();

b) After executing the loop body

c) It must be enclosed in parentheses after the do keyword.

a) The loop body is executed at least once.

b) break

b) Condition

a) To execute a block of code repeatedly as long as a specified condition is true

b) Once

a) Before executing the loop body


Difficult MCQs on do-while Loop

What will be the output of the following code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

        } while (i < 5);

        System.out.println(i);

    }

}

a) 4

b) 5

c) 6

d) 0


Answer: b) 5

Explanation: The loop increments i from 0 to 5. The condition i < 5 becomes false when i equals 5, thus the loop exits and 5 is printed.


What will be the output of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int x = 5;

        int y = 0;

        do {

            y++;

            x--;

        } while (x > 0 && y < 5);

        System.out.println(y);

    }

}

a) 4

b) 5

c) 6

d) 0


Answer: b) 5

Explanation: The do-while loop continues while x > 0 and y < 5. Since x is decremented and y is incremented on each iteration, the loop stops when y reaches 5.


What will the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

            if (i == 3) {

                continue;

            }

            System.out.print(i + " ");

        } while (i < 5);

    }

}

a) 1 2 4 5

b) 1 2 4

c) 1 2 3 4

d) 1 2 3 4 5


Answer: b) 1 2 4

Explanation: The continue statement skips the current iteration when i equals 3, so 3 is not printed.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 10;

        do {

            if (i % 3 == 0) {

                break;

            }

            i--;

        } while (i > 5);

        System.out.println(i);

    }

}

a) 9

b) 10

c) 8

d) 6


Answer: a) 9

Explanation: The break statement terminates the loop when i % 3 == 0, so when i is 9, the loop exits and 9 is printed.


What is the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

            if (i == 3) {

                continue;

            }

            System.out.print(i + " ");

        } while (i < 5);

    }

}

a) 1 2 4

b) 1 2 3 4

c) 1 2 3 4 5

d) 1 2 3


Answer: a) 1 2 4

Explanation: When i equals 3, the continue statement skips printing and moves to the next iteration.


What will be the result of the following code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 10;

        do {

            System.out.print(i + " ");

            i -= 2;

        } while (i > 0);

    }

}

a) 10 8 6 4 2

b) 10 8 6 4

c) 10 9 8 7 6 5 4 3 2 1

d) 10 8 6 4 2 0


Answer: a) 10 8 6 4 2

Explanation: The do-while loop decrements i by 2 each time, stopping when i is no longer greater than 0.


What will the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int x = 5;

        int y = 0;

        do {

            y++;

            x--;

            if (x == 3) {

                break;

            }

        } while (y < 5);

        System.out.println(y);

    }

}

a) 3

b) 4

c) 5

d) 0


Answer: a) 3

Explanation: The break statement terminates the loop when x equals 3, so the loop exits with y equal to 3.


What is the output of the following code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

            if (i % 2 == 0) {

                continue;

            }

            System.out.print(i + " ");

        } while (i < 5);

    }

}

a) 1 3

b) 1 2 3

c) 2 4

d) 1 2 3 4 5


Answer: a) 1 3

Explanation: The continue statement skips printing for even values of i.


What will be the result of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

            if (i > 3) {

                break;

            }

        } while (i < 5);

        System.out.println(i);

    }

}

a) 3

b) 4

c) 5

d) 0


Answer: b) 4

Explanation: The break statement terminates the loop when i becomes 4, and 4 is printed.


What is the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

            if (i == 2) {

                continue;

            }

            System.out.print(i + " ");

        } while (i < 4);

    }

}

a) 1 3

b) 1 2 3

c) 2 3

d) 1 2


Answer: a) 1 3

Explanation: When i equals 2, continue skips printing 2, so only 1 and 3 are printed.


What does the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

            if (i == 3) {

                return;

            }

            System.out.print(i + " ");

        } while (i < 5);

    }

}

a) 1 2

b) 1 2 3

c) 1 2 3 4

d) 1 2 3 4 5


Answer: a) 1 2

Explanation: The return statement terminates the method, stopping the loop and printing 1 2.


What is the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            if (i == 3) {

                break;

            }

            System.out.print(i + " ");

            i++;

        } while (i < 5);

    }

}

a) 0 1 2

b) 0 1 2 3

c) 1 2 3

d) 0 1 2 3 4


Answer: a) 0 1 2

Explanation: The break statement terminates the loop when i equals 3, so only 0 1 2 are printed.


What will the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        do {

            System.out.print(i + " ");

            i += 2;

        } while (i < 10);

    }

}

a) 1 3 5 7 9

b) 1 3 5 7

c) 1 3 5 7 9 11

d) 1 2 3 4 5 6 7 8 9


Answer: a) 1 3 5 7 9

Explanation: The loop increments i by 2 each time, so the output is the series 1 3 5 7 9.


What does this code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i++;

            if (i == 4) {

                continue;

            }

            System.out.print(i + " ");

        } while (i < 5);

    }

}

a) 1 2 3 4

b) 1 2 3

c) 1 2 3 5

d) 1 2 3 4 5


Answer: a) 1 2 3 5

Explanation: The continue statement skips the printing of 4, so 1 2 3 and 5 are printed.


What will the following code snippet output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        do {

            i += 2;

            if (i == 4) {

                continue;

            }

            System.out.print(i + " ");

        } while (i < 6);

    }

}

a) 2 6

b) 2 4 6

c) 2 6 8

d) 2 6


Answer: a) 2 6

Explanation: The continue statement skips printing 4, so 2 and 6 are printed.

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...