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

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

Moderator: admin

Post Reply
Michailangelo

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

Post by Michailangelo »

Its strange how final modifier is permitted here since the variable o2 will change its values during the loop.

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

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

Post by admin »

Michailangelo wrote:Its strange how final modifier is permitted here since the variable o2 will change its values during the loop.
In this case, the scope of o2 ends at the end of the loop block. So it is like declaring a new variable (with the same name, of course) at the start of each iteration. And that is why it is ok to make it final.

But yes, it does look strange :)
If you like our products and services, please help us by posting your review here.

ETS user

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

Post by ETS user »

Will questions on Collections and iterators be included in the Associate Programmer test (1Z0-803)?

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

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

Post by admin »

ETS user wrote:Will questions on Collections and iterators be included in the Associate Programmer test (1Z0-803)?
No, we don't expect any tough questions on Collections and Iterators. But it would be good to know the basic concepts that touch the topics covered on the exam such as this one.

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

Guest

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

Post by Guest »

Object o = null;
Collection c = //valid collection object.
int[][] ia = //valid array

option 4 :
for(int i : ia[0]){ }
Since ia[0] is an array of ints, this is valid. (It may throw a NullPointerException or ArrayIndexOutOfBoundsException at runtime if ia is not appropriately initialized.)

ia is a two dimensional array right? How can int i be assigned ia[0]?
ia[0] is pointing to an array of ints right?

What am I missing?

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

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

Post by admin »

Yes, ia is a two dimensional array. So ia[0] is a 1 dimensional array. Therefore, when you iterate through each element of ia[0], you get ints. It is those ints, which you assign to i one by one in the for loop and not ia[0] itself.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

Of Course...:-)
Thanks.

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post by javaman »

I hope this is not part of 1Z0-803 since I don't understand how (or even if) an interator object can be used within an enhanced for-loop after this explanation:

"c.iterator() does not return any Collection. Note that the following would have been valid: Collection<Iterator> c = //some collection that contains Iterator objects for(Iterator it : c){ }"

If I do this
ArrayList<Integer> arr = new ArrayList<>();
arr.add(0);
arr.add(1);
arr.add(2);
Iterator<Integer> it = arr.iterator();
for(Iterator i : it){
System.out.println("it.next()= " + it.next());
}

I get

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - for-each not applicable to expression type
required: array or java.lang.Iterable

How should I define an interator so I can use it in an enhanced for loop??

Thanks,
Marc

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

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

Post by admin »

You can't use an iterator in a for loop. If you have an iterator, you can do:

Iterator it = //get iterator somehow
while(it.hasNext()){
it.next();
}


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

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

In this case, the scope of o2 ends at the end of the loop block. So it is like declaring a new variable (with the same name, of course) at the start of each iteration. And that is why it is ok to make it final.
Paul, you specify that the scope of o2 ends at the end of the loop block (I'm assuming you mean after all the iterations have completed and not after each iteration). Thus, how can you have redundant final Object o2 references? Are both final variables and final references not reassignable? Final references may be different than final variables (in which case I believe the reference simply points to a new object and the original object is eligible for garbage collection?).

Please let me know whether my understanding is correct or not. Thank you!

Sincerely,
James

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

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

Post by admin »

I am not sure I understand your question correctly but it seems you are confusing multiple things:
1. GC has nothing to do with whether the reference is final or not. An object will be GCed if there is no reference to that object.
2. In Java, a non-primitive variable is also known as a reference. There is no distinction between a variable and a reference. It is a "reference variable". You cannot get a reference to any object without having a variable. A primitive variable is just a variable. There is no reference for primitives.
3. Now, when you declare a variable final that means you can't change its value. If the variable is a non-primitive, its value is nothing but a reference, and if it is a primitive variable, its value is just the value. There is nothing like a "final reference". The keyword final applies to variables only (it could be a reference variable or a primitive variable).

In the given code, o2 is final for each iteration. Inside the for loop, you cannot make o2 point to another object. For example:
for(final Object o2 :c){
o2 = ""; //This is invalid because o2 is final.
}

for(Object o2 :c){
o2 = ""; //This is ok because o2 is not final.
}

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

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

Yes, I believe I understand your explanation. In essence, your for loop is iterating over each object in the Collection c, and simply creating a final reference variable o2 for each one? Perhaps this is where my question lay, are we creating multiple final o2 references; if so, that is allowed?

Lastly, as long as we do not assign these references to anything, then the code compiles successfully?

Apologies if it is is obvious and not clicking yet, thank you for your patience.

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

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

Post by admin »

Yes, each one goes out of scope after that iteration is over.
If you like our products and services, please help us by posting your review here.

likejudo
Posts: 26
Joined: Sun Feb 18, 2024 7:21 pm
Contact:

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

Post by likejudo »

Is a new final variable created each time? I thought the same variable is reused in the for-each loop iterations.
I thought it is like a cursor over a data set.

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

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

Post by admin »

So if you declare a variable in a method and if that method is called a 100 times, how many times is that variable created? The same thing happens when you declare a variable inside a loop.
If you like our products and services, please help us by posting your review here.

likejudo
Posts: 26
Joined: Sun Feb 18, 2024 7:21 pm
Contact:

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

Post by likejudo »

I thought the initial assignment is always before the loop?
As I have understood, the for loop is equivalent to a while loop with the assignment happening once just above the conditional. That would make a different from a method body

Assignment;
While(conditional) {
}

Correction. After thinking a bit I realized this is a for-each loop. I wonder how it is equivalent to the while loop. I don't recall reading the equivalence (as I described above) with the while loop.

Post Reply

Who is online

Users browsing this forum: No registered users and 227 guests