Sunday, July 28, 2024

Break and Continue MCQ

 What is the primary purpose of the break statement in Java?

a) To skip the current iteration and proceed to the next iteration

b) To terminate the loop and exit its body prematurely

c) To restart the loop from the beginning

d) To skip the loop entirely


Which loop statement does the break statement terminate in Java?

a) Only the innermost loop

b) Only the outermost loop

c) All enclosing loops

d) It depends on the loop structure


In a nested loop scenario, if break is used in the inner loop, what happens?

a) It breaks the inner loop and continues the outer loop.

b) It breaks both the inner and outer loops.

c) It continues the inner loop.

d) It causes a compilation error.


What is the purpose of the continue statement in Java?

a) To skip the current iteration and proceed to the next iteration

b) To terminate the loop and exit its body prematurely

c) To restart the loop from the beginning

d) To skip the loop entirely


In a loop containing both break and continue statements, which statement takes precedence?

a) break

b) continue

c) Both have equal precedence

d) It depends on their placement within the loop body


Where does the continue statement take the control flow in a loop?

a) To the beginning of the loop

b) To the end of the loop

c) To the next statement after the loop

d) It terminates the loop


What happens if a continue statement is encountered within a switch statement in Java?

a) It continues to the next case within the switch.

b) It continues to the default case.

c) It exits the switch statement.

d) It results in a compilation error.


What is the result of using break or continue outside of any loop structure in Java?

a) It results in a runtime error.

b) It is allowed and executes as expected.

c) It leads to a compilation error.

d) It halts the program execution.


In a loop with both break and continue statements, if both are encountered within the same iteration, which statement gets executed?

a) break

b) continue

c) Both execute simultaneously

d) It leads to an infinite loop


What does the break statement do when used in a switch statement in Java?

a) Exits the switch statement and continues with the next statement after the switch

b) Exits the current case and proceeds to the next case within the switch

c) Terminates the switch statement and halts the program

d) It is not allowed in a switch statement


Answers:


b) To terminate the loop and exit its body prematurely

c) All enclosing loops

a) It breaks the inner loop and continues the outer loop.

a) To skip the current iteration and proceed to the next iteration

a) break

a) To the beginning of the loop

c) It exits the switch statement.

c) It leads to a compilation error.

a) break

a) Exits the switch statement and continues with the next statement after the switch



MCQs on break and continue Statements in Java

What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {

            if (i == 3) {

                break;

            }

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

        }

    }

}

a) 0 1 2 3

b) 0 1 2

c) 1 2 3

d) 0 1 2 3 4


Answer: b) 0 1 2

Explanation: The loop breaks when i equals 3, so 3 is not printed.


What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            if (i == 2) {

                i++;

                continue;

            }

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

            i++;

        }

    }

}

a) 0 1 2 3 4

b) 0 1 3 4

c) 0 1 2 4

d) 1 2 3 4


Answer: b) 0 1 3 4

Explanation: When i is 2, the continue statement skips the remaining part of the loop and increments i.


What is the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            if (i % 2 == 0) {

                continue;

            }

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

        }

    }

}

a) 1 2 3 4 5

b) 1 3 5

c) 2 4

d) 1 2 3 4


Answer: b) 1 3 5

Explanation: The continue statement skips even numbers, so only odd numbers are printed.


What will be the output of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        outer: for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                if (i == 1 && j == 1) {

                    break outer;

                }

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

            }

        }

    }

}

a) 00 01 10

b) 00 01 10 11

c) 00 01 10

d) 00 01 10 11 12


Answer: a) 00 01 10

Explanation: The break outer statement breaks out of the outer loop when i is 1 and j is 1.


What is the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            if (i == 3) {

                i++;

                break;

            }

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

            i++;

        }

    }

}

a) 0 1 2 3

b) 0 1 2

c) 0 1 2 3 4

d) 0 1 2 3


Answer: b) 0 1 2

Explanation: The loop breaks when i is 3, and 3 is not printed.


What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        for (int i = 1; i <= 3; i++) {

            for (int j = 1; j <= 3; j++) {

                if (i == 2 && j == 2) {

                    continue;

                }

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

            }

        }

    }

}

a) 11 12 13 21 22 23 31 32 33

b) 11 12 13 21 23 31 32 33

c) 11 12 13 21 31 32 33

d) 11 12 13 21 23 31 32


Answer: b) 11 12 13 21 23 31 32 33

Explanation: The continue statement skips printing when i is 2 and j is 2, so 22 is omitted.


What is the output of the following code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            if (i == 2) {

                i++;

                continue;

            }

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

            i++;

        }

    }

}

a) 0 1 2 3 4

b) 0 1 3 4

c) 1 2 3 4

d) 0 1 3 4 5


Answer: b) 0 1 3 4

Explanation: When i is 2, the continue statement skips the print statement for that iteration.


What will be the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        for (int i = 0; i < 4; i++) {

            if (i == 2) {

                continue;

            }

            System.out.println(i);

        }

    }

}

a) 0 1 2 3

b) 0 1 3

c) 0 1 2

d) 0 1 3 4


Answer: b) 0 1 3

Explanation: The continue statement skips printing when i is 2.


What is the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            i++;

            if (i == 4) {

                continue;

            }

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

        }

    }

}

a) 1 2 3 4

b) 1 2 3

c) 1 2 3 5

d) 2 3 5


Answer: a) 1 2 3 5

Explanation: When i is 4, the continue statement skips the print statement. 5 is printed because i increments after the continue.


What will be the output of the following code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            for (int j = 1; j <= 5; j++) {

                if (i == 3 && j == 3) {

                    break;

                }

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

            }

        }

    }

}

a) 11 12 13 21 22 23 31 32

b) 11 12 13 21 22 23 31

c) 11 12 13 21 22 23

d) 11 12 13 21 22


Answer: a) 11 12 13 21 22 23 31 32

Explanation: The break statement breaks out of the inner loop when i is 3 and j is 3, so 33 is not printed.


What is the result of the following Java code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {

            if (i == 2) {

                break;

            }

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

        }

    }

}

a) 0 1 2


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...