C++ Control Structures MCQ

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?

a) if
b) switch
c) loop
d) goto

Answer:

c) loop

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++?

a) test
b) switch
c) decide
d) if

Answer:

d) if

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++?

a) for
b) while
c) do
d) check

Answer:

d) check

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?

a) 0
b) 1
c) Until the condition is true
d) Infinitely

Answer:

b) 1

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";
a) No
b) Yes
c) Error
d) None of the above

Answer:

b) Yes

Explanation:

The condition x == 5 is true, so "Yes" will be printed.

6. Which control structure allows you to choose between multiple alternatives?

a) if-then
b) for
c) switch-case
d) while

Answer:

c) switch-case

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?

a) To start the loop
b) To exit the loop immediately
c) To skip one iteration
d) None of the above

Answer:

b) To exit the loop immediately

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?

a) if
b) do-while
c) while
d) for

Answer:

d) for

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?

a) else
b) elseif
c) then
d) finally

Answer:

a) else

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
a) for( ; ; ) { }
b) while(1) { }
c) do { } while(1);
d) All of the above

Answer:

d) All of the above

Explanation:

All the given loop structures have conditions that will always evaluate to true, causing them to run indefinitely.



Comments