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

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback