Java OCA OCP Practice Question 1161

Question

Given:

class MyClass{ //from   ww  w  .  j  a va2  s .co  m
     
    void method1 (int x){ 
        System.out.println ("method1 int"); 
     } 
     
    void method1 (double x){ 
        System.out.println ("method1 double"); 
     } 
     
    void method1 (String x){ 
        System.out.println ("method1 String"); 
     } 
     
} 

public class Main  { 
    public static void main (String [] args) throws Exception  { 
        MyClass ot = new MyClass (); 
        ot.method1 (1.0); 
     } 
} 

What will be the output?

Select 1 option

  • A. It will fail to compile.
  • B. method1 int
  • C. method1 double
  • D. method1 String


Correct Option is  : C

Note

method1 () is overloading for three different argument types.

So when you call ot.method1 (1.0), the one with argument of type double will be invoked.




PreviousNext

Related