Examveda

What is the result of the expression: 3 * (4 + 2) in C++?

A. 18

B. 20

C. 12

D. Compiler error

Answer: Option A

Solution (By Examveda Team)

Let's break down this C++ expression step by step, just like you would in regular math!
The expression is: 3 * (4 + 2)

In C++ (and most programming languages), we follow a set of rules called operator precedence. This means some operations are performed before others. It's similar to the PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) or BODMAS rule you might know from mathematics.

The most important rule here is: Parentheses ( ) always come first! Any calculation inside parentheses is done before anything outside them.

1. First, evaluate the expression inside the parentheses:
We have (4 + 2).
4 + 2 = 6

2. Now, substitute this result back into the original expression:
The expression now becomes: 3 * 6

3. Finally, perform the multiplication:
3 * 6 = 18

So, the result of the expression 3 * (4 + 2) in C++ is 18.

Therefore, Option A: 18 is the correct answer.

Why not other options?
* Options B and C are incorrect because they do not follow the correct order of operations. If you did 3 * 4 first (12), then added 2, you'd get 14, which is not an option and still incorrect. The parentheses *must* be solved first.
* Option D: Compiler error is incorrect because this is a perfectly valid and well-formed C++ expression. The compiler understands the parentheses and the operators (* and +) and will correctly calculate the result.

Join The Discussion

Comments (1)

  1. Saad Mansoor
    Saad Mansoor:
    3 months ago

    The given answer (Option B: 20) is incorrect.

    In C++, the expression 3 * (4 + 2) follows standard operator precedence rules. The parentheses are evaluated first:

    4 + 2 = 6

    Then multiplication is performed:

    3 * 6 = 18

    So, the correct answer is Option A (18), not Option B.

Related Questions on Operators and Expressions in C plus plus