Page 1 of 1
About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed May 08, 2013 2:55 pm
by ahidell
Using an enhanced for loop does not prevent your code from going into an infinite loop. It's still very possible. I get the point, it helps prevent it, but does not prevent it altogether.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed May 08, 2013 4:00 pm
by admin
Not sure how you can do an infinite loop on an array using the enhanced for loop.
-Paul.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed May 08, 2013 7:31 pm
by ahidell
Code: Select all
int [] array = {1,2,3,4,5};
for (int i : array) {
while(i!=0) {};
}
Now this is obviously very stupid but the point is one of the correct answers according to the test was "Using an enhanced for loop prevents the code from going into an infinite loop." and that simply is not true. As you can see my use of the enhanced for loop did not prevent my code from going into an infinite loop.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Thu May 09, 2013 6:05 am
by admin
This is an infinite loop using a while loop, not using the enhanced for loop. I think you are taking the statement out of context. By your reasoning, I can say that I can do just about anything using an enhanced for loop, such as open a file, throw an exception, exit a program, create a class or whatever because I can write any code inside the loop body. But that has nothing to do with the loop as such.
A similar (and wrong) argument can be made to say that using try-with-resources statement does not prevent leaving resources unclosed.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Thu May 09, 2013 7:30 am
by ahidell
Yes but using the enhanced for loop did not "prevent" the code from going into an infinite loop. The wording on that choice seems to suggest the enhanced for loop has special powers that will prevent this type of behavior.
The wording of that selection could be better. I see your point though, using the enhanced for loop prevents something like for (;;){} or similar nonsense.
Thanks
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Thu May 09, 2013 10:10 am
by ahidell
Oh well your software worked, just took the exam this morning and passed with a 96% thanks!
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Thu May 09, 2013 10:54 am
by admin
That's great! Congratulations

Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed May 07, 2014 4:10 pm
by JeramieH
Using an enhanced for loop prevents the code from going into an infinite loop.
Could another thread be aggressively modifying the Collection at the same time?
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed May 07, 2014 7:53 pm
by admin
JeramieH wrote:Using an enhanced for loop prevents the code from going into an infinite loop.
Could another thread be aggressively modifying the Collection at the same time?
In that case you will get a ConcurrentModificationException
http://docs.oracle.com/javase/7/docs/ap ... ption.html
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Thu May 08, 2014 10:33 am
by JeramieH
admin wrote:In that case you will get a ConcurrentModificationException
Alas, good point.

Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed Sep 10, 2014 2:47 am
by thchuong
As far as I know the Map type is not included in 1Z0-803 exam so why do you put it in?
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed Sep 10, 2014 7:53 am
by admin
Because people have reported getting such questions in the exam.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed Jul 15, 2015 4:10 pm
by Vermeulen
Although the enhanced for loop certainly helps prevent infinite loops, it's certainly not impossible. Try the following code:
Code: Select all
import java.util.Iterator;
public class EnhancedForInfiniteLoop {
private static class IterateMeIfYouDare implements Iterable<Object> {
@Override
public Iterator<Object> iterator() {
return new Iterator<Object>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public Object next() {
return "Hello infinite world!";
}
};
}
}
public static void main(String[] args) {
for (Object o : new IterateMeIfYouDare()) {
System.out.println(o);
}
}
}
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed Jul 15, 2015 9:25 pm
by admin
Actually the code you've shown does not illustrate the problem with the loop. Your collection does have infinite members

But you have made a good point and l will leave it here for others to see. From the exam perspective though, the option is correct.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Wed Aug 26, 2015 8:41 am
by skippy
I also came here to say that the statement that the foreach loop prevents infinite loops is incorrect in my opinion except when iterating over an array. When iterating over Iterable, it's up to the implementation how the iteration will go.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Mon Mar 21, 2016 9:44 am
by ayedodo
I'm still not convinced that the enhanced for loop prevents infinite loops. Consider the following code, which results in an infinite loop:
Code: Select all
package com.company;
import java.util.Iterator;
public class Infinite implements Iterable<String> {
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public String next() {
return "Never ending";
}
};
}
public static void main(String[] args) {
Infinite infinite = new Infinite();
for (String string : infinite) {
System.out.println(string);
}
}
}
EDIT: Oups, I did not see the second page of the forum where this was already discussed...
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Fri Mar 03, 2017 12:31 pm
by AndaRO
HashMap is a collection.
The OCA exam didn't treat collections.
Thanks Paul.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Sat Sep 16, 2017 6:26 am
by ramon.carrascom
For everything I'm seeing while preparing this exam, words mean whatever Oracle testers want them to mean. In this particular question, "Using an enhanced for loop prevents the code from going into an infinite loop" may be the "most correct answer", but it's as "imprecise" as other i've found saying that "exceptions are for logging unexpected behavior" (don't remember exactly question number). And this is not fair for exam takers.
Just a reflexion

Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Sat Sep 16, 2017 6:41 am
by admin
There are a few questions like that in the exam but not all questions are like that. After all, questions of the real exam are created by human beings. So you cannot eliminate all subjectivity

Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Sat Sep 16, 2017 6:54 am
by ramon.carrascom
hahaha but you'll agree with me that an objective-test-style-exam can't (shouldn't) contain subjective-style-questions or subjective-style-answers. And, if subjective-style-answers are allowed, you also must allow me to explain the answer, cause I can give a correct explanation to a first sight incorrect sentence...
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Mon Feb 05, 2018 5:47 pm
by JMartins
The solution consider this : "It can iterate over an array or a Collection but not a Map." as a correct answer...However we can perfectly iterate over a Map using the enhaced for loop :
The bellow code is valid :
Code: Select all
for (Map.Entry<String,String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
So I think the anwser is incorrect.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Mon Feb 05, 2018 9:30 pm
by admin
You are not iterating over a Map here. You are iterating over a Set returned by the entrySet() method of a Map. By this logic, you can iterate over any class that has a method that returns an array or a Collection. Doesn't have to be Map.
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Fri Oct 11, 2019 7:51 am
by timwaagh
help me understand this. enhanced for loop+array to create infinite loop
int[] arr = {1,2}
for (int i : arr){ //seems finite but what if arr changes?
arr = new int[array.length+1];
for(int j =0;j<array.length;j++) {
array[j] = j;
} //non infinite loop just to initialize the bigger array.
}
could i do something like this? or would the loop just end up using the old array instead of replacing it as we go?
Re: About Question enthuware.ocajp.i.v7.2.869 :
Posted: Fri Oct 11, 2019 8:02 am
by admin
Assuming you have gone through the basics of refe rences and objects, what do you you think will happen?
It is quite easy to try out also.