Java OCA OCP Practice Question 2361

Question

Examine the following code:

class MyClass { /*from w  w  w  . j  a  v  a 2 s  . com*/
    String myName; 
} 
class Main { 
    public static void main(String args[]) { 
        MyClass c = new MyClass(); 
        c.myName = "Java"; 
        System.out.println(c.myName); 
    } 
} 

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

  • 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

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

An attempt to do so will cause it to fail at compile time.

Because the code won't compile, it can't execute.




PreviousNext

Related