• 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

JQ+ Question ID :952739443290

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a question
Given the following code fragment, which of the following lines would be a part of the output?
outer:
for ( int i = 0 ; i<3 ; i++ )
{
for ( int j = 0 ; j<2 ; j++ )
{
if ( i == j )
{
continue outer;
}
System.out.println( "i=" + i + " , j=" + j );
}
}
Given the following code fragment, which of the following lines would be a part of the output?
a) i=1, j=0
b) i=0, j=1
c) i=1, j=2
d) i=2, j=1
e) i=2, j=2
This is easy but It says there are only 2 good answers but I thought there were three, the answers are A and C, but I was sure that B could also be an answer. Can anyone explain why B is not an answer?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i takes value 0, 1 & 2; j takes value 0 & 1.
Whenever i == j, the inside loop will stop and the program continue with the outer loop starting with the next value of i.
Starting with i == 0:
(i==0,j==0) ---> not printed, next i
(i==1,j==0) ---> get printed
(i==1,j==1) ---> not printed, next i
(i==2,j==0) ---> get printed
(i==2,j==1) ---> get printed
Loop ends.
So output are:
i=1 , j=0
i=2 , j=0
i=2 , j=1
So correct options should be A and D.

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!Greg,
I compiled your program and got the output
as
i=1,j=0
i=2,j=0
i=2,j=1
the answer b is wrong.According to the program
firt i=0 and j=0
the if conditons becomes true and again it goes to Outer
then i=1 and j=0 the if condition returns false and i=1,j=0 is printed.
now j value is incrimented and becomes j=1 the if conditon returns true so it continues outer then
now i=2 and j=0 the if condition returns false and i=2,j=0 is printed
now j value is incremented and becomes j=1 theif conditon returns false and i=2,j=1 is printed.
now control goes to for loop jis incremented to 2
now the if condition returns true and it continues outer
and the progam terminates.
Hope this helps!.
 
Greg Georges
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, did not follow the continue correctly and that is why I was getting B in the answer, thanks again, next time I will be more careful with the continue statements
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Greg,
Option b is i=0, j=1
In the first iteration, value of i & j are 0 & 0. The code says,

The "outer" label is at the beginning of outer loop. So the further iterations of the inner loop are skipped, i is assigned the value 1, and the iteration proceeds.
So, the values given in option b are incorrect.
If continue statement was present without any label, or if the label was placed at the beginning of the inner loop(both are equivalent), then option b would have been one of the correct options.
Although they may seem very easy on the first glance, codes concerned with flow control are very tricky, and it is a good idea to go through each iteration one by one, to be absolutely sure of the answer.
I hope this clears your doubt. Any comments are welcome.
Prasanna.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic