Examveda

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)

  1. Gopal Ambore
    Gopal Ambore:
    5 months ago

    Ans: A) More

Related Questions on Control Flow Statements in C plus plus

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