MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010

Post by ashishrai.kv »

Code: Select all

public class TestClass{
  public static void main(String[] args){
     Fo : for(int i = 0; i< 10; i++){
        for (int j = 0; j< 10; j++){
             if ( i+ j > 10 )  break fo;
        }
        System.out.println( "hello");
     }
  }
}
why hello is printed only twice and breaks out of the loop while i debug this on eclipse and suddenly after i=2 it breaks out of the loop, and j can still go till 9th iteration,

please explain in detail its very confusing?
Last edited by admin on Tue Feb 20, 2018 11:16 pm, edited 2 times in total.
Reason: Please put code inside [code] [/code]

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: MOCK TEST-2, Q:-46

Post by admin »

When i is 0 or 1, j can go from 0 to 9 without breaking because i+j cannot be > 10 in these cases. This means hello will be printed once when 1 is 0 and second when i is 1. But when i becomes 2, as soon as j becomes 9, break fo; will be executed breaking the outer loop. Hence, the print statement will not be executed.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: MOCK TEST-2, Q:-46

Post by admin »

Btw, please put question id in the subject line so that this topic will be associated with that question.
If you like our products and services, please help us by posting your review here.

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010

Post by ashishrai.kv »

so that means it will print the statement only when J loop will finish all its iterations and if it doesnt then it will break out of the loop and the statement. is it?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010

Post by admin »

Yes, break fo; breaks the outer loop.
If you like our products and services, please help us by posting your review here.


Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests