Java OCA OCP Practice Question 2987

Question

Consider the following program and choose the right option from the given list:

class Base {/*from  ww w.  jav a2 s .c  om*/
    public void test() {
        protected int a = 10;   // #1
    }
}

class Main extends Base {              // #2
    public static void main(String[] args) {
        System.out.printf(null);       // #3
    }
}
  • a) the compiler will report an error at statement marked with the comment #1
  • b) the compiler will report an error at statement marked with the comment #2
  • c) the compiler will report errors at statement marked with the comment #3
  • d) the program will compile without any error


a)

Note

Statement #1 will result in a compiler error since the keyword protected is not allowed inside a method body.

You cannot provide access specifiers (public, protected, or private) inside a method body.

option b) It is acceptable to extend a base class and hence there is no compiler error in line marked with comment #2.

option c) It is acceptable to pass null to printf function hence there is no compiler error in line marked with comment #2.

option d) this program will not compile cleanly and hence this option is wrong.




PreviousNext

Related