File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP Brainteaser (3)

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What gets printed out when the following code is compiled and run?



Thanks for participating Oh, and hey, try to compile mentally and not using and IDE or javac. The exercise will be much more funny that way
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

It prints Hello followed by world and removing the ; will print world.

Regards,
Jothi Shankar Kumar. S

[ October 31, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
[ October 31, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Tuhin Ghosh
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
World
 
Gareth Western
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say:

Hello
World

because of the ";" you tried to hide in the line containing the "if"
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints out

Hallo
ween!



because it's so horrible!

Bu.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentin,

I've become a great fan of your questions. The most interesting part is that you finish the post by giving references to places where we get a decent explanation for that particular question you post. Great job man.

Regards,
Jothi Shankar Kumar. S
[ October 31, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Prahalad Deshpande
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code will print Hello because of the semi colon after the "if". Then it will print world.
 
Vivian Josh
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-------------------------------
public class Main {
public static void main(String[] args) {
boolean x = true;
if (x = isOn()); {
System.out.println("Hello");
}
System.out.println("World");
}

public static boolean isOn() {
return false;
}
}
-----------------------------------
Bcoz of the semicolon, it will print Hello and then on next line will print World .

Hello
World
 
Prasanth Pillai
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
World
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, you nailed it! Good job!

The catch here was of course the semicolon and to a lesser extent the assignment operation in the if condition.

Relevant pointers to the JLS:
14.6 The Empty Statement
15.26 Assignment Operators

Thanks to those who participated. Stay tuned...

Previous brainteasers:
SCJP Brainteaser 1
SCJP Brainteaser 2

because it's so horrible!
Burkhard, could you elaborate on that?
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic