Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Wed Apr 17, 2013 11:52 am
by smearaDubha
Hi

I have a question in relation to this line :

int j = (i*30 - 2)/100;

I don't understand how this is allowed by the compiler. i is 7 so I thought it would be :

(7*30 - 2)/100 = 2.08

However the compiler makes it equal to 2.

How can this be?

Great questions and forum by the way :D

Richie

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Wed Apr 17, 2013 6:25 pm
by admin
When both the operands of the division operator are int, it performs an integer division i.e. it just truncates the decimal part. In this case, i*30 - 2 is an int and so is 100. So (i*30 - 2)/100 will result in 2.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Thu Apr 18, 2013 2:42 am
by smearaDubha
Thank you Paul, good to know

Richie

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Thu Jan 18, 2018 1:13 pm
by geozak
Shouldn't the explanation be:
Remember that a labeled break or continue statement must always exist inside the block where the label is declared. Here, if(j == 4) break POINT1; is a labelled break that is occurring in the second loop while the label POINT1 is declared for the first loop.

Instead of:
Remember that a labeled break or continue statement must always exist inside the loop where the label is declared. Here, if(j == 4) break POINT1; is a labelled break that is occurring in the second loop while the label POINT1 is declared for the first loop.

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Thu Jan 18, 2018 2:01 pm
by admin
You can't have a labeled break or continue unless there is a loop so they must be within a loop and not just any kind of block.

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Fri Apr 09, 2021 1:30 am
by maria_maria

Code: Select all

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

public void method1(int i, double d){
   int j = (i*30 - 2)/100;
   
   POINT1 : for(;j<10; j++){
       var flag  = false;
       while(!flag){
	if(d > 0.5) break POINT1;
       }
   }
  while(j>0){
     System.out.println(j--);
     if(j == 4) break POINT1;
   }
}
What will it print?
Isn't the premise wrongly worded method which is called with an argument of 7 when the method takes two params?
Isn't this the point where all goes haywire and doesn't compile regardless of the code inside the method?

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Fri Apr 09, 2021 1:39 am
by admin
Yes, but what you have quoted is not the code presented in the question. The code given in the question is :

Code: Select all

public void method1(int i){
   int j = (i*30 - 2)/100;
...

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Fri Apr 09, 2021 1:52 am
by maria_maria
I've attached a print-screen:

Re: About Question enthuware.ocajp.i.v7.2.1042 :

Posted: Fri Apr 09, 2021 6:12 am
by admin
Ok, I found the issue. Since the thread titled mentioned OCA, I checked the OCA 8 questions bank (where the code was correct) while you are using OCP 11 question bank, where it is incorrect. For some reason, this was changed in OCP 11 question bank.
Fixed now.
thank you for your feedback!