Java OCA OCP Practice Question 1433

Question

Which of the following implementations of a max () method will correctly return the largest value?

Select 1 option

A.  int max (int x, int y){ 
        return (  if (x > y){ x;  } else{ y;  }  ); 
    } /*ww  w  .j av a2 s.c om*/

B.  int max (int x, int y){ 
        return ( if (x > y){ return x;  }  else{ return y;  } ); 
    } 

C.  int max (int x, int y){ 
     switch (x < y){ 
        case true: 
               return y; 
        default  : 
               return x; 
      }; 
    } 
    
D. int max (int x, int y){ 

      if  (x > y)  return x; 
      return y; 
   } 

E. None of the above. 


Correct Option is  : D

Note

For Option A.

The if statement does not return any value so it can not be used the way it is used in ( 1).

For Option B.

It would work if the first return and the corresponding brackets is removed.

For Option C.

Neither the switch expression nor the case labels can be of type boolean.




PreviousNext

Related