Page 1 of 1

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

Posted: Mon Nov 10, 2014 7:56 am
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)

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

Posted: Mon Nov 10, 2014 8:31 am
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.

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

Posted: Mon Nov 10, 2014 10:15 am
by timbijnen
Thanks for your reply! Sounds clear

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

Posted: Mon May 24, 2021 4:57 am
by kabanvau
value = -10; will flag an error first! Before line 5. Non-static field 'value' cannot be referenced from a static context.

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

Posted: Mon May 24, 2021 8:04 pm
by admin
Please read the code carefully. value is, in fact, static. So, there will be no such error.

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

Posted: Thu Jan 20, 2022 5:47 am
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.

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

Posted: Sat Jul 08, 2023 7:26 am
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

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

Posted: Sat Jul 08, 2023 10:38 pm
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.

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

Posted: Sat Jul 08, 2023 10:49 pm
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