Java OCA OCP Practice Question 334

Question

Consider the following method...

public int setVar (int a, int b, float c)  {  ...} 

Which of the following methods correctly overload the above method?

Select 2 options

A. public int setVar (int a, float b, int c){ 
  return  (int)(a + b + c); 
} 
B. public int setVar (int a, float b, int c){ 
  return this (a, c, b); 
} 
C. public int setVar (int x, int y, float z){ 
  return x+y; /*from w  w w  .  j  a va2  s.  com*/
} 
D. public float setVar (int a, int b, float c){ 
  return c*a; 
} 
E. public float setVar (int a){ 
  return a; 
} 


Correct Options are  : A E

Note

Option B is not valid because of the line: return this (a, c, b);

This is the syntax of calling a constructor and not a method.

It should have been: return this.setVar(a,c,b);

In Optional B, this( ... ) can only be called in a constructor as the first statement.

For Optional C, it will not compile because it is same as the original method.

The name of parameters do not matter.

In Optional D, it will not compile as it is same as the original method.

The return type does not matter.

A method is said to be overloaded when the other method's name is same and parameters, either the number or their order, are different.




PreviousNext

Related