What will be the output of the following code: int x = 10; if (x < 5) cout << "Less"; else if (x == 5) cout << "Equal"; else cout << "More"; in C++?
A. More
B. Less
C. Equal
D. Compilation error
Answer: Option A
Solution (By Examveda Team)
Explanation:Let's break down this C++ code step by step:
1. Variable Initialization:
int x = 10;This line creates an integer variable named 'x' and assigns it the value 10.
2. `if` Statement:
if (x < 5)This checks if the value of 'x' (which is 10) is less than 5. Since 10 is not less than 5, this condition is false.
3. `else if` Statement:
else if (x == 5)Because the first `if` condition was false, the program moves to the `else if` condition. This checks if 'x' is equal to 5. Since 10 is not equal to 5, this condition is also false.
4. `else` Statement:
else cout << "More";Since both the `if` and `else if` conditions were false, the program executes the code within the `else` block. This line prints "More" to the console.
Therefore, the output of the code will be "More".
Correct Answer: A) More
Join The Discussion
Comments (1)
What is the syntax for the 'if' statement in C++?
A. if (condition) then statement;
B. if (condition) then { statement; }
C. if { condition } then { statement; }
D. if (condition) { statement; }
What is the purpose of the 'else' statement in C++?
A. Executes after the 'if' block
B. Executes before the 'if' block
C. Executes when the 'if' condition is false
D. Executes regardless of the 'if' block
What is the correct syntax for the 'switch' statement in C++?
A. switch (expression) { case constant1: statement1; case constant2: statement2; }
B. switch (expression) { case constant1: statement1; break; case constant2: statement2; break; default: defaultStatement; }
C. switch (expression) { case constant1: statement1; default: defaultStatement; }
D. None of the above
A. Compilation error
B. HelloGoodbye
C. Hello
D. Goodbye

Ans: A) More