About Question enthuware.ocpjp.v8.2.1711 :

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

Moderator: admin

Post Reply
ashish
Posts: 9
Joined: Wed Nov 25, 2015 11:54 pm
Contact:

About Question enthuware.ocpjp.v8.2.1711 :

Post by ashish »

Wrong answer given for below question.


import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class Cache {

static ConcurrentHashMap<String, Object> chm = new ConcurrentHashMap<String, Object>();

public static void main(String[] args) {
chm.put("a", "aaa");
chm.put("b", "bbb");
chm.put("c", "ccc");
new Thread() {
public void run() {
Iterator<Entry<String, Object>> it = Cache.chm.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Object> en = it.next();
if (en.getKey().equals("a") || en.getKey().equals("b")) {
it.remove();
}
}
}
}.start();
new Thread() {
public void run() {
Iterator<Entry<String, Object>> it = Cache.chm.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Object> en = it.next();
System.out.print(en.getKey() + ", ");
}
}
}.start();
}
}


Options
1 : It may print any combination of the keys.
2 : It may print any combination except: c,
3: It may print any combination except: a, or b, or a, b, or b, a
4 : It may print any combination except: b, c,
5 : It may print any combination except: a, b,

Answer Given - Option 3

Correct Ans should be - It may print either a,b,c combination or c only.
We have 2 thread and both are running independently so as per prority either it will print (a,b,c) or c only.
But never any combination a, or b like......

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

How about a, c or b, c?
If you like our products and services, please help us by posting your review here.

ashish
Posts: 9
Joined: Wed Nov 25, 2015 11:54 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by ashish »

Please run same example in editor.

I tried 100 times i m same getting answer as per my comments.

If you have any sample program which will help to clear my doughts then share with me.

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

More important than what is actually printed is what all can potentially be printed.
In this case, it is possible for the code to print a, c or b, c (although you may not see that happening in your test runs). The reason is explained in the explanation.
That is why the given option is correct and your suggestion is incorrect.
-Paul.
If you like our products and services, please help us by posting your review here.

ashish
Posts: 9
Joined: Wed Nov 25, 2015 11:54 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by ashish »

Ok thanks.

ibugaienko
Posts: 6
Joined: Wed Dec 14, 2016 11:29 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by ibugaienko »

Hello Paul,

One question here. How about combinations of c, [a, b,], that is combinations with wrong iteration order? These can be ruled out straight off the start, as far as I understand. But the correct answer does not mention them.

BR.

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

The correct option does mention it. It says, "It may print any combination except: a, or b, or a, b, or b, a". This includes c a b and c b a.

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

gaurav.sjsu
Posts: 2
Joined: Sun Feb 11, 2018 11:48 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by gaurav.sjsu »

the answer to the question: Which of the following are possible outputs when the above program is run? is not right.
The reason is there is no guarantee from JVM about the threat execution order.

had the main thread called join on the first thread then the answer & explanation would have been right.

Therefore, the answer should be "It may print any combination of the keys."

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

You need to read the explanation carefully. Specially the sentence, "However, "c" is never removed from the map and so c will always be printed.
If you like our products and services, please help us by posting your review here.

Dhaval Dongre
Posts: 1
Joined: Tue Mar 20, 2018 11:54 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by Dhaval Dongre »

I think the answer should be, "It may print any combination of the keys." In a real world scenario the selected answer sounds most reasonable. But thread execution in this case is unpredictable and in the rarest chance it is possible that the second thread runs first, prints everything and then the first thread runs. Thus the answer must be unpredictable.

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

Can you show a situation where the output contradicts the statement of option 3?
If you like our products and services, please help us by posting your review here.

Touciuciu
Posts: 11
Joined: Fri Aug 17, 2018 8:52 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by Touciuciu »

The answer should be "It may print any combination of the keys". Tested with STS and the output was "a,b,c". It is true that most of the time it will print c and maybe it will combine it with a or b but the question says that we should be able to determine the possible output so one possible output is a, b, c. Very rare but possible, this is why your answer is wrong. Please admit that is wrong and correct it.

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

Yes, it can print "a, b, c". That is why option 3 is correct. Please read the option carefully. It says "except".
Option 1 cannot be correct because it will never print these four possibilities:
1. a,
2. b,
3. a, b,
4. b, a
If you like our products and services, please help us by posting your review here.

floryan
Posts: 17
Joined: Sun Sep 23, 2018 4:10 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by floryan »

Dhaval Dongre wrote:
Mon Apr 23, 2018 2:08 pm
I think the answer should be, "It may print any combination of the keys." In a real world scenario the selected answer sounds most reasonable. But thread execution in this case is unpredictable and in the rarest chance it is possible that the second thread runs first, prints everything and then the first thread runs. Thus the answer must be unpredictable.
Yes, this should be the correct answer. Especially because there's is such a huge focus on the unpredictability of thread execution in this exam.

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

floryan wrote:
Thu Oct 04, 2018 3:16 pm
Dhaval Dongre wrote:
Mon Apr 23, 2018 2:08 pm
I think the answer should be, "It may print any combination of the keys." In a real world scenario the selected answer sounds most reasonable. But thread execution in this case is unpredictable and in the rarest chance it is possible that the second thread runs first, prints everything and then the first thread runs. Thus the answer must be unpredictable.
Yes, this should be the correct answer. Especially because there's is such a huge focus on the unpredictability of thread execution in this exam.
No, it should not be correct. As replied above, can you show a situation where the output contradicts the statement of option 3?
If you like our products and services, please help us by posting your review here.

floryan
Posts: 17
Joined: Sun Sep 23, 2018 4:10 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by floryan »

Ah okay, gotcha. Misleading but technically correct I guess. I didn't see the second page of the conversation here, sorry.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by Mark7777 »

So is the answer, "any combination as long as it contains a "c"? Something like that?

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

Yes, you could say that.
If you like our products and services, please help us by posting your review here.

Touciuciu
Posts: 11
Joined: Fri Aug 17, 2018 8:52 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by Touciuciu »

Yes, the question is very confussing because you might think that:
-> Any combination of the keys can be printed (one of any combination is a, b, c which is possible)
-> Any combination, excluding .... (one of any combination is a, c or b, c)
-> The output cannot be determined

We understand the explanation but the question statement and answers are confussing and missleading. You can have a multiple choice option for this question

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by Bhaskar »

admin wrote:
Fri Dec 16, 2016 1:17 am
The correct option does mention it. It says, "It may print any combination except: a, or b, or a, b, or b, a". This includes c a b and c b a.

HTH,
Paul.
'b, a' is a possible output because ConcurrentHashMap guarantees no order of elements right? i.e, [b,a,c] or [b,c,a] is a possible key entry in the Map.

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

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

jost.boekemeier
Posts: 1
Joined: Fri Dec 30, 2022 1:58 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by jost.boekemeier »

admin wrote:
Wed Aug 29, 2018 6:37 am
Yes, it can print "a, b, c". That is why option 3 is correct.
Well, if it can print a,b,c, then option 3 is certainly not correct, as its exclude list cannot be empty: "except: a, or b, or a, b, or b, a".

You could add "none" to the exclude list to fix this, but as it stands, option #1 is more correct than option #3.

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

Re: About Question enthuware.ocpjp.v8.2.1711 :

Post by admin »

Yes, ideally, "none" could also be added to the exclude list.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot], marpiva and 29 guests