Topics Index

Operator Precedence2621

If you need explanation Read this topic

If you need Answer Take test on this topic

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);

A. 120.0
B. 239.0
C. 240.0
D. Compilation Error or Runtime Error
Topic: Java Operator Precedence And Associativity

If you need explanation Read this topic

If you need Answer Take test on this 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 the order of evaluation is as follows..                 (20+10)*7+(--y + x)
30*7 +(--y+x)
210 + (19+10)
=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 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 to raise the recharge.

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

© meritcampus 2019

All Rights Reserved.

Open In App