Java OCA OCP Practice Question 395

Question

Consider the following class...

class Main{ /*  w ww . ja va2s . com*/
    void probe (Integer x)  { System.out.println ("In Integer");  } //2 
     
    void probe (Object x)  { System.out.println ("In Object");  } //3  
     
    void probe (Long x)  { System.out.println ("In Long");  } //4 
     
    public static void main (String [] args){ 
        String a = "hello";  
        new Main ().probe (a);  
     } 
} 

What will be printed?

Select 1 option

  • A. In Integer
  • B. In Object
  • C. In Long
  • D. It will not compile


Correct Option is  : B

Note

Ee have three overloaded probe() methods.

There is no probe method that takes a String parameter.

The only one that is able to accept a String is the one that takes Object as a parameter.

So that method will be called.

A String cannot be assigned to a variable of class Integer or Long variable, but it can be assigned to a variable of class Object.




PreviousNext

Related