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

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

Moderator: admin

Post Reply
ahidell
Posts: 4
Joined: Mon Apr 08, 2013 7:12 pm
Contact:

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

Post 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.

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

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

Post by admin »

Not sure how you can do an infinite loop on an array using the enhanced for loop.
-Paul.
If you like our products and services, please help us by posting your review here.

ahidell
Posts: 4
Joined: Mon Apr 08, 2013 7:12 pm
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

ahidell
Posts: 4
Joined: Mon Apr 08, 2013 7:12 pm
Contact:

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

Post 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

ahidell
Posts: 4
Joined: Mon Apr 08, 2013 7:12 pm
Contact:

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

Post by ahidell »

Oh well your software worked, just took the exam this morning and passed with a 96% thanks!

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

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

Post by admin »

That's great! Congratulations :)
If you like our products and services, please help us by posting your review here.

JeramieH
Posts: 22
Joined: Wed Jan 08, 2014 11:24 am
Contact:

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

Post 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?

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

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

Post 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
If you like our products and services, please help us by posting your review here.

JeramieH
Posts: 22
Joined: Wed Jan 08, 2014 11:24 am
Contact:

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

Post by JeramieH »

admin wrote:In that case you will get a ConcurrentModificationException
Alas, good point. 8-)

thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

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

Post by thchuong »

As far as I know the Map type is not included in 1Z0-803 exam so why do you put it in?

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

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

Post by admin »

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.

Vermeulen
Posts: 12
Joined: Wed Jul 15, 2015 4:05 pm
Contact:

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

Post 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);
		}
	}
}

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

skippy
Posts: 2
Joined: Wed Aug 26, 2015 8:32 am
Contact:

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

Post 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.

ayedodo
Posts: 1
Joined: Mon Mar 21, 2016 9:41 am
Contact:

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

Post 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...

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

HashMap is a collection.
The OCA exam didn't treat collections.

Thanks Paul.

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

Post 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 :)

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

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

Post 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 :)
If you like our products and services, please help us by posting your review here.

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

Post 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...

JMartins
Posts: 1
Joined: Sun Feb 04, 2018 8:05 pm
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

timwaagh
Posts: 6
Joined: Mon Sep 16, 2019 4:11 am
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 81 guests