Hi all,
When I took one of the tests in the OCA/OCP Java SE7 Programmer I and II Study Guide I came across the following question:
The question was about what happens next and the answer was that it does not compile for the following reason:
Without curly braces, the compiler makes the code 6 lines of code starting with the first if test a single block. That means the second else statement is floating without an if statement to call its own.
Straightforward enough. But then I took an Enthuware test where the following code was present:
Which to me, looked the same as the one in the other test. So I answered that it wouldn't compile. However this was incorrect. The correct answer was that it would compile and would print "False False" if passed false and "True False" if passed true.
I've tried both of these out by tying to compile them and both behave as each test says they will. The first example won't compile and the 2nd one will. It could be that I need to step away and come back later with fresh eyes but at the moment I can't see the difference between them. It looks like both of these have a 2nd floating else that does not correspond to an if. But the 2nd one clearly does correspond to an if. Can anyone shed any light?