KnowledgeBoat Logo

Computer Applications

int a=7, p=0, q=0;
p= ++a + --a;
q-=p;

The output of q will be:

  1. 13
  2. 14
  3. 15
  4. -15

Java Operators

20 Likes

Answer

-15

Reason — Evaluating the given expressions:

Expression      Explanation
p = ++a + --aThis is the first expression. Initial value of a is 7 and p is 0.
p = 8 + --aAt this step, ++a first increments a by 1 so it becomes 8, then this incremented value is used in the expression.
p = 8 + 7 = 15At this step, current value of a (which is 8) is decremented by 1 and this decremented value of 7 is used in the expression.
    q-=p
⇒ q = q - p
⇒ q = 0 - 15
⇒ q = -15
Shorthand operator -= subtracts p from q and assigns the result back to q.

Answered By

6 Likes


Related Questions