About Question enthuware.ocpjp.v8.2.1391 :

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

Moderator: admin

Post Reply
sandeshchaudhari
Posts: 2
Joined: Thu Nov 24, 2016 7:26 am
Contact:

About Question enthuware.ocpjp.v8.2.1391 :

Post by sandeshchaudhari »

When i run this program it prints nothing in the o/p but answer given is 0 2 4. Can you please tell me why this?

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

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

Post by admin »

Did you read the explanation? It explains exactly what you are asking.
If you like our products and services, please help us by posting your review here.

runnerdave
Posts: 12
Joined: Mon Jan 30, 2017 2:58 pm
Contact:

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

Post by runnerdave »

Can you please explain why the results are always ordered?

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

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

Post by admin »

The main thread prints the value of data variable of each thread one by one in the for loop. So irrespective of what the value of data variable is for any thread, the first value printed will be the data value of the first thread, second value printed will be the data value of the second thread, and the third value printed will be the data value of the third thread.
If you like our products and services, please help us by posting your review here.

shatterblast
Posts: 5
Joined: Fri Feb 03, 2017 3:25 am
Contact:

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

Post by shatterblast »

Will you please explain why the output is always in increments of 2?

I try the following code, and the output comes out as 0 2 4 6 8. Thank you for your time.

Code: Select all

    ...  //Other code.
    
    public static void main(String[] args) throws Exception
    {
        Test[] jta = new Test[5];
        for(int i=0; i<5; i++) {
            jta[i] = new Test(i); jta[i].start();
        }
        for(Test jt : jta) {
            if(jt.isDone()) System.out.println(jt.getData());
        }
    }
    
    ...  //Whatever else.

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

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

Post by admin »

Because of the statement data += data; in run() method. data += data; is mathematically same as data = 2*data.
If you like our products and services, please help us by posting your review here.

zukras
Posts: 5
Joined: Fri Jun 02, 2017 4:57 pm
Contact:

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

Post by zukras »

Hi,

I don't understand when it might print 0, 1, 2.
  1. It will print something only if jt.isDone() returns true.
  2. jt.isDone() returns true only when run() is executed.
  3. When run() is executed data is always equals to 2*data
What I am missing in this question?

Thanks

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

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

Post by admin »

You are not missing anything. It can't print 0, 1, 2. The question does not say that it may print 0, 1, 2 either.
If you like our products and services, please help us by posting your review here.

prashantjain25
Posts: 9
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

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

Post by prashantjain25 »

This got me confused it should also print 0,1,2 but it never does that. it prints all of these
0
0,2
0,4
0,2,4
but never 0,1,2 why?

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

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

Post by admin »

You will need to read the explanation. It explains exactly why 0 1 2 cannot be printed.
If you like our products and services, please help us by posting your review here.

moostebring
Posts: 3
Joined: Tue Jul 21, 2020 3:28 am
Contact:

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

Post by moostebring »

One question though. Hypothetically, if the first thread in the list for whatever reason is not ready upon the evaluation of the if(isDone()) method, it get's skipped and 2 - 4 would get printed right? Is that also the reason why the answers state 0 2 4 is a "possible" solution?

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

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

Post by admin »

That's right. But that is not the reason for 0 2 4 .
0 2 4 will be printed if all three threads are done before the main thread is able to execute the second loop.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

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

Post by jme_chg »

Just to confirm,
all possible outputs are:
0 2 4
0 2
0 4
0
2 4
2
4
?

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

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

Post by admin »

That is correct.
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] and 38 guests