What will be the output of the following code:

public class Main {
        public static void main (String []args) {
                 int z = 1;
                 z += z += z += z += z;
                 System.out.println(z);
        }       
}
Explanation
The initial value is used for z from left to right. Then expressions are evaluated from right to left. In other words, every expression z+=f(z) is the same as z = z+f(z), where all left summands are equal to 1. Therefore the result will be 1+1+1+1+1 = 5.
Java
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback