Which change will enable the code to compile?

Given:
package p1;
public interface DoInterface {
void method1(int n1); // line n1
}
package p3;
import p1.DoInterface;
public class DoClass implements DoInterface {
public DoClass(int p1) { }
public void method1(int p1) { } // line n2
private void method2(int p1) { } // line n3
}
public class Test {
public static void main(String[] args) {
DoInterface doi= new DoClass(100); // line n4
doi.method1(100);
doi.method2(100);
}
}
Which change will enable the code to compile?
A. Adding the public modifier to the declaration of method1 at line n1
B. Removing the public modifier from the definition of method1 at line n2
C. Changing the private modifier on the declaration of method 2 public at line n3
D. Changing the line n4 DoClass doi = new DoClass ( );

Download Printable PDF. VALID exam to help you PASS.

One thought on “Which change will enable the code to compile?

  1. I think the answer should be D.

    Making the correction in Answer C still yields the following compilation error:
    The method method2(int) is undefined for the type DoInterface

    Even though method2() is created as part of a DoClass object, it is not visible when referenced using DoInterface types.

    It is possible to to something like this:
    ((DoClass)doi).method2(100);
    but that is not an option for this question.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.