Computer Applications

Evaluate the following expressions, when int a = 8, b = 14.

(a) b/a++

(b) --a + b-- + a

Java Operators

6 Likes

Answer

(a) b/a++

b / a++
⇒ 14 / 8 [Postfix operator first uses the value then increments it]
⇒ 1 [Integer division is performed as both a and b are int variables]

Hence, the expression evaluates to 1

(b) --a + b-- + a

--a + b-- + a
⇒ 7 + b-- + a [a = 7, Prefix operator first decrements the value then uses it]
⇒ 7 + 14 + 7 [b = 13, Postfix operator first uses the value then decrements it]
⇒ 28

Hence, the expression evaluates to 28

Answered By

3 Likes


Related Questions