Sunday, May 17, 2009

Puzzle 25 - Deja-Vu (Code completion -3)

Language – Java | Type – Problem (Code) | Last date 20-May-2009 9:00 p.m. IST | Points 2

I spend some time making a few changes with the blog look and feel – customizing thing around – I especially like the social sharing icons I put down there – gives the blog a lot more color! I wonder if there is a hint hidden here :). Folks time to get on with the next question.

If you haven't had a look at the previous puzzle – do have a look at it before you begin this one!

For the rest – getting a feeling of Deja-Vu? Think you have seen this puzzle before? – you have it's the same question as the previous one. Ditto – Same.

package com.twisters;
public class UnIncrement{
public static void main(String[] args) {
int x = 0;
x
++;
System.out.println(
"X is : " + x);//This must print 'X is 0'
}
}


Get the System.out.prinltn() to print 'X is 0' at line 6.
You may add (but not delete) any additional amount of code.

(Same lines – thats because I copy pasted them!!!)

Here the change – the puzzle a lot simpler now. My clause "You can just add one character to the above code to get it to work - any one character but that all"

Site of the day - http://interviewpattern.com/post/Puzzles-and-Riddles-on-the-interview-(Part-e28093-I).aspx. If you like analytical puzzles you might enjoy the post!

Got an answer? Do leave it here.

12 comments:

  1. The solution is:

    int x = ~0;
    x++;
    Console.WriteLine("X is : " + x);

    Alex

    ReplyDelete
  2. System.out.println("X is : " + 0)

    ReplyDelete
  3. let's invert the bits of zero.. shall we? :)

    by stating int x = ~0 we invert the bits of 0 during the attribution, thus making it so that when adding 1 to it, we end up with zero.


    package com.twisters;

    public class UnIncrement {
    public static void main(String[] args) {
    int x = ~0;
    x++;
    System.out.println("X is : " + x);//This must print 'X is 0'
    }
    }

    ReplyDelete
  4. From what I've learnt about how the '~' operator works, if we use something like
    ~x
    he converts to
    -(x+1)

    Therefore, the code below should print "x is : 0".

    public class UnIncrement {
    public static void main(String[] args) {
    int x = ~0;
    x++;
    System.out.println("X is : " + x);//This must print 'X is 0'
    }
    }

    If there is something wrong with my answer, please do tell me. I can't see any other way of solving this with just one more character.

    I would like to congratulate you on the great work you've done with twisters, they are a really neat way of making people think on the "not-so-obvious" singularities of Java.

    Cheers,
    Bernardo

    ReplyDelete
  5. Hi deer,

    You just add "--" in the print method to get zero
    It is easy :)
    regards,

    your friend:
    Turki.27

    ReplyDelete
  6. I know this isnt what you mean...but one way to solve it is to not add any characters but instead rearrange the code:

    ...
    int x=0;
    System.out.println("X is : " + x);
    x++;
    ..

    ReplyDelete
  7. change

    int x = 0;

    to

    int x = ~0;

    ReplyDelete
  8. int x = ~0;
    x++;
    System.out.println("X is : " + x); //This must print 'X is 0'

    ReplyDelete
  9. System.out.println("X is :" + --x);

    ReplyDelete
  10. Most above solutions print 'X is : 0', how to make it print 'X is 0'?
    so havent figured it out...

    ReplyDelete
  11. Cool puzzles keep posting !

    ReplyDelete
  12. Change line x++; to x = x++;

    ReplyDelete

Solution for this question?