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

Operator Precedence2620
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. 530.0 C. 1110.0 D. Some other output E. Compilation Error or Runtime Error
Topic:

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

z = x + 70 + y-- + x (*,/ has higher precedence and then +,- are evaluated from left to right )
z = 20 + 70 + y-- + x
z = 90 + 10 + x (value of y is taken as 10 since y is post decremented )
z = 100 + 20
z =120.00 (type castes to double since z is a double)

Posted by Shashanka Mogaliraju    2014-11-25 04:30:40

E,,, int can't be converted to double

Posted by Raju VarshnEy    2014-11-25 04:31:35

a.X=20...y=10.... wid z=x y*7 y-- x;...usng bodmas rule frst multiplctn wil b done... y*7=70....den x 70=90...y   means frst use den chmg hence frstly y=10 will b added to 90 n den agn x is aded n rslt is120 as z is doubl henc rslt is 120.0

Posted by Asma Mujtaba Khan    2014-11-25 04:33:54

ans is A,,,
type casting is deifintely required to print integer value,,but we have double z which accepts fractional values,so
double z =(int)x+Y*7+y--+x we can have 120,but since we dont have typecasting here,it prints 120.0 which is implicit typwe
type conversion before

Posted by Sai Veerendra    2014-11-25 06:09:33

ans a.                                                                         in this program x,y variables initialized with 20,10.                                                                            double variable z is assigned with one expression.                                                                    in this expression -- sign has highest precedence y-- post decrement so 1st value  of the y get used in this program aft ly decrement happen.so y--=10  .z=20+10*7+10+20                                               * gets second priority so z=20+70+10+20.              + gets lowest precedence priority z=120 thn implicitily int convert into double so z=120.0

Posted by Maheshwari Natarajan    2014-11-25 18:44:06

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 N Rajasekhar Reddy. 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-11-26 03:10:12