Java OCA OCP Practice Question 980

Question

Which code can be inserted to have the code print 2?

public class Main { 
  private int v; 
  boolean call; //from   www .  ja  v  a  2  s.  com

  public Main() { 
    // LINE 1 
    call = false; 
    // LINE 2 
  } 
  public Main(int v) { 
    this.v = v; 
  } 
  public static void main(String[] args) { 
    Main seed = new Main(); 
    System.out.println(seed.v); 
  } 
} 
  • A. Replace line 1 with Main(2);
  • B. Replace line 2 with Main(2);
  • C. Replace line 1 with new Main(2);
  • D. Replace line 2 with new Main(2);
  • E. Replace line 1 with this(2);
  • F. Replace line 2 with this(2);


E.

Note

Options A and B will not compile because constructors cannot be called without new.

Options C and D will compile but will create a new object rather than setting the fields in this one.

Option F will not compile because this() must be the first line of a constructor.

Option E is correct.




PreviousNext

Related