All Exams  >   Software Development  >   Basics of C++  >   All Questions

All questions of Loops for Software Development Exam

Which of the following loop constructs is guaranteed to execute at least once?
  • a)
    while loop
  • b)
    do-while loop
  • c)
    for loop
  • d)
    if-else statement
Correct answer is option 'B'. Can you explain this answer?

Explanation:

The correct answer is option 'B' - do-while loop.

Reasoning:

The do-while loop is the only loop construct among the given options that is guaranteed to execute at least once. This is because the do-while loop first executes the statements within its body and then checks the loop condition. If the condition is true, the loop continues to execute, otherwise, it terminates.

Comparison with other loop constructs:

a) while loop: The while loop first checks the loop condition, and if it is true, it executes the statements within its body. If the condition is false initially, the loop will not execute at all. Therefore, it is not guaranteed to execute at least once.

c) for loop: The for loop also checks the loop condition before executing the statements within its body. If the condition is false initially, the loop will not execute at all. Therefore, it is also not guaranteed to execute at least once.

d) if-else statement: The if-else statement is not a loop construct but a conditional statement. It does not involve any iteration or repetition. It executes a block of code based on a condition, but it does not guarantee execution.

Example of do-while loop:

```java
int count = 0;
do {
System.out.println("Count: " + count);
count++;
} while (count < />
```

In this example, the loop will execute at least once because the condition `count < 5`="" is="" not="" checked="" until="" after="" the="" first="" iteration.="" therefore,="" even="" if="" the="" condition="" is="" initially="" false,="" the="" loop="" body="" will="" still="" be="" executed="" />

Conclusion:

The do-while loop is the only loop construct among the given options that is guaranteed to execute at least once, making it the correct answer to the question.
1 Crore+ students have signed up on EduRev. Have you? Download the App

Which of the following statements is true about nested loops?
  • a)
    Nested loops cannot be used in C++
  • b)
    Only for loops can be nested, while loops cannot
  • c)
    Both while loops and for loops can be nested
  • d)
    Nested loops can only execute a fixed number of times
Correct answer is option 'C'. Can you explain this answer?

Nested loops are loops that are placed inside other loops. They allow us to repeat a set of instructions multiple times within another set of instructions. In programming, both while loops and for loops can be nested.

Explanation:
Nested loops are commonly used when we need to perform repetitive tasks that require multiple levels of iteration. They allow us to iterate over a set of values for one variable while iterating over another set of values for another variable.

Benefits of Nested Loops:
Nested loops provide several benefits in programming, including:

1. Complex Iteration: Nested loops allow us to iterate over complex data structures, such as multi-dimensional arrays or matrices. For example, we can use a nested loop to iterate over the rows and columns of a 2D array.

2. Pattern Generation: Nested loops can be used to generate patterns or sequences of numbers. By controlling the number of iterations and the values of the loop variables, we can create various patterns, such as triangles or squares.

3. Search and Filtering: Nested loops can be used to search for specific elements or perform filtering operations. For example, we can use nested loops to search for a particular value in a 2D array or to filter a list based on certain conditions.

Example:
Here is an example of a nested for loop in C programming language:

```
for (int i = 1; i <= 3;="" i++)="">
for (int j = 1; j <= 3;="" j++)="">
printf("%d ", i * j);
}
printf("\n");
}
```

This nested loop will iterate over the values of `i` from 1 to 3 and the values of `j` from 1 to 3. It will print the product of `i` and `j` on each iteration, resulting in the following output:

```
1 2 3
2 4 6
3 6 9
```

As we can see, the nested loop allows us to perform a repetitive task (printing the product of `i` and `j`) multiple times within the outer loop.

Therefore, the correct statement is option 'C': Both while loops and for loops can be nested.

What is the purpose of the continue statement in a loop?
  • a)
    It terminates the loop and transfers control to the next statement after the loop.
  • b)
    It continues to the next iteration of the loop.
  • c)
    It skips the current iteration and proceeds to the next one.
  • d)
    It restarts the loop from the beginning.
