About Question enthuware.ocpjp.v7.2.1729 :

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

Moderator: admin

Post Reply
kavitha.veluri
Posts: 1
Joined: Fri Nov 14, 2014 7:38 pm

About Question enthuware.ocpjp.v7.2.1729 :

Post by kavitha.veluri »

Since the code in main method has t1.join() and t2.join() , the main thread will wait till t1 and t2 ends and then executes System.out.println(counter);
so , it will always print 0.

Why will it print any number between -5000 and 5000 ?

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

Re: About Question enthuware.ocpjp.v7.2.1729 :

Post by admin »

Did you read the explanation? It explains exactly why it may print any number between -5000 and 5000.
If you like our products and services, please help us by posting your review here.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1729 :

Post by sir_Anduin@yahoo.de »

But it could happen that t2 finishes before t2 starts, so t1 will spin forever.

or will it never see the update on the counter because counter is not volotile?

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

Re: About Question enthuware.ocpjp.v7.2.1729 :

Post by admin »

sir_Anduin@yahoo.de wrote:But it could happen that t2 finishes before t2 starts, so t1 will spin forever.
I not sure what you mean by t1 will spin for ever. It will only execute the loop as long as i is less that 5000 and it does increment i after every iteration. So why do you think it will keep running?
or will it never see the update on the counter because counter is not volotile?
It is possible that it will never see counter updated. But that has nothing to do with loop iterations. Loop variable is i and not counter.
If you like our products and services, please help us by posting your review here.


javaace
Posts: 3
Joined: Wed Apr 05, 2017 1:33 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1729 :

Post by javaace »

I don't get this. I ran the code 100 times and it always print 0, so the answer should be "It will always print 0"?

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

Re: About Question enthuware.ocpjp.v7.2.1729 :

Post by admin »

Did you read the explanation? You may see it print 0 a million times but there is no guarantee that it will print 0 one million one-th time.
If you like our products and services, please help us by posting your review here.

javaace
Posts: 3
Joined: Wed Apr 05, 2017 1:33 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1729 :

Post by javaace »

admin wrote:Did you read the explanation? You may see it print 0 a million times but there is no guarantee that it will print 0 one million one-th time.

okay great. thanks. that makes sense

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 49 guests