Java OCA OCP Practice Question 159

Question

Consider the following class:

 1.  class MyClass { 
 2.    void myMethod(int i) { 
 3.      System.out.println("int version"); 
 4.    } //from  w ww  .  j  a  va  2s.co m
 5.    void myMethod(String s) { 
 6.      System.out.println("String version"); 
 7.    } 
 8. 
 9.    public static void main(String args[]) { 
10.      MyClass crun = new MyClass(); 
11.      char ch = 'p'; 
12.      crun.myMethod(ch); 
13.    } 
14.  } 

Which of the following statements is true? (Choose one.)

  • A. Line 5 will not compile, because void methods cannot be overridden.
  • B. Line 12 will not compile, because no version of myMethod() takes a char argument.
  • C. The code will compile but will throw an exception at line 12.
  • D. The code will compile and produce the following output: int version.
  • E. The code will compile and produce the following output: String version.


D.

Note

At line 12, the char argument ch is widened to type int (a method-call conversion) and passed to the int version of method myMethod().




PreviousNext

Related