0% found this document useful (0 votes)
3 views

Lecture 6

The document contains a series of questions and answers related to loops in C++, focusing on their purpose, types, and behavior. Key concepts include the execution of loops based on conditions, handling infinite loops, and calculating sums and factorials. The document serves as a quiz or study guide for understanding loop mechanics in programming.

Uploaded by

myvujourneywell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 6

The document contains a series of questions and answers related to loops in C++, focusing on their purpose, types, and behavior. Key concepts include the execution of loops based on conditions, handling infinite loops, and calculating sums and factorials. The document serves as a quiz or study guide for understanding loop mechanics in programming.

Uploaded by

myvujourneywell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is the primary purpose of a loop in programming?

a) To execute code once


b) To execute a set of instructions repeatedly based on a condition
c) To terminate a program
d) To store data in a variable

Answer: b) To execute a set of instructions repeatedly based on a condition

2. Which type of loop in C++ repeats a block of code as long as the condition evaluates to
true?
a) for loop
b) while loop
c) do-while loop
d) switch loop

Answer: b) while loop

3. In the following code, how many times will the loop execute if number starts at 1 and
upperLimit is 10?

cpp
Copy code
int sum = 0, number = 1;
while (number <= upperLimit) {
sum += number;
number++;
}

a) 10
b) 9
c) Infinite
d) 11

Answer: a) 10

4. What is the output of the following code if the sum is calculated from 1 to 5?

cpp
Copy code
int sum = 0, number = 1;
while (number <= 5) {
sum += number;
number++;
}
cout << "Sum: " << sum;

a) Sum: 10
b) Sum: 15
c) Sum: 20
d) Sum: 5

Answer: b) Sum: 15

5. What could happen if you forget to increment the variable in a while loop?
a) The loop will execute an infinite number of times
b) The loop will execute only once
c) The program will throw a syntax error
d) The loop will terminate immediately

Answer: a) The loop will execute an infinite number of times

6. Which of the following is an example of an infinite loop in C++?


a) while (number > 0)
b) while (true)
c) while (number <= 5)
d) while (number != 0)

Answer: b) while (true)

7. In the code below, what is the output if upperLimit = 10?

cpp
Copy code
int sum = 0, number = 1;
while (number <= upperLimit) {
if (number % 2 == 0) {
sum += number;
}
number++;
}
cout << "Sum of even numbers: " << sum;

a) Sum of even numbers: 25


b) Sum of even numbers: 30
c) Sum of even numbers: 20
d) Sum of even numbers: 15

Answer: b) Sum of even numbers: 30

8. Which of the following can prevent an infinite loop?


a) Ensuring the condition becomes false eventually
b) Using goto to jump out of the loop
c) Changing the loop to a do-while loop
d) Adding a break statement inside the loop

Answer: a) Ensuring the condition becomes false eventually

9. What will be the result if the following code is executed with the input number = 5?

cpp
Copy code
int factorial = 1;
while (number > 1) {
factorial = factorial * number;
number--;
}
cout << "Factorial: " << factorial;

a) Factorial: 120
b) Factorial: 60
c) Factorial: 5
d) Factorial: 24

Answer: a) Factorial: 120

10. In a while loop, what will happen if the condition is false initially?
a) The loop will execute once
b) The loop will execute multiple times
c) The loop will not execute at all
d) The program will terminate

Answer: c) The loop will not execute at all

11. What is the output of the following code?


cpp
Copy code
int sum = 0, number = 1;
while (number <= 1000) {
sum = sum + number;
number = number + 1;
}
cout << "The sum of the first 1000 integers is: " << sum;

a) 500500
b) 1000
c) 1000000
d) 999500

Answer: a) 500500

12. Which of the following describes the flow of a while loop?


a) Start → Execute code → Check condition → Exit
b) Start → Check condition → Execute code → Go back to check condition
c) Start → Check condition → Execute code → Execute code again
d) Start → Execute code → Exit

Answer: b) Start → Check condition → Execute code → Go back to check condition

13. What will happen if the loop condition in the following code is always true?

cpp
Copy code
while (true) {
// Infinite loop
}

a) The program will exit automatically


b) The loop will run indefinitely
c) The program will crash immediately
d) The program will run for a set number of iterations

Answer: b) The loop will run indefinitely

14. What type of error can occur if a loop keeps executing without the condition eventually
becoming false?
a) Runtime error
b) Syntax error
c) Logical error
d) Stack overflow

Answer: a) Runtime error

15. How can you calculate the factorial of a number using a while loop?
a) By multiplying the number by each integer greater than it
b) By multiplying the number by each integer less than it
c) By adding all the numbers together
d) By recursively calling the same function until the condition is met

Answer: b) By multiplying the number by each integer less than it

16. Which of the following will result in a valid factorial calculation?

cpp
Copy code
int factorial = 1, number = 5;
while (number > 1) {
factorial *= number;
number--;
}

a) The loop runs correctly, calculating 120


b) The loop runs infinitely
c) The factorial will be incorrect due to a missing decrement
d) The loop will output 1

Answer: a) The loop runs correctly, calculating 120

17. In the following code, what will be printed if upperLimit = 15?

cpp
Copy code
int sum = 0, number = 1;
while (number <= upperLimit) {
if (number % 2 != 0) {
sum += number;
}
number++;
}
cout << "Sum of odd numbers: " << sum;
a) Sum of odd numbers: 64
b) Sum of odd numbers: 75
c) Sum of odd numbers: 65
d) Sum of odd numbers: 80

Answer: a) Sum of odd numbers: 64

18. What is the maximum value of a sum that can be calculated without overflow using an
int in most C++ systems?
a) 2^31 - 1 (about 2 billion)
b) 2^16 - 1 (about 65 thousand)
c) 2^64 - 1 (about 18 quintillion)
d) 2^32 - 1 (about 4 billion)

Answer: a) 2^31 - 1 (about 2 billion)

19. What is the value of sum after this code is executed, if number = 2 and upperLimit =
10?

cpp
Copy code
int sum = 0;
while (number <= upperLimit) {
if (number % 2 == 0) {
sum += number;
}
number++;
}
cout << "Sum of even numbers: " << sum;

a) 30
b) 20
c) 25
d) 18

Answer: b) 20

20. If you want to calculate the sum of all numbers up to a given upperLimit, which loop
condition should be used?

cpp
Copy code
while (number <= upperLimit) {
sum += number;
number++;
}

a) number > upperLimit


b) number == upperLimit
c) number <= upperLimit
d) number != upperLimit

Answer: c) number <= upperLimit

You might also like