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: 10036
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: 10036
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: 10036
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: 202
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.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

I find it hard to understand how far the compiler can go to check if a local variable will be initialized. I understand it can go to into max 1 if-else function, but are there other functions it can look into? For example a for loop function? Or in a print function?
For example:
public static void localMethod() {
String A;
String B = "b";
System.out.println(A = "x");
}
I tried it out and the compiler does not complain. It is able to see that the local variable A is initialized within the print call. This also made me wonder if replacing "x" with B will matter, as A is then assigned to a variable instead of a compile time constant (and it doesn't matter).

I guess what I mean to ask: what is the exact 'depth' a compiler can check?

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

I tested into it a bit more and a for-loop never works. But surprisingly a switch statement does (as long as a default case is given)..

public static void localMethod() {
boolean bool = true;
String A;
// if (bool) A = "abc"; //compile error
// if (bool) A = "abc"; else A = "abc"; //OK
// for(int i = 0; i<2;i++){A = "abc";}; //compile error
// for(int i = 1; i==1;i=1){A = "abc";};//compile error
// A = bool ? "a" :"b"; //OK
// A = bool ? !bool ? "a" : "b" : "c"; //OK
// A = bool ? !bool ? "a" : bool ? "a" : "b" : "c"; //OK, same goes for nested if

final int i = 1;
switch (i){
case 0: A = "0"; break;
case 1: A = "1"; break; //even though i is final and the case 1 is here, it doesn't work if there is no default case.
default: A = "def"; //Only works when switch has default!
}

System.out.println(A);
}

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

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

Post by admin »

For the kind of precision that you are looking for, please go through Chapter 16 (Definte Assignment) of JLS carefully: https://docs.oracle.com/javase/specs/jl ... ls-16.html
If you like our products and services, please help us by posting your review here.

Syams123
Posts: 3
Joined: Sun Sep 26, 2021 5:12 pm
Contact:

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

Post by Syams123 »

Hi,

What about n variable which is not explicitly initialized so how we are going to evaluate if statement without declaring n value? And even though string local value is not initialized at first its assigned value through if/else statement right?

Can please someone let me know.

Thanks

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

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

Post by admin »

n is a method parameter. It is always initialized by the value sent by the caller of this method. The caller has to send a value when invoking this method otherwise the code will not compile.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests