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.
The solution is:
ReplyDeleteint x = ~0;
x++;
Console.WriteLine("X is : " + x);
Alex
System.out.println("X is : " + 0)
ReplyDeletelet's invert the bits of zero.. shall we? :)
ReplyDeleteby 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'
}
}
From what I've learnt about how the '~' operator works, if we use something like
ReplyDelete~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
Hi deer,
ReplyDeleteYou just add "--" in the print method to get zero
It is easy :)
regards,
your friend:
Turki.27
I know this isnt what you mean...but one way to solve it is to not add any characters but instead rearrange the code:
ReplyDelete...
int x=0;
System.out.println("X is : " + x);
x++;
..
change
ReplyDeleteint x = 0;
to
int x = ~0;
int x = ~0;
ReplyDeletex++;
System.out.println("X is : " + x); //This must print 'X is 0'
System.out.println("X is :" + --x);
ReplyDeleteMost above solutions print 'X is : 0', how to make it print 'X is 0'?
ReplyDeleteso havent figured it out...
Cool puzzles keep posting !
ReplyDeleteChange line x++; to x = x++;
ReplyDelete