Page 1 of 1

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

Posted: Tue Jan 02, 2024 7:00 am
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.

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

Posted: Tue Jan 02, 2024 8:15 am
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)?

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

Posted: Tue Jan 02, 2024 9:12 pm
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.

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

Posted: Wed Jan 03, 2024 12:24 am
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.