Vidyalelo
Python · Q3

Python Operator Precedence: MCQs for Exam Prep

Programming · Python · question 3

Q3

What is the result of 5 - 2 + 1 ?

A.
2
B.
4
Answer
C.
-2
D.
3

Answer: Option B

Solution

Answer: Option B
Solution:
In Python, operators have specific precedence levels that determine the order in which operations are performed.

Subtraction (-) and addition (+) operators have the same precedence level and are evaluated from left to right (left associativity).

In the expression 5 - 2 + 1:
First, 5 - 2 is evaluated, resulting in 3.
Then, 3 + 1 is evaluated, resulting in 4.

Thus, the result of the expression 5 - 2 + 1 is 4.