Java OCA OCP Practice Question 2363

Question

Given the following definition of the class Course,

package com.mypkg.courses; 
class Course { 
    public String courseName; 
} 

what's the output of the following code?

package com.mypkg; 
import com.mypkg.courses.Course; 
class Main { 
    public static void main(String args[]) { 
        Course c = new Course(); 
        c.courseName = "Java"; 
        System.out.println(c.courseName); 
    } 
} 
  • a The class Main will print Java.
  • b The class Main will print null.
  • c The class Main won't compile.
  • d The class Main will throw an exception at runtime.


c

Note

The class will fail to compile because a nonpublic class can't be accessed outside a package in which it's defined.

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

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




PreviousNext

Related