File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Brainteaser (3) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Brainteaser (3)" Watch "SCJP Brainteaser (3)" New topic
Author

SCJP Brainteaser (3)

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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 ]

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Tuhin Ghosh
Ranch Hand

Joined: Oct 11, 2006
Posts: 33
Hello
World
Gareth Western
Ranch Hand

Joined: Apr 07, 2004
Posts: 45
I'd say:

Hello
World

because of the ";" you tried to hide in the line containing the "if"
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
It prints out

Hallo
ween!



because it's so horrible!

Bu.


all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Oct 31, 2006
Posts: 14
The code will print Hello because of the semi colon after the "if". Then it will print world.
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
-------------------------------
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

Joined: Oct 16, 2006
Posts: 52
Hello
World
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SCJP Brainteaser (3)