• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Label query

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
Following is a piece of code from javabeat mock exam 14.

class TechnoSample {
public static void main (String args[]) {
int i = 0;
int j = 0;
label1: while (i++<5) {
label2:
for(; {
label3:
do {
System.out.print(i + j);
switch (i+j++) {
case 0: continue label3;
case 1: continue label2;
case 2: continue label1;
case 3: break;
case 4: break label3;
case 5: break label2;
case 6: break label1;
default: break label1; } }
while (++j<5); } } }}

What is the result of attempting to compile and run the above program?

(1) Prints: 12457
(2) Prints: 02357
(3) Prints: 02356
(4) Prints: 1357
(5) Prints: 1356
(6) Runtime Exception
(7) Compiler Error
(8) None of the Above

Answer is 1. I often get wrong answers with such iterattion questions.Please anyone tell me how to apply the logic behind such type of questions?

[ June 19, 2007: Message edited by: ketki kalkar ]
[ June 19, 2007: Message edited by: ketki kalkar ]
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ketki kalkar,

Please wrap your code examples in Code tags by highlighting the section of your post that is source code and hit the code button below the text input box on the form. Some of your code in this post is being interpreted by the forum software as a smiley face.

Thanks.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code doesn't compile because of the smile.

If I change that then 12457 wil be printed.

I write the values of i and j on paper en writes down the different values. If I follow the code.

Also you have to take into account that with



The value is first incremented and then evaluated and with



The value is first evaluated and then incremented.

It toke me some time before I understood this and could apply this right.
 
reply
    Bookmark Topic Watch Topic
  • New Topic