Finding some clarification for a question on Loop Constructs

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

Moderator: admin

Post Reply
Karuna
Posts: 2
Joined: Sun May 03, 2020 12:54 pm
Contact:

Finding some clarification for a question on Loop Constructs

Post by Karuna »

Code: Select all


Consider the following method which is called with an argument of 7:

public void method1(int i){
   int j = (i*30 - 2)/100;
   
   POINT1 : for(;j<10; j++){
       boolean flag  = false;
       while(!flag){
	if(Math.random()>0.5) break POINT1;
       }
   }
  while(j>0){
     System.out.println(j--);
     if(j == 4) break POINT1;
   }
}
What will it print? (Assume that Math.random() return a double between 0.0 and 1.0, not including 1.0)
On Line --> boolean flag = false;
// Getting compile error : Declaration not allowed here.
It will be great if someone can help on this.
Last edited by Karuna on Sun May 03, 2020 11:48 pm, edited 1 time in total.

Karuna
Posts: 2
Joined: Sun May 03, 2020 12:54 pm
Contact:

Re: Finding some clarification for a question on Loop Constructs

Post by Karuna »

This query has been resolved. Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests