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








Question

Examine the following code:

class Course { 
    String courseName; 
} 
class Main { 
    public static void main(String args[]) { 
        Course c = new Course(); 
        c.courseName = "Java"; 
        System.out.println(c.courseName); 
    } 
} 

Which of the following statements will be true if the variable courseName is defined as a private variable?

  1. class Main will print Java.
  2. class Main will print null.
  3. class Main won't compile.
  4. class Main will throw an exception at runtime.




Answer



C

Note

If the variable courseName is defined as a private member, it won't be accessible from the class Main.