About Question enthuware.ocpjp.v8.2.1078 :

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

Moderator: admin

Post Reply
sharpmario
Posts: 4
Joined: Sun Jul 31, 2016 12:21 pm
Contact:

About Question enthuware.ocpjp.v8.2.1078 :

Post by sharpmario »

Hi, the question has the follow options:
0 1
1 1
0 0
1 2
and the right answer is "0 1" and "0 0" but, if "0 0" is correct, should not "1 1" be correct as well? The explanation states it, taking in consideration that is not known the value of the variable before the execution.

Thanks

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

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

Post by admin »

No, 1 1 is not possible because the initial value is count is 0. Observe that count is defined as private int count; so it will be initialized to the default value of 0. So the thread that executes print first will print 0.

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

sharpmario
Posts: 4
Joined: Sun Jul 31, 2016 12:21 pm
Contact:

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

Post by sharpmario »

Ok, thanks for your help! :thumbup:

Harvey Manfrenjensen
Posts: 14
Joined: Fri May 11, 2018 6:59 am
Contact:

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

Post by Harvey Manfrenjensen »

Hi, I tried to reproduce the result of this question
(please see the code below), but i get
0 1 and 1 0 as result, but 1 0 is not the possible choice.
What could be wrong?
Regards, Paul

Code: Select all

public class enthuware_1078 {
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++){
            System.out.print("i = " + i + "\t");
            MyCounter c = new MyCounter();
            MyRunnable mr = new MyRunnable(c);
            Thread t1 = new Thread(mr);
            Thread t2 = new Thread(mr);
            t1.start();
            t2.start();
            try {
                t1.join();
                t2.join();
            } catch (InterruptedException ex) {
            }
            System.out.println();
        }
    }
}

class MyRunnable implements Runnable{
    private MyCounter counter;
    MyRunnable(MyCounter counter){
        this.counter = counter;
    }
            
    @Override
    public void run() {
        counter.increment();
    }
}

class MyCounter {
    private int count;
    public void increment(){
        System.out.print(count++);
    }
}

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

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

Post by admin »

There is nothing wrong. 1 0 is just not one of the options. That doesn't mean it is not a valid output. The question is only asking you to select possible outputs out of the ones that it has given. It does not say that it has listed all possible outputs.

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

Harvey Manfrenjensen
Posts: 14
Joined: Fri May 11, 2018 6:59 am
Contact:

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

Post by Harvey Manfrenjensen »

admin wrote:There is nothing wrong. 1 0 is just not one of the options. That doesn't mean it is not a valid output. The question is only asking you to select possible outputs out of the ones that it has given. It does not say that it has listed all possible outputs.

HTH,
Paul.
Hello Paul, thank you :idea:

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

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

Post by ArpRokz »

I understand that 0 1 and 0 0 are possible outputs if a thread accesses the count variable before the other thread has completely updated its value but option 1 2 also seems to be a correct answer to me. count++ is not an atomic operation and may produce unexpected results but it can be a case where the second thread actually access count after it has been updated by the first thread then 1 2 is correct option.

I mean to say there is a chance of 0 0 or 0 1 since the operation ++ is not atomic but there can also be a case where everything goes as expected and outputs 1 2 . :?

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

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

Post by admin »

1 2 is not a possible output because the code is printing count++ (and not ++count). So if everything goes as expected the output will be 0 1.
If you like our products and services, please help us by posting your review here.

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

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

Post by ArpRokz »

Yes you are correct I didn't notice that. Thanks for the quick reply :)

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

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

Post by Bhaskar »

admin wrote:
Fri May 11, 2018 10:08 am
There is nothing wrong. 1 0 is just not one of the options. That doesn't mean it is not a valid output. The question is only asking you to select possible outputs out of the ones that it has given. It does not say that it has listed all possible outputs.

HTH,
Paul.
I am trying to understand why is 1 0 a valid output. Post-increment will first print the value of count and then increment it. So no matter which thread runs, 0 will always be printed first followed by further processing. Am i wrong in my understanding of post increment here?

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

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

Post by admin »

count++ and the print operation are not atomic. It is possible that one thread passes the initial value of 0 to println, increments count to 1 but before it is able to print the value 0, the second thread gets to run, passes the new value 1 to println, increments count to 2, prints the value 1. The first thread now gets to print the value that it was passed to print i.e. 0.
If you like our products and services, please help us by posting your review here.

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

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

Post by Bhaskar »

admin wrote:
Thu Dec 26, 2019 9:31 am
count++ and the print operation are not atomic. It is possible that one thread passes the initial value of 0 to println, increments count to 1 but before it is able to print the value 0, the second thread gets to run, passes the new value 1 to println, increments count to 2, prints the value 1. The first thread now gets to print the value that it was passed to print i.e. 0.
Thank you for the quick reply. Appreciate it.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 25 guests