About Question enthuware.ocpjp.v8.2.1711 :
Moderator: admin
-
- Posts: 9
- Joined: Wed Nov 25, 2015 11:54 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1711 :
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......
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......
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
How about a, c or b, c?
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Wed Nov 25, 2015 11:54 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
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.
-
- Posts: 9
- Joined: Wed Nov 25, 2015 11:54 pm
- Contact:
-
- Posts: 6
- Joined: Wed Dec 14, 2016 11:29 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Feb 11, 2018 11:48 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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."
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."
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
-
- Posts: 1
- Joined: Tue Mar 20, 2018 11:54 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
-
- Posts: 11
- Joined: Fri Aug 17, 2018 8:52 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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
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.
-
- Posts: 17
- Joined: Sun Sep 23, 2018 4:10 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.Dhaval Dongre wrote: ↑Mon Apr 23, 2018 2:08 pmI 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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
No, it should not be correct. As replied above, can you show a situation where the output contradicts the statement of option 3?floryan wrote: ↑Thu Oct 04, 2018 3:16 pmYes, this should be the correct answer. Especially because there's is such a huge focus on the unpredictability of thread execution in this exam.Dhaval Dongre wrote: ↑Mon Apr 23, 2018 2:08 pmI 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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Sun Sep 23, 2018 4:10 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
Ah okay, gotcha. Misleading but technically correct I guess. I didn't see the second page of the conversation here, sorry.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
So is the answer, "any combination as long as it contains a "c"? Something like that?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
Yes, you could say that.
If you like our products and services, please help us by posting your review here.
-
- Posts: 11
- Joined: Fri Aug 17, 2018 8:52 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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
-> 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
-
- Posts: 19
- Joined: Fri Aug 02, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
'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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
Correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri Dec 30, 2022 1:58 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1711 :
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.
Who is online
Users browsing this forum: No registered users and 20 guests