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

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

Moderator: admin

Post Reply
timbijnen
Posts: 4
Joined: Fri Oct 03, 2014 5:21 am
Contact:

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

Post by timbijnen »

Code: Select all

public class TestClass{
   static int value = 0; //1
   public static void main(String args[]) //2
   {
      int 2ndArgument = Integer.parseInt(args[2]); //3
      if( true == 2 > 10 ) //4  
      {
         value = -10;
      }
      else{
         value =  2ndArgument;
      }
      for( ; value>0; value--) System.out.println("A"); //5
   }
}
i get that line 3 is causing a compiler error, but isn't it also that line 4 causes one, since the expression always evaluates to false it makes it so that the true part of the if statement is unreachable code. Doesn't change the fact that still line 3 causes a compiler error in the first place but i missed it in the answer description:

== has less precedence than > so true == 2 > 10 is same as true == (2 > 10)

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

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

Post by admin »

Compiler doesn't evaluate value>0 at compile time so it doesn't know that the block is unreachable. Compiler can consider only compile time constant values to evaluate expressions and determine whether a code is unreachable or not.
If you like our products and services, please help us by posting your review here.

timbijnen
Posts: 4
Joined: Fri Oct 03, 2014 5:21 am
Contact:

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

Post by timbijnen »

Thanks for your reply! Sounds clear

kabanvau
Posts: 14
Joined: Thu Nov 21, 2019 5:48 am
Contact:

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

Post by kabanvau »

value = -10; will flag an error first! Before line 5. Non-static field 'value' cannot be referenced from a static context.

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

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

Post by admin »

Please read the code carefully. value is, in fact, static. So, there will be no such error.
If you like our products and services, please help us by posting your review here.

sijucm
Posts: 8
Joined: Tue Jan 11, 2022 4:41 am

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

Post by sijucm »

How can something be checked if it compiles if the declaration itself does not compile. If //2 does not compile, that means args is not a valid references then how can //3 be checked. I hope these kind of things does not happen in real test because it is confusing. I won't fight the question setter and just mark what she expects me to.

cristibctr
Posts: 6
Joined: Sun Jun 18, 2023 11:03 am
Contact:

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

Post by cristibctr »

admin wrote:
Mon May 24, 2021 8:04 pm
Please read the code carefully. value is, in fact, static. So, there will be no such error.
I'm not sure if this question was updated since this answer was posted but value is, in fact, not static. At least in my test "value" is defined as "var".

That obviously doesn't compile but now you've got an assumption to make in order to find the next line that doesn't compile:
  • Value is static which means that line 5 also compiles so the correct answer list is incorrect
  • Value is not static which means that both value = -10; and value = secondArg; won't compile but these options are not present

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

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

Post by admin »

Could you please mention which question bank / exam code are you using? Because I just checked in the OCA 1z0808 question bank and value is static. The code listing given in the first post of this topic also has it as static.
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: About Question enthuware.ocajp.i.v7.2.1073 :

Post by admin »

I think I found the issue. A similar question in OCP 11 and 17 question banks with id (enthuware.ocpjp.v17.2.3236) is being incorrectly directed to this discussion, which is what is causing this confusion. This topic is for the OCA question 2.1073 and this question / answer is correct.

The link to the discussion on the OCP question is https://enthuware.com/forum/viewtopic.php?f=2&t=4913
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 44 guests