OCA Java SE 8 Mock Exam Review - OCA Mock Question 15


Given the following definition of the class Course,

package com.java2s.courses; 
class Course { 
    public String courseName; 

What's the output of the following code?

package com.java2s; 
import com.java2s.courses.Course; 
class Main { 
    public static void main(String args[]) { 
        Course c = new Course(); 
        c.courseName = "Java"; 
  1. The class Main will print Java.
  2. The class Main will print null.
  3. The class Main will not compile.
  4. The class Main will throw an exception at runtime.




The class will fail to compile because a non-public class cannot be accessed outside a package in which it is defined.

The class Course therefore can't be accessed from within the class Main, even if it is explicitly imported into it.

If the class itself isn't accessible, there's no point in accessing a public member of a class.