info@meritcampus.com    +91-85006-22255
...
`

Operator Precedence2621
What will be the output of the following program?
`public class Precedence {    public static void main(String[] args) {        int x = 20;        int y = 10;        double z = (x + y) * 7 + (--y + x);        System.out.println(z);    }}`

 A. 120.0 B. 239.0 C. 240.0 D. Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

Answer is B.....first expressions inside () will be evaluated since they have higher precedence ....--operator has precedence over arithmetic operators....so the order of evaluation is as follows..                 (20+10)*7+(--y + x)
30*7 +(--y+x)
210 + (19+10)
210+29
=239.0(since the final result has to casted to double)

Posted by Shashanka Mogaliraju    2014-12-02 04:47:41

b... x=20 y=10 and x+y=30 .ALSO (X+Y)*7=210 ...DEN --Y=9 AND WEN ADDEE WID X GIVES 29 ...DIS 29 WEN ADDED WID 210 GIVS 239 SINCE THE OUTPUT IS IN DOUBL DATATYP SO ANSWR IS 239.0

Posted by Asma Mujtaba Khan    2014-12-02 04:50:20

ans b.predecrement operator has high priority ,next closed brackets,thn * operator,finally + operation performed .
initially x y assigned with 20,10.
double variable z assigned with follwing expression
z=(x+y)*7+(--y+x)

z=(20+10)*7+(19+10)

z=(30)*7+(29)

z=210+29

z=239

z is double so expression result implicitily changed to double so sop prints 239.0

Posted by Maheshwari Natarajan    2014-12-02 04:56:03

This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-03 01:29:45