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

Moderator: admin

Post Reply
pink.freak
Posts: 3
Joined: Mon May 23, 2016 11:23 am
Contact:

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

Post by pink.freak » Mon May 23, 2016 11:31 am

Hi I started using enthuware recently and I have encountered this type of for loop in the Practice Test:

for(int j=1; j <= 5;)

I have answered that it will not compile. However the answer says otherwise.
If I would correct the for loop it would look something like this:

for(int j=1; j <= 5;;)

I would like to clarify why my answer was incorrect.

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

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

Post by admin » Mon May 23, 2016 11:48 am

Hi,
What happened when you tried to compile both the versions (i.e. the one given in the question and the one you have proposed)?
-Paul.
If you like our products and services, please help us by posting your review here.

mrmartinbriones
Posts: 15
Joined: Sat Dec 24, 2016 9:39 pm
Contact:

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

Post by mrmartinbriones » Tue Jan 31, 2017 8:35 am

In the while loop, j is referring to the instance variable declared above the method showJ()? This currently has the default value of 0. Which is the same reason why I don't have to initialize the j in the while loop? and then the j in the for loop shadows the instance variable j? So j in the while loop doesn't shadow the instance variable j?

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

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

Post by admin » Tue Jan 31, 2017 9:07 am

mrmartinbriones wrote:In the while loop, j is referring to the instance variable declared above the method showJ()?
Correct.
This currently has the default value of 0. Which is the same reason why I don't have to initialize the j in the while loop?
Correct.
and then the j in the for loop shadows the instance variable j?
Correct.
So j in the while loop doesn't shadow the instance variable j?
No, there is no new j in while loop's scope (the one inside the for loop is not visible outside the for loop). The only j available in while is the instance variable j.
If you like our products and services, please help us by posting your review here.

abutalib
Posts: 3
Joined: Wed Aug 08, 2018 2:56 am
Contact:

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

Post by abutalib » Sun Sep 23, 2018 3:36 am

Hi,

I have a confusion between variable names scope and reference scope. So, based on my understanding I created the following list.

{
There are two types of scopes for variable in Java: name scope and reference scope.

Variable Name scopes:
- Variable names are not allowed to duplicate within one scope.
- They are: 1. outside methods 2. inside methods

Reference scopes:
- They are allowed to be used in the scope they were declared in.
- They are: 1. class 2. instance 3. method scope 4. loop scope 5. if scope 6. switch scope

Both types of scopes are hierarchical. So variable from child scope can access parent scope variables, but parent cannot access child scope variables.
}

Am I understanding this correctly?

Cheers,

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

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

Post by admin » Sun Sep 23, 2018 7:02 am

Yes, that seems logical but "name scope" and "reference scope" are not real or technical terms so I will not be able to vouch for the correctness of what you have written.
You should go through Section 2.1 of OCAJP Associate Java 8 Programmer Certification Fundamentals by Hanumant Deshmukh. You can download it for free from here for the time being.

Go through this section and let me know if you still have doubts about scopes.

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

abutalib
Posts: 3
Joined: Wed Aug 08, 2018 2:56 am
Contact:

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

Post by abutalib » Sun Sep 23, 2018 10:57 am

Thanks Paul, Logical is what I needed to hear.

I got two additional things from the book:
1. you can have nested unnamed block codes with their own scope.
2. Two declared entities with the same name can share the method scope if they do not cross.
Example:
public static void main(String[] args){
{int i = 1; System.out.println(i);}
int i = 2; System.out.println(i);
}

Java never ceases to amaze me. Dear I ask how far does this rabbit hole go?

Cheers,

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

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

Post by admin » Sun Sep 23, 2018 10:40 pm

abutalib wrote:
Sun Sep 23, 2018 10:57 am
Java never ceases to amaze me. Dear I ask how far does this rabbit hole go?
For the purpose of the exam, you have gone far enough :D
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 3 guests