MCQ
Q.
What will be the output of the following C++ code snippet?
#include
int main() {
int a = 5;
int b = 3;
if (a > b && b < 10) {
std::cout << "Condition Met" << std::endl;
} else {
std::cout << "Condition Not Met" << std::endl;
}
return 0;
}
Correct Answer: A
The correct answer is Condition Met.
🔑 Key Points
- The variables a and b are initialized with values 5 and 3 respectively.
- The if statement evaluates the condition (a > b && b < 10).
- The first part of the condition, a > b (which is 5 > 3), evaluates to true.
- The second part of the condition, b < 10 (which is 3 < 10), also evaluates to true.
- Since both parts are true, and they are joined by the logical AND (&&) operator, the entire condition (true && true) evaluates to true. Thus, the code inside the if block is executed.
📄 Additional Information
- The && operator (logical AND) returns true only if both its operands are true. If even one operand is false, it returns false.
- The if-else statement is a fundamental control flow construct in C++ used for conditional execution of code blocks.
- Comparison operators like > (greater than) are used to compare values and return a boolean result (true or false).