Java OCA OCP Practice Question 1836

Question

Given the following pairs of method declarations, which of the statements are true?

1.
void m (int time){  }
int  m (int time, int speed){ return time*speed ;}

2.
void m (int time){  }
int  m (int speed){return speed ;}

3.
void m (int time){  }
void M (int time){  }

Select 2 options//from  w ww .j a  v a2 s.com

A. The first pair of methods will compile correctly and overload the method 'm'.
B. The second pair of methods will compile correctly and overload the method 'm'.
C. The third pair of methods will compile correctly and overload the method 'm'.
D. The second pair of methods will not compile correctly.
E. The third pair of methods will not compile correctly.


Correct Options are  : A D

Note

Overloading of a method occurs when the name of more than one methods is exactly same but the parameter lists are different.

The first and the third pairs of methods will compile correctly as they follow the above stated rule.

The second pair of methods will not compile correctly, since their method signatures are same and the compiler cannot differentiate between the two methods as it does not look for return type.

Also, only name and input parameters are the part of method declaration.

Names of the parameters don't matter.

Both methods in the first pair are named m but have different parameter list so they overload this method name i.e. m.

The method named 'm' is distinct from the method named 'M', as identifiers in Java are case-sensitive.




PreviousNext

Related