Control structures guide the flow of a program’s execution. In C++, they’re essential for creating loops, making decisions, and crafting more complex logic. Ready to test your understanding of these structures? Dive into this beginner-friendly quiz!
1. Which control structure is used for executing a block of statements repeatedly based on a condition?
Answer:
Explanation:
Loops, like for, while, and do-while, execute blocks of code repeatedly based on a condition.
2. Which keyword is used to test a condition in C++?
Answer:
Explanation:
The if keyword is used to test a condition. If the condition is true, the block of code inside the if is executed.
3. Which of the following is NOT a loop in C++?
Answer:
Explanation:
check is not a loop in C++. The three primary loops are for, while, and do-while.
4. How many times is the body of a do-while loop guaranteed to execute?
Answer:
Explanation:
The do-while loop checks its condition at the end, ensuring the body of the loop executes at least once.
5. What will be the output of the following code snippet?
int x = 5;
if (x == 5)
cout << "Yes";
else
cout << "No";
Answer:
Explanation:
The condition x == 5 is true, so “Yes” will be printed.
6. Which control structure allows you to choose between multiple alternatives?
Answer:
Explanation:
The switch-case structure allows you to choose between multiple alternatives based on the value of an expression.
7. What is the purpose of the break statement in loops?
Answer:
Explanation:
The break statement is used to exit a loop immediately, bypassing any remaining iterations.
8. Which loop is best suited for iterating over arrays when you know the number of iterations in advance?
Answer:
Explanation:
The for loop is best suited for cases when you know the number of iterations in advance, such as iterating over arrays.
9. How can you execute a block of code irrespective of whether a condition in an if statement is true or false?
Answer:
Explanation:
The else block will execute when the condition in the associated if statement is false. If you want a block of code to run irrespective of the condition, place it outside the if-else structure.
10. Which of the following statements will run indefinitely?
a) for( ; ; ) { }
b) while(1) { }
c) do { } while(1);
d) All of the above
Answer:
Explanation:
All the given loop structures have conditions that will always evaluate to true, causing them to run indefinitely.