Software Development Exam  >  Software Development Tests  >  Basics of C++  >  Test: Loops - 2 - Software Development MCQ

Test: Loops - 2 - Software Development MCQ


Test Description

30 Questions MCQ Test Basics of C++ - Test: Loops - 2

Test: Loops - 2 for Software Development 2024 is part of Basics of C++ preparation. The Test: Loops - 2 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Loops - 2 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Loops - 2 below.
Solutions of Test: Loops - 2 questions in English are available as part of our Basics of C++ for Software Development & Test: Loops - 2 solutions in Hindi for Basics of C++ course. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free. Attempt Test: Loops - 2 | 30 questions in 60 minutes | Mock test for Software Development preparation | Free important questions MCQ to study Basics of C++ for Software Development Exam | Download free PDF with solutions
Test: Loops - 2 - Question 1

Which loop is used when the number of iterations is known in advance?

Detailed Solution for Test: Loops - 2 - Question 1

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.

Test: Loops - 2 - Question 2

Which loop is guaranteed to execute at least once?

Detailed Solution for Test: Loops - 2 - Question 2

The do-while loop is guaranteed to execute at least once because the condition is checked at the end of the loop. The loop body executes first, and then the condition is evaluated. If the condition is true, the loop continues; otherwise, it terminates.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Loops - 2 - Question 3

What is the purpose of the break statement in a loop?

Detailed Solution for Test: Loops - 2 - Question 3

The break statement is used to terminate the loop and transfer control to the next statement after the loop. It is often used in combination with a condition to exit the loop prematurely.

Test: Loops - 2 - Question 4

What is the purpose of the continue statement in a loop?

Detailed Solution for Test: Loops - 2 - Question 4

The continue statement is used to skip the current iteration of the loop and proceed to the next one. It is often used in combination with a condition to skip certain iterations based on specific criteria.

Test: Loops - 2 - Question 5

Which loop is suitable when the condition for termination is checked at the beginning?

Detailed Solution for Test: Loops - 2 - Question 5

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.

Test: Loops - 2 - Question 6

What is an infinite loop?

Detailed Solution for Test: Loops - 2 - Question 6

An infinite loop is a loop that executes forever unless terminated by an external factor, such as a break statement or a condition becoming false.

Test: Loops - 2 - Question 7

What is the output of the following code snippet?

for (int i = 0; i < 5; i++) {
    if (i == 3)
        break;
    cout << i << " ";
}

Detailed Solution for Test: Loops - 2 - Question 7

The loop iterates from 0 to 4. When the value of i becomes 3, the break statement is encountered, and the loop terminates. Therefore, the output is 0 1 2 4.

Test: Loops - 2 - Question 8

What is the output of the following code snippet?

int i = 0;
while (i < 5) {
    i++;
    if (i == 3)
        continue;
    cout << i << " ";
}

Detailed Solution for Test: Loops - 2 - Question 8

The loop iterates from 1 to 5. When the value of i becomes 3, the continue statement is encountered, causing the current iteration to be skipped. The output is 1 2 4 5.

Test: Loops - 2 - Question 9

Which loop structure allows the body of the loop to be executed first and then checks the condition?

Detailed Solution for Test: Loops - 2 - Question 9

The do-while loop allows the body of the loop to be executed first and then checks the condition. If the condition is true, the loop continues; otherwise, it terminates.

Test: Loops - 2 - Question 10

What is the output of the following code snippet?

int x = 10;
do {
    cout << x << " ";
    x--;
} while (x > 0);

Detailed Solution for Test: Loops - 2 - Question 10

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

Test: Loops - 2 - Question 11

What is the output of the following code snippet?

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

Detailed Solution for Test: Loops - 2 - Question 11

The loop iterates from 0 to 4, and on each iteration, the value of i is printed. Therefore, the output is 0 1 2 3 4.

Test: Loops - 2 - Question 12

What is the output of the following code snippet?

int x = 5;
while (x > 0) {
    cout << x << " ";
    x--;
}

Detailed Solution for Test: Loops - 2 - Question 12

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.

Test: Loops - 2 - Question 13

What is the output of the following code snippet?

int x = 5;
do {
    cout << x << " ";
    x--;
} while (x > 0);

Detailed Solution for Test: Loops - 2 - Question 13

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.

Test: Loops - 2 - Question 14

What is the output of the following code snippet?

int i = 1;
while (i <= 5) {
    cout << i << " ";
    i += 2;
}

Detailed Solution for Test: Loops - 2 - Question 14

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.

Test: Loops - 2 - Question 15

What is the output of the following code snippet?

int x = 10;
while (x >= 0) {
    if (x % 2 == 0)
        cout << x << " ";
    x--;
}

Detailed Solution for Test: Loops - 2 - Question 15

The loop starts with x equal to 10 and decrements x by 1 on each iteration. The loop continues as long as x is greater than or equal to 0. The loop prints the values of x only when x is even (divisible by 2), resulting in 10 8 6 4 2 0.

Test: Loops - 2 - Question 16

What is the output of the following code snippet?

int i = 0;
do {
    cout << i << " ";
    i++;
} while (i <= 0);

Detailed Solution for Test: Loops - 2 - Question 16

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.

Test: Loops - 2 - Question 17

What is the output of the following code snippet?

int i = 10;
while (i > 0) {
    cout << i << " ";
    i -= 3;
}

Detailed Solution for Test: Loops - 2 - Question 17

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.

Test: Loops - 2 - Question 18

What is the output of the following code snippet?

for (int i = 0; i < 5; i += 2) {
    cout << i << " ";
}

Detailed Solution for Test: Loops - 2 - Question 18

The loop iterates from 0 to 4 with a step of 2. On each iteration, the value of i is printed. Therefore, the output is 0 2 4.

Test: Loops - 2 - Question 19

What is the output of the following code snippet?

int x = 5;
do {
    cout << x << " ";
    x++;
} while (x < 5);

Detailed Solution for Test: Loops - 2 - Question 19

The condition x < 5 in the do-while loop is always true because x starts at 5 and is incremented on each iteration. Therefore, the loop will continue indefinitely, resulting in an infinite loop.

Test: Loops - 2 - Question 20

What is the output of the following code snippet?

int i = 0;
while (i < 5) {
    cout << i << " ";
    if (i == 2)
        break;
    i++;
}

Detailed Solution for Test: Loops - 2 - Question 20

The loop starts with i equal to 0 and increments i by 1 on each iteration. The loop continues as long as i is less than 5. On the third iteration, when i becomes 2, the break statement is encountered, and the loop terminates. Therefore, the output is 0 1 2.

Test: Loops - 2 - Question 21

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++;
    }
}

Detailed Solution for Test: Loops - 2 - Question 21

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.

Test: Loops - 2 - Question 22

What is the output of the following code snippet?

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 3; j++) {
        cout << i * j << " ";
    }
    cout << endl;
}

Detailed Solution for Test: Loops - 2 - Question 22

The outer loop iterates from 0 to 3, and the inner loop iterates from 0 to 2. On each iteration, the product of i and j is printed. Therefore, the output is 0 1 2 0 2 4 0 3 6.

Test: Loops - 2 - Question 23

What is the output of the following code snippet?

int x = 2;
while (x <= 8) {
    if (x % 2 == 0) {
        cout << x << " ";
        x += 2;
    } else {
        x++;
    }
}

Detailed Solution for Test: Loops - 2 - Question 23

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

Test: Loops - 2 - Question 24

What is the output of the following code snippet?

int i = 2;
while (i < 8) {
    if (i % 2 == 0) {
        cout << i << " ";
    }
    i++;
}

Detailed Solution for Test: Loops - 2 - Question 24

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.

Test: Loops - 2 - Question 25

What is the output of the following code snippet?

int i = 1;
while (i < 5) {
    if (i == 3) {
        i++;
        continue;
    }
    cout << i << " ";
    i++;
}

Detailed Solution for Test: Loops - 2 - Question 25

The loop starts with i equal to 1. If i is equal to 3, i is incremented by 1 using the continue statement. Otherwise, i is printed, and i is incremented by 1. The loop continues as long as i is less than 5. Therefore, the output is 1 2 4.

Test: Loops - 2 - Question 26

What is the output of the following code snippet?

int i = 1;
while (i < 6) {
    if (i % 2 == 0) {
        cout << i << " ";
    }
    i++;
}

Detailed Solution for Test: Loops - 2 - Question 26

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.

Test: Loops - 2 - Question 27

What is the output of the following code snippet?

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j)
            continue;
        cout << i << " " << j << endl;
    }
}

Detailed Solution for Test: Loops - 2 - Question 27

The outer loop iterates from 0 to 2, and the inner loop iterates from 0 to 2. If i is equal to j, the continue statement skips the current iteration. Otherwise, the values of i and j are printed. Therefore, the output is 0 1 0 2 1 0 1 2 2 0 2 1.

Test: Loops - 2 - Question 28

What is the output of the following code snippet?

int x = 1;
while (x <= 5) {
    if (x % 2 == 0) {
        cout << x << " ";
        break;
    }
    x++;
}

Detailed Solution for Test: Loops - 2 - Question 28

The loop starts with x equal to 1. If x is divisible by 2 (i.e., even), it is printed, and the loop terminates using the break statement. Therefore, the output is 1.

Test: Loops - 2 - Question 29

What is the output of the following code snippet?

int i = 1;
while (i <= 5) {
    if (i % 3 == 0) {
        cout << i << " ";
        break;
    }
    i++;
}

Detailed Solution for Test: Loops - 2 - Question 29

The loop starts with i equal to 1. If i is divisible by 3, it is printed, and the loop terminates using the break statement. Therefore, the output is 3.

Test: Loops - 2 - Question 30

What is the output of the following code snippet?

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (j == 3)
            break;
        cout << i << " " << j << endl;
    }
}

Detailed Solution for Test: Loops - 2 - Question 30

The outer loop iterates from 0 to 4, and the inner loop iterates from 0 to 3. The values of i and j are printed for each iteration, except when j is equal to 3, where the break statement is encountered, and the inner loop terminates. Therefore, the output is 0 0 0 1 0 2 0 3 1 0 1 1 1 2 1 3 2 0 2 1 2 2 2 3 3 0 3 1 3 2 3 3 4 0 4 1 4 2 4 3.

70 videos|45 docs|15 tests
Information about Test: Loops - 2 Page
In this test you can find the Exam questions for Test: Loops - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Loops - 2, EduRev gives you an ample number of Online tests for practice

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF

Top Courses for Software Development