All Exams  >   EmSAT Achieve  >   Java for EmSAT Achieve  >   MCQ Questions

Loops MCQs for EmSAT Achieve Exam

It covers all Important Questions with answers on Loops for the EmSAT Achieve exam. The questions are based on important topics. Details about the questions:
  • Topic: Loops
  • Type of Questions: MCQs with solutions
  • Number of Questions: 40
  • You can attempt them on EduRev to score high in EmSAT Achieve exam.

What will be the output of the following code?
```java
int x = 5;
while (x > 0) {
    System.out.print(x + " ");
    x -= 2;
}
```
  • a)
    5 3 1
  • b)
    5 4 3 2 1
  • c)
    5 5 5 5 5
  • d)
    1 3 5
Correct answer is option 'B'. Can you explain this answer?

This code snippet is incomplete and will result in a compilation error. The while loop condition is not complete, as it is missing the comparison operator and the value it should be compared to.

To get the desired result, the code should be corrected as follows:

```java
int x = 5;
while (x > 0) {
System.out.println(x);
x--;
}
```

With this correction, the output will be:
```
5
4
3
2
1
```

What is the purpose of the 'break' statement in a loop?
  • a)
    To terminate the current iteration of the loop
  • b)
    To skip the remaining statements in the loop and move to the next iteration
  • c)
    To terminate the loop entirely
  • d)
    To restart the loop from the beginning
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Terminating the current iteration:
- The purpose of the break statement in a loop is to terminate the current iteration of the loop.
- When the break statement is encountered within a loop, it immediately exits the loop and moves to the code following the loop.

Example:
- For example, in a for loop, if a certain condition is met and you want to stop the loop from running any further, you can use the break statement.

Usage:
- The break statement is commonly used when you want to exit a loop prematurely based on certain conditions.
- It helps in controlling the flow of the loop and allows you to terminate the loop when a specific condition is met.

Other options:
- Option (b) is incorrect as the break statement does not skip the remaining statements in the loop; it only terminates the current iteration.
- Option (c) is incorrect as the break statement does not terminate the loop entirely; it only exits the current iteration.
- Option (d) is incorrect as the break statement does not restart the loop from the beginning; it simply ends the current iteration.

Conclusion:
- In conclusion, the break statement is a useful tool in programming loops as it allows you to control the flow of the loop and exit it when needed. It helps in improving the efficiency and readability of your code by terminating iterations based on specific conditions.

What will be the output of the following code?
```java
int n = 3;
int i = 0;
do {
    System.out.print(n + " ");
    n--;
    i++;
} while (i < n);
```
  • a)
    3 2 1
  • b)
    3 2
  • c)
    3 1
  • d)
    3
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code prints the values of 'n' from 3 to 1 because the condition 'i < n' is true for those values. The loop terminates when 'i' becomes 3, and the condition becomes false.

What is the output of the following code snippet?
int i = 0;
while (i < 5) {
    System.out.print(i + " ");
    i++;
}
  • a)
    0 1 2 3 4
  • b)
    1 2 3 4 5
  • c)
    0 1 2 3 4 5
  • d)
    This code results in an infinite loop
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code snippet initializes 'i' to 0 and then enters the while loop. It prints the value of 'i', increments 'i' by 1, and repeats the process until 'i' is no longer less than 5.

In Java, which loop construct is best suited when the number of iterations is known in advance?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    if-else statement
Correct answer is option 'A'. Can you explain this answer?

Best Loop Construct for Known Number of Iterations:

Introduction:
In Java programming language, there are various loop constructs available to execute a set of statements repeatedly. Each loop construct has its own purpose and usage scenarios. When the number of iterations is known in advance, the best-suited loop construct is the for loop (option A).

Explanation:

1. for loop:
- The for loop in Java is used when the number of iterations is known in advance.
- It consists of three parts: initialization, condition, and increment/decrement.
- The loop executes until the condition becomes false.
- The initialization part is executed only once at the beginning of the loop.
- The condition is checked before each iteration, and if it evaluates to true, the loop body is executed.
- After each iteration, the increment/decrement part is executed.
- Example syntax:
```
for (initialization; condition; increment/decrement) {
// Statements to be executed
}
```
- The for loop is efficient and concise when the number of iterations is fixed.

2. while loop:
- The while loop is used when the number of iterations is not known in advance.
- It continues executing until the condition becomes false.
- The condition is checked before each iteration, and if it evaluates to true, the loop body is executed.
- Example syntax:
```
while (condition) {
// Statements to be executed
}
```
- The while loop is suitable when the number of iterations is uncertain or depends on some condition.

3. do-while loop:
- The do-while loop is similar to the while loop, but it guarantees the execution of the loop body at least once.
- It executes the loop body first and then checks the condition.
- If the condition is true, it continues executing; otherwise, it terminates.
- Example syntax:
```
do {
// Statements to be executed
} while (condition);
```
- The do-while loop is useful when the loop body must be executed at least once, regardless of the condition.

4. if-else statement:
- The if-else statement is not a loop construct; it is used for conditional branching.
- It allows executing a set of statements based on a condition.
- The if-else statement is not suitable for repeating a set of statements for a known number of iterations.

Conclusion:
In Java, when the number of iterations is known in advance, the best-suited loop construct is the for loop. It provides a concise and efficient way to execute a set of statements repeatedly for a fixed number of iterations.

Which loop construct in Java is most suitable when the number of iterations is unknown?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    switch statement
Correct answer is option 'B'. Can you explain this answer?

While loop is the most suitable loop construct in Java when the number of iterations is unknown.

Explanation:

The while loop in Java is a control flow statement that allows us to repeatedly execute a block of code as long as a given condition is true. It is commonly used when the number of iterations is unknown or not fixed. Here are the reasons why the while loop is the most suitable in such situations:

1. Flexibility:
The while loop provides flexibility as it allows us to execute a block of code based on a condition that can be dynamically evaluated. This means that we can continue looping until a specific condition is met, and the number of iterations can vary depending on the situation.

2. Condition-based:
The while loop is condition-based, meaning it checks the condition before executing the block of code. If the condition is false initially, the loop will not execute at all. This makes it suitable when the number of iterations is unknown, as we can check a condition that determines when to exit the loop.

3. Unknown Iterations:
When the number of iterations is unknown, using a for loop or a do-while loop may not be suitable. A for loop requires a fixed number of iterations, and a do-while loop executes the code block at least once before checking the condition. The while loop, on the other hand, allows us to check the condition before each iteration, making it ideal when the exact number of iterations is uncertain.

4. Dynamic Termination:
The while loop allows for dynamic termination by modifying the condition during the loop execution. This means that we can change the condition within the loop based on certain criteria, allowing us to exit the loop when necessary. This flexibility is crucial when the number of iterations is unknown.

In conclusion, the while loop is the most suitable loop construct in Java when the number of iterations is unknown. Its flexibility, condition-based nature, ability to handle unknown iterations, and dynamic termination make it the preferred choice in such situations.

Which loop structure is best suited for iterating over an array or a collection?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    nested loop
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The for loop structure is best suited for iterating over an array or a collection since it provides a concise way to define the initialization, condition, and update statements in a single line.

What is the output of the following code snippet?
int i = 10;
do {
    System.out.print(i + " ");
    i -= 3;
} while (i > 0);
  • a)
    10 7 4 1
  • b)
    1 4 7 10
  • c)
    10 8 6 4 2
  • d)
    This code results in an infinite loop
Correct answer is option 'A'. Can you explain this answer?

There is a syntax error in the code snippet provided. The code is missing a closing parenthesis after the "System.out.print(i" statement. Once the missing parenthesis is added, the code snippet would look like this:

```
int i = 10;
do {
System.out.print(i);
} while (i <>
```

In this case, the output would be "10".

What is the output of the following code snippet?
int i = 0;
while (i < 5) {
    if (i % 2 == 0) {
        i++;
        continue;
    }
    System.out.print(i + " ");
    i++;
}
  • a)
    0 1 2 3 4
  • b)
    1 3
  • c)
    1 3 5
  • d)
    1 3 5 7 9
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The while loop starts with 'i' initialized to 0 and increments 'i' by 1 in each iteration. If 'i' is even, it skips the remaining statements using the continue statement. Therefore, only odd values of 'i' are printed.

What is the output of the following code snippet?
int x = 3;
while (x > 0) {
    System.out.print(x + " ");
    x--;
}
  • a)
    3 2 1
  • b)
    1 2 3
  • c)
    0 1 2
  • d)
    1 1 1
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The while loop starts with 'x' initialized to 3 and prints the value of 'x'. It then decrements 'x' by 1 in each iteration until 'x' is no longer greater than 0.

What is the output of the following code snippet?
int sum = 0;
for (int i = 1; i <= 5; i++) {
    sum += i;
}
System.out.println(sum);
  • a)
    5
  • b)
    10
  • c)
    15
  • d)
    25
Correct answer is option 'B'. Can you explain this answer?

The code snippet is incomplete and the output cannot be determined. The condition for the loop is missing, so it is unclear how many times the loop will iterate. Additionally, there is no code inside the loop that modifies the value of the variable "sum", so the final value of "sum" cannot be determined either.

What is the output of the following code snippet?
int i = 0;
while (i < 3) {
    int j = 0;
    while (j < 2) {
        System.out.print(i + "" + j + " ");
        j++;
    }
    i++;
}
  • a)
    00 01 10 11 20 21
  • b)
    00 10 20 01 11 21
  • c)
    00 01 11 02 12 22
  • d)
    00 01 10 11 12 20 21 22
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The outer while loop initializes 'i' to 0 and increments 'i' by 1 in each iteration. The inner while loop initializes 'j' to 0 and increments 'j' by 1 in each iteration. The combination of 'i' and 'j' is printed in each iteration of the inner loop.

What will be the output of the following code?
```java
int i = 0;
int sum = 0;
while (i < 5) {
    sum += i;
    i++;
}
System.out.print(sum);
```
  • a)
    0
  • b)
    5
  • c)
    10
  • d)
    15
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code initializes 'i' as 0 and calculates the sum of 'i' in each iteration until 'i' becomes 5. The final sum will be 0 + 1 + 2 + 3 + 4 = 10.

What will be the output of the following code?
```java
int x = 5;
do {
    System.out.print(x + " ");
    x++;
} while (x < 10);
```
  • a)
    5 6 7 8 9
  • b)
    6 7 8 9 10
  • c)
    5 6 7 8 9 10
  • d)
    6 7 8 9
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code starts with 'x' as 5 and prints its value in each iteration while incrementing it by 1. The loop terminates when 'x' becomes 10, and the condition becomes false.

Chapter doubts & questions for Loops - Java for EmSAT Achieve 2026 is part of EmSAT Achieve exam preparation. The chapters have been prepared according to the EmSAT Achieve exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for EmSAT Achieve 2026 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Loops - Java for EmSAT Achieve in English & Hindi are available as part of EmSAT Achieve exam. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.

Java for EmSAT Achieve

60 videos|37 docs|12 tests

Top Courses EmSAT Achieve