Java OCA OCP Practice Question 1176

Question

Given the class below,

which method signature could be successfully added to the class as an overloaded version of the m() method?

public class Main { 
       public Integer m(int sum) { return sum; } 
} 
  • A. public Long m(int sum)
  • B. public Long m(int sum, int divisor)
  • C. public Integer average(int sum)
  • D. private void m(int sum)


B.

Note

Options A and D would not allow the class to compile because two methods in the class cannot have the same name and arguments, but a different return value.

Option C would allow the class to compile, but it is not a valid overloaded form of our m() method since it uses a different method name.

Option B is a valid overloaded version of the m() method, since the name is the same but the argument list differs.




PreviousNext

Related