Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Assignment: Loops in C++

Assignment: Loops in C++ | Basics of C++ - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which loop in C++ executes the statements at least once, even if the condition is false?
(a) for loop
(b) while loop
(c) do-while loop
(d) switch loop

Ans. (c)

Q.2. Which statement is used to exit a loop and skip the remaining iterations?
(a) break
(b) continue
(c) return
(d) exit

Ans. (a)

Q.3. What will be the output of the following code snippet?
int i = 0;
while (i < 5) {
    cout << i << " ";
    i++;
}

(a) 0 1 2 3 4
(b) 1 2 3 4 5
(c) 0 1 2 3
(d) 1 2 3 4

Ans. (a)

Q.4. Which loop structure is ideal for iterating over an array?
(a) for loop
(b) while loop
(c) do-while loop
(d) switch loop

Ans. (a)

Q.5. What is the purpose of the continue statement in a loop?
(a) It terminates the loop completely.
(b) It skips the current iteration and continues with the next iteration.
(c) It exits the program.
(d) It prints a message to the console.

Ans. (b)

High Order Thinking Questions (HOTS)

Q.1. Write a C++ code snippet to find the sum of all even numbers between 1 and 100 using a loop.

C++ code snippet to find the sum of all even numbers between 1 and 100 using a loop:

int sum = 0;

for (int i = 2; i <= 100; i += 2) {

    sum += i;

}

cout << "Sum of even numbers between 1 and 100: " << sum << endl;

Q.2. Explain the difference between the while loop and the do-while loop in C++.

The while loop in C++ checks the condition before executing the loop body, whereas the do-while loop checks the condition after executing the loop body. This means that the do-while loop is guaranteed to execute at least once, even if the condition is initially false.

Q.3. Write a C++ code snippet to display the Fibonacci series using a loop.

C++ code snippet to display the Fibonacci series using a loop:

int n;

cout << "Enter the number of terms: ";

cin >> n;


int first = 0, second = 1, next;

cout << "Fibonacci series: ";


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

    cout << first << " ";


    next = first + second;

    first = second;

    second = next;

}

Q.4. How can you create an infinite loop in C++? Provide an example.

An infinite loop in C++ can be created by omitting the loop condition or providing a condition that is always true. For example:
// Infinite loop

while (true) {

    // Loop body

}

Q.5. Explain the concept of nested loops in C++. Give an example to illustrate their usage.

Nested loops in C++ are loops within loops. They allow you to repeat a set of statements multiple times. For example, a loop inside another loop. Here's an example of nested loops to display a multiplication table:

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

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

        cout << i * j << "\t";

    }

    cout << endl;

}

Fill in the Blanks

Q.1. In C++, the for loop consists of three parts: ______________, ______________, and ______________.

Ans. initialization, condition, increment/decrement

Q.2. The ______________ statement is used to skip the remaining iterations and continue with the next iteration of the loop.

Ans. continue

Q.3. A ______________ is a loop that executes its statements at least once, even if the condition is false.

Ans. do-while loop

Q.4. The ______________ statement is used to exit a loop completely.

Ans. break

Q.5. The ______________ loop is ideal for situations where you want the loop to execute at least once, regardless of the condition.

Ans. do-while

True or False

Q.1. In C++, the for loop can only iterate over arrays.

Ans. False

Q.2. The while loop is more suitable when the number of iterations is known in advance.

Ans. False

Q.3. The do-while loop is guaranteed to execute at least once.

Ans. True

Q.4. The break statement can only be used inside a loop or switch statement.

Ans. True

Q.5. The continue statement terminates the loop completely. 

Ans. False

Hands-On Questions

Q.1. Write a C++ program to print all the numbers from 1 to 10 using a for loop.

C++ program to print all the numbers from 1 to 10 using a for loop:

#include <iostream>

using namespace std;


int main() {

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

        cout << i << " ";

    }

    return 0;

}

Q.2. Write a C++ program to calculate the factorial of a number using a while loop.

C++ program to calculate the factorial of a number using a while loop:

#include <iostream>

using namespace std;


int main() {

    int number;

    cout << "Enter a positive integer: ";

    cin >> number;


    int factorial = 1;

    int i = 1;

    while (i <= number) {

        factorial *= i;

        i++;

    }


    cout << "Factorial of " << number << " = " << factorial << endl;

    return 0;

}

Q.3. Write a C++ program to find the sum of all odd numbers between 1 and 50 using a do-while loop.

C++ program to find the sum of all odd numbers between 1 and 50 using a do-while loop:

#include <iostream>

using namespace std;


int main() {

    int sum = 0;

    int number = 1;


    do {

        if (number % 2 != 0) {

            sum += number;

        }

        number++;

    } while (number <= 50);


    cout << "Sum of odd numbers between 1 and 50: " << sum << endl;

    return 0;

}

Q.4. Write a C++ program to check if a given number is prime or not using a for loop.

C++ program to check if a given number is prime or not using a for loop:

#include <iostream>

using namespace std;


int main() {

    int number;

    cout << "Enter a positive integer: ";

    cin >> number;


    bool isPrime = true;


    for (int i = 2; i <= number / 2; ++i) {

        if (number % i == 0) {

            isPrime = false;

            break;

        }

    }


    if (isPrime) {

        cout << number << " is a prime number." << endl;

    } else {

        cout << number << " is not a prime number." << endl;

    }

    return 0;

}

Q.5. Write a C++ program to display the following pattern using nested loops:

*
**
***
****
***** 

C++ program to display the following pattern using nested loops:

#include <iostream>

using namespace std;


int main() {

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

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

            cout << "*";

        }

        cout << endl;

    }

    return 0;

}

The document Assignment: Loops in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|44 docs|15 tests
70 videos|44 docs|15 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Summary

,

Important questions

,

Semester Notes

,

Assignment: Loops in C++ | Basics of C++ - Software Development

,

Exam

,

Viva Questions

,

ppt

,

Sample Paper

,

Free

,

study material

,

Assignment: Loops in C++ | Basics of C++ - Software Development

,

pdf

,

past year papers

,

mock tests for examination

,

Extra Questions

,

MCQs

,

Previous Year Questions with Solutions

,

Objective type Questions

,

shortcuts and tricks

,

practice quizzes

,

video lectures

,

Assignment: Loops in C++ | Basics of C++ - Software Development

;