Sunday, April 19, 2009

Puzzle 17 – YAAP (Yet Another Arithmetic Puzzle)

Language – Java/C++ | Type – Concept | Last date 22-Apr-2009 9:00 p.m. IST | Points 2

Coding is easy; it’s maintaining other people’s code that really gets on my nerve. Just the other day I came across something that made me laugh in anguish! As usual looking for your help!

package com.twister;

public class TooMuchAdd {
public static void main(String[] args) {
int i = 5, j;
j
= i++ + i++;
System.out.println(
"Value of i : " + i);
System.out.println(
"Value of j : " + j);
}
}

Got an answer? Why don’t you leave it here.

6 comments:

  1. It is easy :)
    The outputs are:

    Value of i : 7
    Value of j : 11

    Thanks my friend.
    your friend: "Turki.27" :)

    ReplyDelete
  2. Was'nt that simple,
    i=7 and j=11

    ReplyDelete
  3. I think....

    Line 6 would evaluate as j = 5 + 5;
    Therefore, in the end i = 7, j = 10.

    If you wanted the final value of j to be 13 you would have to rewrite line 6 as:

    j = ++i + ++i;

    The latter forces the increment operation to happen before the assignment.

    ReplyDelete
  4. i=7,
    1.i=5
    2.i++,so i=6
    3.i++,so i=7

    j=11
    j=i++ + i++;
    means
    j=5+6;
    j=(initial i)+((i+1))
    then i becomes (i+1)+1

    ReplyDelete
  5. Value of i : 7
    Value of j : 11

    ReplyDelete

Solution for this question?