About Question enthuware.ocpjp.v11.2.3396 :

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

Moderator: admin

Post Reply
alfredo.zuloaga
Posts: 10
Joined: Fri Nov 18, 2016 5:48 pm
Contact:

About Question enthuware.ocpjp.v11.2.3396 :

Post by alfredo.zuloaga »

I have a question., Does TYPE_USE can be applied like
var str = (@NonNull String) "";
and also like
@NonNull String str = "";

?

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

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by admin »

>var str = (@NonNull String) "";
No.
>@NonNull String str = "";
Yes.

But I suggest you to actually try it out by writing a simple test program.
If you like our products and services, please help us by posting your review here.

61d14837
Posts: 13
Joined: Tue Jul 21, 2020 1:39 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by 61d14837 »

Option 6, explanation says that this is valid
Function<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val);
but that doesn't work unless @Target is updated to include ElementType.PARAMETER. This question instead uses ElementType.TYPE_PARAMETER which can be used on parameterized types, generic declarations

In response to the original poster, these both compile

Code: Select all

var str = (@NonNull String) "";
@NonNull String str2 = "";

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by hsnclk »

61d14837 wrote:
Sun Apr 18, 2021 2:42 am
Option 6, explanation says that this is valid
Function<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val);
but that doesn't work unless @Target is updated to include ElementType.PARAMETER. This question instead uses ElementType.TYPE_PARAMETER which can be used on parameterized types, generic declarations

In response to the original poster, these both compile

Code: Select all

var str = (@NonNull String) "";
@NonNull String str2 = "";
I aggre with "61d14837", the question requires to be updated. ElementType.TYPE_PARAMETER has to be changed with ElementType.PARAMETER. I mean, in order for the explanation in part "E" to be fulfilled, this must be done.

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

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by admin »

Not sure why you think so because both the examples given in explanation of option 6 compile fine (without any change in the problem statement):
Function<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val); //compiles fine
//or
Function<Integer, String> f = ( @NonNull Integer val ) -> Integer.toHexString(val); //also compiles fine
If you like our products and services, please help us by posting your review here.

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by hsnclk »

Actually, I had tried to create the @NonNull annotation myself, I had not used Spring one.

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

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by admin »

You may put the annotation code given in the problem statement in a java file and then use it in another java class. It compiles fine.
It has nothing to do with Spring.
If you like our products and services, please help us by posting your review here.

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by hsnclk »

Screenshot 2024-01-10 at 20.03.21.png
Screenshot 2024-01-10 at 20.03.21.png (148.72 KiB) Viewed 2331 times
Screenshot 2024-01-10 at 20.02.41.png
Screenshot 2024-01-10 at 20.02.41.png (149.83 KiB) Viewed 2331 times
Either I misunderstood something, or we are talking about different things. In the second image I added also ElementType.PARAMETER.

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

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by admin »

I tested the following code (as given in the problem statement and the explanation):

Code: Select all

import static java.lang.annotation.ElementType.*;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.util.function.*;

@Retention(value=RUNTIME)
@Target(value={TYPE_USE,TYPE_PARAMETER})
@interface NonNull{    
}
public class TestClass {
    public static void main(String[] args) {

        Function<Integer, String> f2 = ( @NonNull var val ) -> Integer.toHexString(val);
         //or 
        Function<Integer, String> f3 = ( @NonNull Integer val ) -> Integer.toHexString(val);
        System.out.println(f2.apply(10));
    }
}

It compiles and runs fine.
If you like our products and services, please help us by posting your review here.

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by hsnclk »

I don't understand my problem.
Screenshot 2024-01-13 at 20.33.22.png
Screenshot 2024-01-13 at 20.33.22.png (149.84 KiB) Viewed 2093 times
Screenshot 2024-01-13 at 20.33.39.png
Screenshot 2024-01-13 at 20.33.39.png (252.88 KiB) Viewed 2093 times
I copied and pasted your code.. The result was like this.

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

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by admin »

What is your JDK version?
Try compiling it from command line and post the error message.
If you like our products and services, please help us by posting your review here.

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by hsnclk »

openjdk-17 version17.0.1

by the way it compiled. but still looks like an error. I think it is an IntelliJ idea problem. thanks

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

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by admin »

Well, let's put it in the list of reasons to avoid IDEs while preparing for the certification exam :D
If you like our products and services, please help us by posting your review here.

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3396 :

Post by hsnclk »

You're right...

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests