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

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

Moderator: admin

Post Reply
UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

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

Post by UmairAhmed »

Hi,
I need some explanation about this question,
class SomeClass{
   String s1 = "green mile";   // 0
   public void generateReport( int n ){
      String local;   // 1
      if( n > 0 ) local = "good";   //2
      System.out.println( s1+" = " + local );   //3
   } }
options in answer say :
1). Insert else "bad" (Which is fine as my understanding)
2). Move line 1 and place it after 0, as it local variable inside the method. (???)

If i compile this code, by adding the main method in the above code like this
class SomeClass{
   String s1 = "green mile";   // 0
   public void generateReport( int n ){
      String local;   // 1
      if( n > 0 ) local = "good";   //2
else local = "bad";
      System.out.println( s1+" = " + local );   //3
   }
public static void main(String args[]) {
SomeClass aa = new SomeClass(); // for using the generateReport() method
aa.generateReport(5);
}}
This code works fine even we are not making automatic variable "local" as instance variable.
Isn't the second option redundant ... as this question can be answered completely with 1st option only.

Please explain if I am missing something with respect to this question.
Regards
/Umair

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

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

Post by admin »

The problem statement says, "Which of the changes given in options can be done (independent of each other)...". So both the changes are not to be done together. Either of the changes will make it work.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

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

Post by UmairAhmed »

Thanks Paul

dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

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

Post by dmcinnis1 »

I still do not understand why the second option doesn't work. I don't understand why the compiler has a problem with it. Wouldn't the compiler look at both statements independently of each other?

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

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

Post by admin »

That is how the Java compiler is designed. It cannot execute the code it can only make inferences by looking at information available at compile time. In the second option, the two ifs are independent and therefore, the compiler cannot assume that i will be initialized after the second if.
If you like our products and services, please help us by posting your review here.

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

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

Post by JaredTse »

Can someone please elaborate on this, as to why is wrong. I can't seem to wrap my head around it.

Insert after line 2 : if(n <= 0) local = "bad";

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

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

Post by admin »

Can you elaborate what exactly is causing the confusion to you so that we can help?
Also, did you try compiling this option? What error message do you get?
-Paul.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

So the compiler works like this:
If there is at most 1 statement that would initilize the variable, then compiler allows the uninutilize variable otherwise it doesn't.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests