OCA Java SE 8 Mock Exam - OCA Mock Question 30








Question

Shape and Main are defined in the same package, examine the code and select the correct statements.

         line1>    class Shape { 
         line2>        public void print() { 
         line3>            System.out.println("Shape"); 
         line4>        } 
         line5>    } 

         line6>    class Main { 
         line7>        public static void main(String... args) { 
         line8>            Shape anShape = new Shape(); 
         line9>            anShape.print(); 
         line10>        } 
         line11>    } 
a The class Main prints Shape. 
                
b If the code on line 2 is changed as follows, the class Main will print Shape: 

private void print() { 

c If the code on line 2 is changed as follows, the class Main will print Shape: 

void print() { 

d If the code on line 2 is changed as follows, the class Main will print Shape: 

default void print() { 




Answer



a, c

Note

A is correct. The code will compile successfully and print Shape.

B is incorrect. The code won't compile if the access modifier of the method print is changed to private. private members of a class can't be accessed outside the class.

C is correct. The classes Shape and Main are defined in the same package, so changing the access modifier of the method print to default access will make it accessible in the class Main. The class will compile successfully and print Shape.

D is incorrect. "default" isn't a valid access modifier or keyword in Java. This code will fail to compile.