Correct answer is option 'B'. Can you explain this answer?

Explanation:

Purpose of the continue statement in a loop:
The continue statement in a loop is used to skip the current iteration and proceed to the next one. It allows the programmer to control the flow of the loop by skipping certain iterations based on certain conditions.

Function of the continue statement:
- When the continue statement is encountered in a loop, the rest of the code within the current iteration is skipped.
- The loop then continues with the next iteration, evaluating the loop condition again.

Example:
python
for i in range(1, 6):
if i == 3:
continue
print(i)
In this example, when the value of `i` is equal to 3, the `continue` statement is executed, skipping the `print(i)` statement for that iteration. The loop then continues with the next iteration.

Benefits of using the continue statement:
- It helps in improving the efficiency of the loop by skipping unnecessary iterations.
- It allows for better control over the flow of the loop based on specific conditions.
- It can simplify the logic of the loop by allowing the programmer to focus on the essential parts of the iteration.
Overall, the continue statement in a loop is a powerful tool for controlling the execution flow and optimizing the performance of loops in programming.

What is the purpose of the continue statement in a loop?
  • a)
    To terminate the loop and skip the remaining iterations
  • b)
    To skip the current iteration and continue with the next iteration
  • c)
    To exit the program completely
  • d)
    To execute a different block of code
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
To skip the current iteration and continue with the next iteration
Explanation: The continue statement is used to skip the current iteration and continue with the next iteration of the loop. It is typically used when a certain condition is met, and you want to skip some code within the loop but continue with the next iteration.

What will be the output of the following code snippet?
int x = 10;
while (x > 0) {
    x--;
    if (x % 3 == 0)
        continue;
    cout << x << " ";
}
  • a)
    9 8 7 5 4 2 1
  • b)
    10 9 8 7 6 5 4 3 2 1
  • c)
    9 8 7 6 5 4 2 1
  • d)
    10 9 8 7 5 4 2 1
Correct answer is option 'C'. Can you explain this answer?

The code snippet is incomplete and lacks a condition in the while loop. Without a condition, the loop will run indefinitely, causing an infinite loop. Therefore, it is not possible to determine the output of the code snippet without knowing the condition.

Which loop construct is best suited when the number of iterations is unknown?
  • a)
    while loop
  • b)
    do-while loop
  • c)
    for loop
  • d)
    if-else statement
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The while loop is best suited when the number of iterations is unknown. It continues to execute as long as the condition specified in the loop header remains true.

What will be the output of the following code snippet?
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= i; j++) {
        cout << i << " ";
    }
}
  • a)
    1 2 2 3 3 3
  • b)
    1 2 3
  • c)
    1 1 2 1 2 3
  • d)
    1 1 1 2 2 3
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The outer for loop iterates over the values of i from 1 to 3. The inner for loop iterates over the values of j from 1 to i. In each iteration, the value of i is printed. The loop prints the values 1, 1, 2, 1, 2, and 3.

What will be the output of the following code snippet?
int i = 1;
do {
    cout << i << " ";
    i *= 2;
} while (i <= 5);
  • a)
    1 2 3 4 5
  • b)
    1 2 4
  • c)
    1 2 4 8
  • d)
    1
Correct answer is option 'B'. Can you explain this answer?

The code snippet is incomplete and does not provide the complete expression after "cout". Therefore, it is not possible to determine the output of the code snippet.

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

Explanation:
The given code snippet initializes a variable i to 0 and then enters a while loop that increments i by 1 in each iteration. If i equals 3, the continue statement is executed, which skips the rest of the loop body and goes to the next iteration.

Details:
- The initial value of i is 0.
- In the first iteration, i is incremented to 1 and printed.
- In the second iteration, i is incremented to 2 and printed.
- In the third iteration, i is incremented to 3. Since i equals 3, the continue statement is executed, skipping the print statement for 3.
- In the fourth iteration, i is incremented to 4 and printed.
- In the fifth iteration, i is incremented to 5 and printed.

