Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid's Mock Test.

 
Ajay Kumar
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. This code will fail. I take this to Mock Exam errata. Thanks Ajay.
regds
maha anna
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vishad patel
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
could you please tell me url of khalids mock exam
thanks
bhakti
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic