About Question enthuware.ocajp.i.v7.2.1391 :
Moderator: admin
-
- Posts: 3
- Joined: Mon May 23, 2016 11:23 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1391 :
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.
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1391 :
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.
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.
Re: About Question enthuware.ocajp.i.v7.2.1391 :
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1391 :
Correct.Deleted User 3513 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?
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.So j in the while loop doesn't shadow the instance variable j?
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Aug 08, 2018 2:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1391 :
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,
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,
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1391 :
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.
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.
-
- Posts: 12
- Joined: Wed Aug 08, 2018 2:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1391 :
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,
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,
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1391 :
For the purpose of the exam, you have gone far enough
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 90 guests