About Question enthuware.ocajp.i.v7.2.869 :
Moderator: admin
-
- Posts: 4
- Joined: Mon Apr 08, 2013 7:12 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.869 :
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
Not sure how you can do an infinite loop on an array using the enhanced for loop.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Mon Apr 08, 2013 7:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
Code: Select all
int [] array = {1,2,3,4,5};
for (int i : array) {
while(i!=0) {};
}
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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.
A similar (and wrong) argument can be made to say that using try-with-resources statement does not prevent leaving resources unclosed.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Mon Apr 08, 2013 7:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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
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
-
- Posts: 4
- Joined: Mon Apr 08, 2013 7:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
Oh well your software worked, just took the exam this morning and passed with a 96% thanks!
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
That's great! Congratulations
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Jan 08, 2014 11:24 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
Could another thread be aggressively modifying the Collection at the same time?Using an enhanced for loop prevents the code from going into an infinite loop.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
In that case you will get a ConcurrentModificationException http://docs.oracle.com/javase/7/docs/ap ... ption.htmlJeramieH wrote:Could another thread be aggressively modifying the Collection at the same time?Using an enhanced for loop prevents the code from going into an infinite loop.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Jan 08, 2014 11:24 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
Alas, good point.admin wrote:In that case you will get a ConcurrentModificationException
-
- Posts: 8
- Joined: Wed Sep 10, 2014 2:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
As far as I know the Map type is not included in 1Z0-803 exam so why do you put it in?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
Because people have reported getting such questions in the exam.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Jul 15, 2015 4:05 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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);
}
}
}
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Aug 26, 2015 8:32 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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.
-
- Posts: 1
- Joined: Mon Mar 21, 2016 9:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
I'm still not convinced that the enhanced for loop prevents infinite loops. Consider the following code, which results in an infinite loop:
EDIT: Oups, I did not see the second page of the forum where this was already discussed...
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);
}
}
}
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
HashMap is a collection.
The OCA exam didn't treat collections.
Thanks Paul.
The OCA exam didn't treat collections.
Thanks Paul.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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
Just a reflexion
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Aug 27, 2017 12:35 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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...
-
- Posts: 1
- Joined: Sun Feb 04, 2018 8:05 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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 :
So I think the anwser is incorrect.
The bellow code is valid :
Code: Select all
for (Map.Entry<String,String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Mon Sep 16, 2019 4:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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?
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?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.869 :
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.
It is quite easy to try out also.
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 149 guests