Page 1 of 1

About Question enthuware.ocpjp.v8.2.1868 :

Posted: Sun Dec 06, 2015 4:31 am
by ashish
Answer of below question is wrong.

What will the following code print when run?
Period p = Period.between(LocalDate.now(), LocalDate.of(2015, Month.SEPTEMBER, 1)); System.out.println(p);

Duration d = Duration.between(LocalDate.now(), LocalDate.of(2015, Month.SEPTEMBER, 1)); System.out.println(d);

Assume that the local time on the system at the time this code is run is 2nd Sep 2015 1:00 AM.

Options Given:
1 : P-1D
PT-14H-50M

2 : P1D
PT9H10M

3 : P-1D
P-9H-10M

4:P1D
PT14H50M

5 : It will throw an exception at run time.

Answer Given : Option 5.

But when you will run same code in java editor it will print below answer.

P-1D
It will throw an exception at run time. (Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds)

Means Option 5 is correct partially , Actual answer should be above (P-1D with exception) as you have give 2 system.out.println statement.

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

Posted: Sun Dec 06, 2015 4:45 am
by admin
The given answer is correct because you have to pick the best answer out of all the given options. Yes, it does print something but it does also throw an exception at run time and that is given in option 5. None of the other options is correct and that is why option 5 is the correct answer.

You will get similar situation in the real exam as well and you need to pick the best option.
HTH,
Paul.

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

Posted: Sun Dec 06, 2015 4:47 am
by ashish
Thanks Paul for quick support and cooperations

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

Posted: Wed Dec 09, 2015 4:27 am
by MichaelZett
This question is marked as easy, so perhaps it's okay but I'd like more options that actually could be the answer for the second statement. Something like: PT-25H.

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

Posted: Mon Jan 16, 2017 5:42 am
by dr_astico
This question is marked as easy...
even too easy tough.
none of the first four option has any acceptable value for the code given (if it would be somehow valid).

In this way, you can get the correct option even if you don't know about Temporal fields.

it feels like this question is more about logical thinking, not about Duration.betwen() method, isn't it?

Thanks in advance,
Marco

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

Posted: Thu Feb 21, 2019 11:12 am
by crazymind
The answer says throw java.time.temporal.UnsupportedTemporalTypeException which is wrong. This method only throws two kind of exceptions:
This code should throw DateTimeException since missing second component

DateTimeException - if the seconds between the temporals cannot be obtained
ArithmeticException - if the calculation exceeds the capacity of Duration
https://docs.oracle.com/javase/8/docs/a ... .Temporal-

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

Posted: Thu Feb 21, 2019 12:21 pm
by admin
Did you try running the code?

java.time.temporal.UnsupportedTemporalTypeException is a DateTimeException.

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

Posted: Thu Nov 04, 2021 12:58 pm
by Javatje
Why does this even compile?

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

Posted: Thu Nov 04, 2021 9:46 pm
by admin
why do you think it should not compile?