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

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

Moderator: admin

Post Reply
giorgiadiro
Posts: 7
Joined: Wed Feb 04, 2015 1:06 pm
Contact:

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

Post by giorgiadiro »

Hi Enthuware Admin and everybody.
I don't understand this answer "It will print Sum = 0".
Ok, variable sum is never changed but... isn't this a case of "unreachable code"?

int sum = 0;
for (int i = 0, j = 10; sum > 20; ++i, --j) // 1
{
sum = sum+ i + j;
}


At compile time sum = 0 and the condition to enter the for loop (sum>20) is not satisfied at the first iteration.
So the instruction sum = sum+ i + j; can never be executed.
Maybe compiler doesn't check the for loop statements or it doesn't check the variables value?
I'm confused, can someone help me?

Thank you so much.
G.

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

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

Post by admin »

Compiler doesn't execute code so it can't evaluate variables. I can only look at compile time constants to see if some code is unreachable or not. That is why there is no compilation error for unreachable code.
If you like our products and services, please help us by posting your review here.

giorgiadiro
Posts: 7
Joined: Wed Feb 04, 2015 1:06 pm
Contact:

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

Post by giorgiadiro »

Thank you.
It's clear now.

Post Reply

Who is online

Users browsing this forum: No registered users and 88 guests