Lecture 6
Lecture 6
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
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
cpp
Copy code
int sum = 0, number = 1;
while (number <= upperLimit) {
if (number % 2 == 0) {
sum += number;
}
number++;
}
cout << "Sum of even numbers: " << sum;
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
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
a) 500500
b) 1000
c) 1000000
d) 999500
Answer: a) 500500
13. What will happen if the loop condition in the following code is always true?
cpp
Copy code
while (true) {
// Infinite loop
}
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
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
cpp
Copy code
int factorial = 1, number = 5;
while (number > 1) {
factorial *= number;
number--;
}
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
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)
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++;
}