wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Khalid Watch "Khalid New topic
Author

Khalid's Mock Test.

Ajay Kumar
Ranch Hand

Joined: Apr 28, 2000
Posts: 87
Hi,
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?
<pre>
public class Q28fd {
public static void main(String args[]) {
int counter = 0;
l1:
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 /> Q28fd.java:7: A declaration cannot be labelled: l2<br /> int j = 0;<br /> ^ <br /> Q28fd.java:9: 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.

Thnx
Ajay


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.
regds
maha anna
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Hie,
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.
Herbert.
sgwbutcher
Ranch Hand

Joined: May 13, 2000
Posts: 56
Herbert,
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
exceptionraised@aol.com


-------------------------<BR>Steve<BR>sgwbutcher@aol.com<BR>http://hometown.aol.com/sgwbutcher/
vishad patel
Greenhorn

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;
l1:
for (int i=10; i<0; i--) {
l2:
int j = 0;
while (j < 10) {
if (j > i) break l2;
if (i == j) {
counter++;
continue l1;
}
}
counter--;
}
System.out.println(counter);
}
}
The output will be 0.
Mukti
Greenhorn

Joined: Dec 10, 2000
Posts: 3
Hi,
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, www.javaranch.com/name.jsp
Thank,
Bill
bhakti soman
Greenhorn

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

Joined: Jun 30, 2000
Posts: 1070
bhakti,
check out this link. This has loads of mocks, and khalid's is one of them.
http://www.javaranch.com/maha/_Mock_Exams/_mock_exams.html
Bill
 
jQuery in Action, 2nd edition
 
subject: Khalid's Mock Test.
 
Similar Threads
loops & labels
For loop
Labeeled Problem
Error while Running Code
Problem with labeled break