• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Khalid's Mock Test.

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. This code will fail. I take this to Mock Exam errata. Thanks Ajay.
regds
maha anna
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic