About Question enthuware.ocajp.i.v8.2.1434 :

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

Moderator: admin

Post Reply
Mushfiq Mammadov
Posts: 32
Joined: Fri Oct 16, 2015 4:33 am
Contact:

About Question enthuware.ocajp.i.v8.2.1434 :

Post by Mushfiq Mammadov »

Image
Option statement is clear, the code does still compile without changing LocalDateTime to ZonedDateTime. But I can't understand explanation maybe for my english. Is it regarding LocalDateTime parameter or ZonedDateTime? Because the code will work if we change LocalDateTime to ZonedDateTime:

Code: Select all

class TestZonedDateTime {
    
    public static void main(String[] args) {
        TestZonedDateTime t = new TestZonedDateTime();
        System.out.println(t.getDateString(ZonedDateTime.now()));
    }

    public String getDateString(ZonedDateTime ldt) {
        return DateTimeFormatter.ISO_ZONED_DATE_TIME.format(ldt);  // 2015-11-25T16:10:42.794+04:00[Asia/Baku]
    }
}

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

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by admin »

If you pass a LocalDateTime and try to format it with ISO_ZONED_DATE_TIME, it will throw an exception at run time. That is why the explanation says that the given code will not work at run time. Of course, the code can be fixed. But the explanation is talking about the code as presented in the problem statement.
If you like our products and services, please help us by posting your review here.

Mushfiq Mammadov
Posts: 32
Joined: Fri Oct 16, 2015 4:33 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by Mushfiq Mammadov »

admin wrote:If you pass a LocalDateTime and try to format it with ISO_ZONED_DATE_TIME, it will throw an exception at run time. That is why the explanation says that the given code will not work at run time.
This had already been written in the explanation of option 1 therefore I guessed the underlined part was regarding ZonedDateTime parameter.
Thanks for the reply

actuaris
Posts: 3
Joined: Mon Feb 27, 2017 4:52 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by actuaris »

Zoned date times are not part of the OCA exam. So I gambled for option 4. Since my exam is next week, do I have to look at things zoned dates as well?

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

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by admin »

No, you don't need to read any thing more about ZonedDateTime. Whatever information is given in this question's explanation is enough.
Just remember that LocalDateTime class does not contain Zone information but if you format a local date using ISO_ZONED_DATE_TIME formatter, then there will be an exception because this formatter requires zone information.
If you like our products and services, please help us by posting your review here.

zserrbo
Posts: 2
Joined: Tue Feb 28, 2017 2:33 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by zserrbo »

I think the fourth answer's explanation is not fully correct.

option four:
The method parameter type must be changed from LocalDateTime to ZonedDateTime for it to compile.
and its explanation:
Although it is true that this code will never work at runtime, it will compile fine as it is.

I understand that the method parameter changing from LocalDateTime to ZonedDateTime it's not neccessary and thus the option one is the answer. But the code will works at runtime if we change the method parameter type (and of course we should add to ZoneDateTime to method argument).

Am I correct?

nickimix
Posts: 1
Joined: Wed Aug 02, 2017 3:41 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by nickimix »

zserrbo wrote:I think the fourth answer's explanation is not fully correct.

option four:
The method parameter type must be changed from LocalDateTime to ZonedDateTime for it to compile.
and its explanation:
Although it is true that this code will never work at runtime, it will compile fine as it is.

I understand that the method parameter changing from LocalDateTime to ZonedDateTime it's not neccessary and thus the option one is the answer. But the code will works at runtime if we change the method parameter type (and of course we should add to ZoneDateTime to method argument).

Am I correct?
Agree with you. This variant and explanation is not clear.

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

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by admin »

I am not sure what is not clear. The option says that you must do X for it to compile.
The explanation says that it will compile without any change.
The explanation also says that the given code will not work at run time even though it will compile fine.

Now, you are saying that it will compile and run fine if the code is changed to something else. Yes, there could be multiple ways to make the code compile and run fine. But the explanation is talking about the code that is given.

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

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by flex567 »

In the book the only use of DateTimeFormatter class is like this:

Code: Select all

DateTimeFormatter.ofPattern()
The way you are using it is by calling the inner class (which should not be on the exam)

Code: Select all

DateTimeFormatter.ISO_ZONED_DATE_TIME.format(ldt);
Can you maybe explain a bit about the use of it, just in case we encounter question like that?

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by raj_dp »

The option number 3 seems to be correct because when we change from LocalDateTime to ZonedDateTime it compiles and runs properly.
ZonedDateTime zdt1 = ZonedDateTime.now();
System.out.println(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zdt1)); //output: 2018-12-12T21:51:59.931+05:30[Asia/Calcutta]

I dont understand why it has been said that it will never work at runtime.
Regards
Raj

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

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by admin »

raj_dp wrote:
Wed Dec 12, 2018 11:24 am
The option number 3 seems to be correct because when we change from LocalDateTime to ZonedDateTime it compiles and runs properly.
I dont see your code showing that it compiles and runs properly!!
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by admin »

Also, option 3 says the given code wont complie as it is. Did you try to compile it?
Please go through the discussion above.
If you like our products and services, please help us by posting your review here.

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by raj_dp »

I can see that the option 3 of Question number 63 of Test 6 say - "The method parameter type must be changed from LocalDateTime to ZonedDateTime for it to compile." So, I tried out the following:

import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;

public class Q_63 {

public static void main(String[] args) {
ZonedDateTime zdt1 = ZonedDateTime.now();
System.out.println(getDateString(zdt1));

}
public static String getDateString(ZonedDateTime zdt){
return DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zdt);
}
}
This compiles and runs fine and the output is: 2018-12-13T10:52:47.093+05:30[Asia/Calcutta]
Regards
Raj

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

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by admin »

Sorry, but I am really not sure what is the issue. To see whether the given option is correct or not you have to compile the code that is given. Does the given code compile? If yes, then the option is wrong because the option says the code must be changed to compile. But no change is required to make the code compile. The explanation also says the same thing...the given code will compile but it will throw an exception at run time. So, I don't know what you are trying to show by your example. The same thing is also explained in older posts. You need to go through the thread.
If you like our products and services, please help us by posting your review here.

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1434 :

Post by raj_dp »

Oh I am so sorry. I got it now. That option is wrong because even without changing the parameter from LocalDateTime to ZonedDateTime, the given program will compile.
Thanks for patiently explaining.
Regards
Raj

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests