This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Mock Exam Errata and the fly likes Khalid's Mock Test. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Khalid Watch "Khalid New topic

Khalid's Mock Test.

Ajay Kumar
Ranch Hand

Joined: Apr 28, 2000
Posts: 87
Can someone please explain this to me
This is a question from the Khalid's mock test.
What is the output if the following code is run?
public class Q28fd {
public static void main(String args[]) {
int counter = 0;
for (int i=10; i<0; i--) {<br /> l2:<br /> int j = 0;<br /> while (j < 10) {<br /> if (j > i) break l2;<br /> if (i == j) {<br /> counter++;<br /> continue l1;<br /> }<br /> }<br /> counter--;<br /> }<br /> System.out.println(counter);<br /> }<br /> }<br /> </pre><br /> Among the answers one of the option is It will fail to compile<br /> But when selected the answer says it is false.<br /> I tested the exact code, and the compile error listing is below.<br /> <font color=red><br /> A declaration cannot be labelled: l2<br /> int j = 0;<br /> ^ <br /> No label definition found for l2.<br /> if(j>i) break l2;

I just wanted to know if this needs to be listed to the mock exam errata.


Regds<BR>Ajay Kumar
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Yes. This code will fail. I take this to Mock Exam errata. Thanks Ajay.
maha anna
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
I really do not see how this question qualifies for the mock exam errata.One of the choices for this question is that it will not compile, which is the correct answer.
Ranch Hand

Joined: May 13, 2000
Posts: 56
It may be a Mock Exam errata if you choose the correct answer, the answer section says this is the incorrect answer.
However, the second printing is correct with respect this.
Steve Butcher

vishad patel

Joined: Dec 05, 2000
Posts: 17
Hello friend ;
answer is right because you can't declare label before the loop as declare in the given coding.
See following code which will compile without error and run.public class Q28fd {
public static void main(String args[]) {
int counter = 0;
for (int i=10; i<0; i--) {
int j = 0;
while (j < 10) {
if (j > i) break l2;
if (i == j) {
continue l1;
The output will be 0.

Joined: Dec 10, 2000
Posts: 3
Label l2 should be stated just before the while() loop or for that matter any loop declaration.And this compiles fine giving the output of 0.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
sgwbutcher and Mukti,
Your names do not comply with the naming policies here at JavaRanch. Please re-register with a more appropriate name before your next postings To see more info on this, check out this page,
bhakti soman

Joined: Nov 12, 2000
Posts: 22
could you please tell me url of khalids mock exam
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
check out this link. This has loads of mocks, and khalid's is one of them.
I agree. Here's the link:
subject: Khalid's Mock Test.
It's not a secret anymore!