Page 1 of 1

About Question enthuware.ocpjp.v8.2.1711 :

Posted: Sun Dec 06, 2015 12:40 am
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......

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

Posted: Sun Dec 06, 2015 2:08 am
by admin
How about a, c or b, c?

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

Posted: Sun Dec 06, 2015 4:36 am
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.

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

Posted: Sun Dec 06, 2015 4:43 am
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.

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

Posted: Sun Dec 06, 2015 4:46 am
by ashish
Ok thanks.

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

Posted: Thu Dec 15, 2016 7:38 pm
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.

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

Posted: Fri Dec 16, 2016 1:17 am
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.

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

Posted: Sun Feb 11, 2018 3:30 pm
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."

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

Posted: Sun Feb 11, 2018 9:50 pm
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.

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

Posted: Mon Apr 23, 2018 2:08 pm
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.

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

Posted: Mon Apr 23, 2018 7:57 pm
by admin
Can you show a situation where the output contradicts the statement of option 3?

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

Posted: Wed Aug 29, 2018 4:22 am
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.

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

Posted: Wed Aug 29, 2018 6:37 am
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

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

Posted: Thu Oct 04, 2018 3:16 pm
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.

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

Posted: Thu Oct 04, 2018 3:22 pm
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?

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

Posted: Thu Oct 04, 2018 3:29 pm
by floryan
Ah okay, gotcha. Misleading but technically correct I guess. I didn't see the second page of the conversation here, sorry.

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

Posted: Wed Oct 31, 2018 9:06 pm
by Mark7777
So is the answer, "any combination as long as it contains a "c"? Something like that?

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

Posted: Wed Oct 31, 2018 9:10 pm
by admin
Yes, you could say that.

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

Posted: Fri Dec 14, 2018 8:14 am
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

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

Posted: Sat Dec 28, 2019 3:18 am
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.

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

Posted: Sat Dec 28, 2019 4:23 am
by admin
Correct.

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

Posted: Fri Dec 30, 2022 2:10 pm
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.

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

Posted: Sat Dec 31, 2022 2:02 am
by admin
Yes, ideally, "none" could also be added to the exclude list.