Java Puzzlers - Can you figure this out?

The book Java Puzzlers is quite good. I don’t think anyone can get every puzzle right. This shows again how you can very easily make someone fail interviews if you ask too silly questions. I suppose that if people were asking those questions they would not expect the right answers, but study the candidate reactions.

Here is a sample:

public class DosEquis {
  public static void main(String[] args) {
    char x = ‘X’;
    int i = 0;
    System.out.print(true ? x : 0);
    System.out.print(false ? i : x);

This will output “X88”. Obviously this is not good code, which is precisely one of the book objectives: to show how bad some practices can be. But at the same time you learn a bit more about the Java language and its possibilities. In the latter chapters they have more interesting puzzles.

Tags: , ,

comments powered by Disqus
Tweet Submit to reddit
© 2006-16 Fabien Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License.