Final Output:
- The output of the code snippet is: 1 2 4 5
Therefore, the correct answer is option B - 1 2 4 5.

Which loop is guaranteed to execute at least once?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    None of the above
Correct answer is option 'C'. Can you explain this answer?

The Do-while Loop

The do-while loop is the loop guaranteed to execute at least once. It is a control flow statement that allows code to be executed repeatedly based on a given condition. The structure of a do-while loop consists of a block of code enclosed in curly braces, followed by the keyword "do", a set of statements to be executed, and the keyword "while" followed by a condition. The condition is evaluated at the end of the loop, and if it evaluates to true, the loop will repeat.

Structure:
The structure of a do-while loop is as follows:
```
do {
// code to be executed
} while (condition);
```

Execution:
1. The code within the loop is executed first, regardless of the condition.
2. After the code is executed, the condition is checked.
3. If the condition is true, the loop will repeat and the code will be executed again.
4. If the condition is false, the loop will terminate, and the program will continue to the next statement after the loop.

At least one execution:
The key difference between a do-while loop and other loops, such as the for loop and the while loop, is that the do-while loop guarantees the execution of its code block at least once. This is because the condition is evaluated at the end of the loop, meaning the code block will always execute at least once before the condition is checked.

In contrast, a for loop and a while loop check the condition before executing the code block. If the condition is false initially, the loop will not execute at all. This is why the do-while loop is the only loop that is guaranteed to execute at least once.

Use cases:
The do-while loop is often used when we want to ensure that a block of code is executed at least once, regardless of the condition. It is commonly used in situations where we need to take user input or validate input before proceeding with the rest of the program.

Overall, the do-while loop provides flexibility and ensures that a certain code block is executed at least once, making it a valuable tool in programming.

Which loop construct is most suitable when the number of iterations is known in advance?
  • a)
    while loop
  • b)
    do-while loop
  • c)
    for loop
  • d)
    if-else statement
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The for loop is most suitable when the number of iterations is known in advance, as it provides a compact way to specify the initialization, condition, and update expressions in a single line.

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

Sonal Yadav answered
The code initializes the variable 'x' with a value of 5. The while loop iterates as long as 'x' is greater than 0. In each iteration, the value of 'x' is printed, and then it is decremented by 1. The loop continues until 'x' becomes 0.

What will be the output of the following code snippet?
for (int i = 0; i < 5; i++) {
    if (i == 2)
        continue;
    cout << i << " ";
}
  • a)
    0 1 2 3 4
  • b)
    0 1 3 4
  • c)
    0 1 2 3
  • d)
    0 1 3
Correct answer is option 'B'. Can you explain this answer?

The code snippet provided is incomplete and does not specify the condition for the for loop. Therefore, it is not possible to determine the output without additional information.

What will be the output of the following code snippet?
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        cout << i + j << " ";
    }
}
  • a)
    0 1 1 2 2 3
  • b)
    0 1 2 3 4 5
  • c)
    1 2 3 4 5 6
  • d)
    1 2 2 3 3 4
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The outer for loop iterates over the values of i from 0 to 2. The inner for loop iterates over the values of j from 0 to 1. In each iteration, the sum of i and j is printed. The loop prints the values 0, 1, 1, 2, 2, 3.

What is the output of the following code snippet?
int i = 1;
while (i <= 5) {
    cout << i << " ";
    i += 2;
}
  • a)
    1 2 3 4 5
  • b)
    1 3 5
  • c)
    2 4 6
  • d)
    1 3
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The loop starts with i equal to 1 and increments i by 2 on each iteration. The loop continues as long as i is less than or equal to 5. The output is 1 3 5.

What will be the output of the following code snippet?
int n = 0;
while (n < 5) {
    cout << n << " ";
    if (n == 2)
        break;
    n++;
}
  • a)
    0 1 2
  • b)
    0 1 2 3 4
  • c)
    0 1 2 3
  • d)
    0 1 2 3 4 5
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The while loop iterates as long as n is less than 5. In each iteration, the value of n is printed, and then it is incremented by 1. The loop breaks when n becomes 2 due to the break statement. The loop prints the values 0, 1, 2, and 3.

What is the output of the following code snippet?
int i = 0;
do {
    cout << i << " ";
    i++;
} while (i <= 0);
  • a)
    0
  • b)
    0 1
  • c)
    1
  • d)
    The code will result in an infinite loop.
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The loop starts with i equal to 0 and prints its value. Then, i is incremented by 1. Since the condition i <= 0 is not satisfied, the loop terminates. Therefore, the output is 0.

What is the output of the following code snippet?
int i = 2;
while (i < 8) {
    if (i % 2 == 0) {
        cout << i << " ";
    }
    i++;
}
  • a)
    2 4 6
  • b)
    2 4 6 8
  • c)
    2 4 6 8 10
  • d)
    4 6 8
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The loop starts with i equal to 2. If i is divisible by 2 (i.e., even), it is printed. i is then incremented by 1. The loop continues as long as i is less than 8. Therefore, the output is 2 4 6.

What is an infinite loop?
  • a)
    A loop that executes a fixed number of times
  • b)
    A loop that executes an unknown number of times
  • c)
    A loop that never terminates
  • d)
    A loop that only executes once
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
An infinite loop is a loop that never terminates. It keeps executing indefinitely unless interrupted by an external factor, such as a break statement or a condition that causes the loop to terminate.

What is the output of the following code snippet?
int i = 10;
while (i > 0) {
    cout << i << " ";
    i -= 3;
}
  • a)
    10 7 4 1
  • b)
    10 8 6 4 2
  • c)
    10 7 4
  • d)
    10 7 4 1 0
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The loop starts with i equal to 10 and prints its value. Then, i is decremented by 3. The loop continues as long as i is greater than 0. The loop prints the values of i on each iteration, resulting in 10 7 4 1.

What is the output of the following code snippet?
int i = 1;
while (i < 6) {
    if (i % 2 == 0) {
        cout << i << " ";
        i += 2;
    } else {
        i++;
    }
}
  • a)
    2 4 6
  • b)
    2 4
  • c)
    1 3 5
  • d)
    1 2 3 4 5
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The loop starts with i equal to 1. If i is divisible by 2 (i.e., even), it is printed, and i is incremented by 2. Otherwise, i is incremented by 1. The loop continues as long as i is less than 6. Therefore, the output is 2 4.

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

Sonal Yadav answered
The do-while loop is executed at least once because the condition n > 0 is checked at the end of the loop. In each iteration, the value of n is printed, and then it is decremented by 1. The loop continues until n becomes 0.

Which loop is suitable when the condition for termination is checked at the beginning?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    None of the above
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The do-while loop is suitable when the condition for termination is checked at the end. This ensures that the loop body is executed at least once before the condition is evaluated.

What will be the output of the following code snippet?
int i = 5;
do {
    cout << i << " ";
    i--;
} while (i > 0);
  • a)
    5 4 3 2 1
  • b)
    5
  • c)
    1 2 3 4 5
  • d)
    1
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The do-while loop is executed as long as i is greater than 0. In each iteration, the value of i is printed, and then it is decremented by 1. The loop continues until i becomes 0.

What will be the output of the following code snippet?
int i = 0;
do {
    cout << i << " ";
    i++;
} while (i < 5);
  • a)
    0 1 2 3 4
  • b)
    0 1 2 3
  • c)
    0 1 2 3 4 5
  • d)
    1 2 3 4
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The do-while loop iterates as long as i is less than 5. In each iteration, the value of i is printed, and then it is incremented by 1. The loop prints the values 0, 1, 2, 3, and 4.

What will be the output of the following code snippet?
int i = 0;
while (i < 3) {
    for (int j = 0; j < 2; j++) {
        cout << i + j << " ";
        if (i + j == 2)
            break;
    }
    i++;
}
  • a)
    0 1 1 2
  • b)
    0 1 2 3
  • c)
    1 2 2 3
  • d)
    0 1 2
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The while loop iterates as long as i is less than 3. In each iteration, the value of i is printed, and then the inner for loop iterates over the values of j from 0 to 1. The sum of i and j is printed in each inner loop iteration. The loop prints the values 0, 1, 2.

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

Sonal Yadav answered
The loop starts with x equal to 5 and prints its value. Then, x is decremented by 1, and the loop continues as long as x is greater than 0. The loop prints the values of x on each iteration, resulting in 5 4 3 2 1.

What will be the output of the following code snippet?
int i = 1;
do {
    cout << i << " ";
    i *= 3;
} while (i <= 10);
  • a)
    1 2 3 4 5 6 7 8 9 10
  • b)
    1 3 9
  • c)
    1 3 6 9
  • d)
    1 3 6
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The do-while loop is executed as long as i is less than or equal to 10. In each iteration, the value of i is printed, and then it is multiplied by 3. The loop prints the values 1, 3, and 9.

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

Sonal Yadav answered
The loop starts with x equal to 5 and prints its value. Then, x is decremented by 1, and the loop continues as long as x is greater than 0. The loop prints the values of x on each iteration, resulting in 5 4 3 2 1.

Which loop construct is used to read an unknown number of inputs until a certain condition is met?
  • a)
    while loop
  • b)
    do-while loop
  • c)
    for loop
  • d)
    if-else statement
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The while loop is commonly used to read an unknown number of inputs until a certain condition is met. The loop continues to execute as long as the condition specified in the loop header remains true.

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

Sonal Yadav answered
The loop starts with i equal to 1. If i is divisible by 2 (i.e., even), it is printed. i is then incremented by 1. The loop continues as long as i is less than 6. Therefore, the output is 2 4 6.

What will be the output of the following code snippet?
int n = 0;
while (n < 5) {
    cout << n << " ";
    n++;
    if (n == 3)
        break;
}
  • a)
    0 1 2
  • b)
    0 1 2 3 4
  • c)
    0 1
  • d)
    0 1 2 3
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The while loop iterates as long as n is less than 5. In each iteration, the value of n is printed, and then it is incremented by 1. The loop breaks when n becomes 3 due to the break statement.

What will be the output of the following code snippet?
int x = 5;
while (x > 0) {
    x--;
    if (x == 2)
        break;
    cout << x << " ";
}
  • a)
    4 3
  • b)
    4 3 2
  • c)
    3
  • d)
    4
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The while loop decrements the value of x until it becomes 2. In each iteration, the value of x is printed, and then it is decremented by 1. The loop breaks when x becomes 2 due to the break statement.

Which loop is used when the number of iterations is known in advance?
  • a)
    for loop
  • b)
    while loop
  • c)
    do-while loop
  • d)
    None of the above
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The for loop is used when the number of iterations is known in advance. It consists of three parts: initialization, condition, and increment/decrement. The loop body executes until the condition becomes false.

What will be the output of the following code snippet?
int x = 10;
while (x > 0) {
    x -= 3;
    if (x % 2 == 0)
        continue;
    cout << x << " ";
}
  • a)
    7 4 1
  • b)
    10 7 4 1
  • c)
    7 1
  • d)
    10 4
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The while loop iterates as long as x is greater than 0. In each iteration, x is decremented by 3. The if statement checks if x is divisible by 2, and if so, the continue statement skips the remaining code in the current iteration. The loop prints the values 7, 4, and 1.

Chapter doubts & questions for Loops - Basics of C++ 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

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

Basics of C++

70 videos|45 docs|15 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev