Sunday, July 28, 2024

While Loop MCQ

 What is the primary purpose of a while loop in Java?

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

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

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

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


In a while loop, what happens if the condition 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 is the syntax to create a while loop in Java?

a) while { }

b) while () { }

c) while <> { }

d) while [] { }


Which part of the while loop is responsible for modifying the loop control variable?

a) Initialization

b) Condition

c) Iteration

d) Termination


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

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

b) The while loop always executes its body at least once.

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

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


Which statement is used to exit a while loop prematurely in Java?

a) terminate

b) skip

c) exit

d) break


What happens if the loop body in a while loop lacks an instruction to modify the loop control variable?

a) It results in a runtime error.

b) The loop will run indefinitely.

c) The loop will only execute once.

d) It leads to a compilation error.


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

a) Initialization

b) Condition

c) Iteration

d) Termination


How is the condition evaluated in a while loop?

a) Before each iteration

b) After each iteration

c) Only once at the start

d) It depends on the loop structure


What is the role of the condition in a while loop?

a) It defines when the loop should terminate.

b) It specifies the loop's body.

c) It initializes the loop control variable.

d) It updates the loop control variable.


Answers:


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

b) The loop body is skipped entirely.

b) while () { }

c) Iteration

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

d) break

b) The loop will run indefinitely.

b) Condition

a) Before each iteration

a) It defines when the loop should terminate.



MCQs on while Loop in Java

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 < 3) {

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

            i++;

        }

    }

}

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 loop prints i from 0 to 2, and then exits.


What will be the output of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 5;

        while (i > 0) {

            if (i == 3) {

                break;

            }

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

            i--;

        }

    }

}

a) 5 4 3

b) 5 4

c) 5 4 3 2 1

d) 5 4 3 2


Answer: b) 5 4

Explanation: The loop breaks when i equals 3, so 5 and 4 are printed.


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 < 3) {

            i++;

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

        }

    }

}

a) 1 2 3

b) 0 1 2 3

c) 1 2 3 4

d) 0 1 2


Answer: a) 1 2 3

Explanation: The loop increments i before printing, so it starts from 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 % 2 == 0) {

                i++;

                continue;

            }

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

            i++;

        }

    }

}

a) 1 3

b) 0 1 2 3 4

c) 1 2 3 4

d) 1 2 4


Answer: a) 1 3

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


What is the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 3;

        while (i > 0) {

            i--;

            if (i == 1) {

                continue;

            }

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

        }

    }

}

a) 2 1

b) 2 0

c) 2

d) 1 0


Answer: c) 2

Explanation: The continue statement skips the 1 value.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 3) {

            if (i == 1) {

                i++;

                continue;

            }

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

            i++;

        }

    }

}

a) 0 2

b) 0 1 2

c) 0 1 2 3

d) 1 2


Answer: a) 0 2

Explanation: The loop skips printing 1 due to the continue statement.


What will be the result of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        int j = 5;

        while (i < j) {

            i++;

            j--;

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

        }

    }

}

a) 1 4 2 3

b) 1 4 2 3 3 3

c) 1 4 2 3 3 2 2

d) 1 4 2 3 4 1


Answer: a) 1 4 2 3

Explanation: The values of i and j converge and are printed until i equals j.


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 < 3) {

            i++;

            if (i == 2) {

                break;

            }

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

        }

    }

}

a) 1

b) 1 2

c) 1 2 3

d) 0 1


Answer: a) 1

Explanation: The loop breaks when i equals 2, so only 1 is printed.


What is the result of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 10) {

            i += 2;

            if (i == 6) {

                continue;

            }

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

        }

    }

}

a) 2 4 8 10

b) 2 4 8 10

c) 2 4 8

d) 2 4 8 10


Answer: d) 2 4 8 10

Explanation: The continue statement skips the 6, so the output includes all numbers except 6.


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) {

            i++;

            if (i % 2 == 0) {

                continue;

            }

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

        }

    }

}

a) 1 3 5

b) 1 2 3 4

c) 0 2 4

d) 1 2 3


Answer: a) 1 3 5

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


What is the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        while (i < 10) {

            if (i % 2 == 0) {

                i++;

                continue;

            }

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

            i++;

        }

    }

}

a) 1 3 5 7 9

b) 2 4 6 8

c) 1 2 3 4 5 6 7 8 9

d) 1 3 5 7 9 10


Answer: a) 1 3 5 7 9

Explanation: The loop prints odd numbers and skips even numbers due to the continue statement.


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) {

            i++;

            if (i == 4) {

                break;

            }

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

        }

    }

}

a) 1 2 3

b) 1 2 3 4

c) 1 2 3 4 5

d) 1 2 3 4


Answer: a) 1 2 3

Explanation: The loop breaks when i equals 4, so the numbers 1 2 3 are printed.


What will the following code output?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        while (i < 4) {

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

            i++;

            if (i == 2) {

                continue;

            }

        }

        System.out.print(i);

    }

}

a) 1 2 3 4

b) 1 2 3

c) 1 2 3 3

d) 1 2 3 4


Answer: b) 1 2 3

Explanation: The continue statement skips printing 2, so the result is 1 2 3.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            i++;

            if (i == 3) {

                continue;

            }

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

        }

    }

}

a) 1 2 4 5

b) 1 2 3 4 5

c) 1 2 4

d) 0 1 2 4


Answer: a) 1 2 4 5

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


What is the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 5;

        while (i > 0) {

            i--;

            if (i % 2 != 0) {

                continue;

            }

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

        }

    }

}

a) 4 2 0

b) 4 2

c) 5 4 3 2 1

d) 5 4 3 2 1 0


Answer: a) 4 2 0

Explanation: The loop prints even numbers only due to the continue statement.




Difficult MCQs on while Loop in Java

What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        while (i <= 5) {

            if (i % 2 == 0) {

                i++;

                continue;

            }

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

            i++;

        }

    }

}

a) 1 3 5

b) 2 4 6

c) 1 2 3 4 5

d) 1 3


Answer: a) 1 3 5

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


What will be the result of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        while (i < 10) {

            if (i % 2 == 0) {

                i += 3;

            } else {

                i++;

            }

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

        }

    }

}

a) 2 6 10

b) 2 5 8

c) 4 8 12

d) 2 5 9


Answer: b) 2 5 8

Explanation: When i is even, it is incremented by 3; otherwise, it is incremented by 1. This pattern results in 2 5 8.


What will the output be for the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            if (i % 2 == 0) {

                i++;

                continue;

            }

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

            i += 2;

        }

    }

}

a) 1 3

b) 1 3 5

c) 0 2 4

d) 2 4


Answer: a) 1 3

Explanation: The continue statement skips even values, and only odd values 1 and 3 are printed.


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 % 2 == 0) {

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

            }

        }

    }

}

a) 2 4 6

b) 1 3 5

c) 2 4

d) 1 2 3 4 5


Answer: c) 2 4

Explanation: The while loop increments i before checking the condition and printing only even numbers.


What is the output of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 10;

        while (i > 0) {

            if (i % 3 == 0) {

                i -= 2;

            } else {

                i -= 1;

            }

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

        }

    }

}

a) 8 6 4 2 0

b) 9 7 5 3 1

c) 8 6 4 2

d) 10 8 6 4 2 0


Answer: a) 8 6 4 2 0

Explanation: If i is divisible by 3, it decreases by 2; otherwise, it decreases by 1. This sequence results in 8 6 4 2 0.


What is the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 5;

        while (i > 0) {

            if (i % 2 == 1) {

                i--;

                continue;

            }

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

            i -= 2;

        }

    }

}

a) 4 2

b) 5 3 1

c) 4 2 0

d) 5 3


Answer: a) 4 2

Explanation: The continue statement skips odd numbers. Only even numbers 4 and 2 are printed.


What will be the output of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        while (i < 6) {

            if (i % 2 == 1) {

                i += 2;

                continue;

            }

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

            i++;

        }

    }

}

a) 2 4

b) 1 3 5

c) 2 4 6

d) 1 2 3 4


Answer: a) 2 4

Explanation: Odd numbers are skipped due to the continue statement, resulting in only even numbers being printed.


What is the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 5;

        while (i > 0) {

            i--;

            if (i == 2) {

                continue;

            }

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

        }

    }

}

a) 4 3 1 0

b) 4 3 1

c) 5 4 3 1 0

d) 4 3 1 2


Answer: b) 4 3 1

Explanation: The continue statement skips printing when i equals 2, so the output is 4 3 1.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 5) {

            if (i % 2 == 0) {

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

            }

            i++;

        }

    }

}

a) 0 2 4

b) 1 3 5

c) 1 2 3 4

d) 2 4


Answer: a) 0 2 4

Explanation: The loop prints only even numbers from 0 to 4.


What is the output of the following code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 5;

        while (i > 0) {

            if (i % 3 == 0) {

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

            }

            i--;

        }

    }

}

a) 3 6

b) 3

c) 5 3

d) 0


Answer: b) 3

Explanation: The loop prints values divisible by 3 and i starts from 5.


What will be the output of this code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        while (i <= 10) {

            if (i % 2 == 0) {

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

            }

            i += 2;

        }

    }

}

a) 2 4 6 8 10

b) 1 3 5 7 9

c) 2 4 6 8

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


Answer: a) 2 4 6 8 10

Explanation: The loop increments i by 2 and prints only even numbers.


What will be the result of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 0;

        while (i < 10) {

            if (i % 3 == 0) {

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

            }

            i += 2;

        }

    }

}

a) 0 3 6 9

b) 0 2 4 6 8

c) 1 4 7

d) 3 6 9


Answer: a) 0 3 6 9

Explanation: The loop prints numbers divisible by 3 with an increment of 2.


What is the output of this code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 5;

        while (i > 0) {

            if (i == 3) {

                i--;

                continue;

            }

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

            i--;

        }

    }

}

a) 5 4 3 2 1

b) 5 4 2 1

c) 5 4 3 2

d) 5 4 3 1


Answer: b) 5 4 2 1

Explanation: When i equals 3, the continue statement skips printing 3, and the loop continues with 2 and 1.


What will be the output of the following code?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 1;

        while (i < 10) {

            if (i % 2 == 0) {

                i += 2;

                continue;

            }

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

            i++;

        }

    }

}

a) 1 3 5 7 9

b) 1 2 3 4 5 6 7 8 9

c) 1 4 7

d) 1 3 5 7


Answer: a) 1 3 5 7 9

Explanation: The loop increments i by 2 if i is even and prints odd numbers.


What is the result of the following code snippet?


java

Copy code

public class Main {

    public static void main(String[] args) {

        int i = 10;

        while (i >= 0) {

            if (i % 2 != 0) {

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

            }

            i--;

        }

    }

}

a) 10 8 6 4 2 0

b) 9 7 5 3 1

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

d) 10 8 6 4 2


Answer: b) 9 7 5 3 1

Explanation: The loop prints odd numbers from 10 to 0 by decrementing i each time.



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