Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.150 :

Posted: Thu Apr 09, 2015 5:44 am
by dv_scjp
Hi - I'm just starting out on the chapter-wise tests, so this is kind of a newb question:
What will the following code print?

void crazyLoop()
{
int c = 0;
JACK: while (c < 8)
{
JILL: System.out.println(c);
if (c > 3) break JACK; else c++;
}
}
I selected the answer "It will not compile" as the code displayed would NOT compile on its own. The correct answer was "It will print numbers 0 to 4" - I guess that is true if you are assuming this is in a proper class with a main method calling this method properly.

Is this a typo or are these situations common on the test (where you have to just guess at the imaginary context of the code displayed)?

Re: About Question com.enthuware.ets.scjp.v6.2.150 :

Posted: Thu Apr 09, 2015 6:24 am
by admin
This is a common scenario. You've have to assume a valid context.

Re: About Question com.enthuware.ets.scjp.v6.2.150 :

Posted: Wed Apr 14, 2021 10:23 pm
by sphyror
I can't understand how this line of code compiles:
JILL: System.out.println( c );

And another this can not:
JILL: int i = 0;

I thought labels are only valid for loops and I also can't understand what is the utility of the JILL lavel. I hope you can help me. Thanks.

Re: About Question com.enthuware.ets.scjp.v6.2.150 :

Posted: Wed Apr 14, 2021 11:46 pm
by admin
That's is a rule of the Java language. A label cannot be applied on a declaration.
https://docs.oracle.com/javase/specs/jl ... l#jls-14.7

It is true that labels are mostly useful for loops. The statement JILL: System.out.println( c ); is not particularly useful.