Report the wrong answer of a question (Q2 Objective-wise Tests - instanceof operator )

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

Moderator: admin

Post Reply
hienng
Posts: 2
Joined: Tue Jan 02, 2024 6:56 am
Contact:

Report the wrong answer of a question (Q2 Objective-wise Tests - instanceof operator )

Post by hienng »

Given:

Code: Select all

public class TestClass
{
  public static void main(String args[])
  {
    B b = new C();
    A a = b;

    INSERT CODE HERE

  }
}
class A {
    void a(){ System.out.println("a"); }
}
class B extends A {
    void b(){ System.out.println("b"); }
}
class C extends B {
    void c(){ System.out.println("c"); }
}
[code]

What can be inserted in the above code?

The option:[b] if (a instanceof A a1) a1.a();[/b] will compile and run as expected.
Last edited by admin on Tue Jan 02, 2024 8:18 am, edited 1 time in total.
Reason: Please put code inside [code] [/code]

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

Re: Report the wrong answer of a question (Q2 Objective-wise Tests - instanceof operator )

Post by admin »

1. Please quote the question id so that the same question can be opened. I think you are talking about enthuware.ocpjp.v17.2.3721
Question number in a test may vary.

2. Did you actually try compiling the given code and the option (using Java 17)?
If you like our products and services, please help us by posting your review here.

hienng
Posts: 2
Joined: Tue Jan 02, 2024 6:56 am
Contact:

Re: Report the wrong answer of a question (Q2 Objective-wise Tests - instanceof operator )

Post by hienng »

QID is 2.3721
I tried with jdk-17.0.3.1 => Compiled and run OK
I have just tried with jdk-17.0.6+10 => Failed to compile

Could you please double-check? Should we take notice of this question?
I lost a 50% score because of this question in the mock test. :lol:


Thanks you.

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

Re: Report the wrong answer of a question (Q2 Objective-wise Tests - instanceof operator )

Post by admin »

JLS 17 in section 15.20 page 662 mentions this restriction:
If the type of the RelationalExpression is a subtype of the type of the Pattern,
then a compile-time error occurs.
But JLS 21 does not have this restriction. That is why it compiles on Java 21.

Since the 1z0-829 exam is for Java 17, you need to accept what JLS 17 says.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 222 guests