Java OCA OCP Practice Question 155

Question

Given:

class Main { /*from w  ww.  ja v  a2  s.  co m*/
  Main() { } 
  Main(Main m) { m1 = m; } 
  Main m1; 
  public static void main(String[] args) { 
    Main m2 = new Main(); 
    Main m3 = new Main(m2);  
    m3.go(); 
    Main m4 = m3.m1;          
    m4.go(); 
    Main m5 = m2.m1;          
    m5.go(); 
  } 
  void go() { 
    System.out.print("hi "); 
  } 
} 

What is the result?

  • A. hi
  • B. hi hi
  • C. hi hi hi
  • D. Compilation fails
  • E. hi, followed by an exception
  • F. hi hi, followed by an exception


F is correct.

Note

The m2 object's m1 instance variable is never initialized, so when m5 tries to use it a NullPointerException is thrown.




PreviousNext

Related