Java OCA OCP Practice Question 1377

Question

Given the code below,

what data types can be inserted into the blank that would allow the code to print 3?

Choose three.

public class Main { 
   public int m(long car) { return 2; } 
   public int m(double car) { return 3; } 
   public int m(int car) { return 5; } 
   public int m(short car) { return 3; } 
   public static void main(String[] argv) { 
      ___ value = 5; //from  w  w w  .j a v  a  2s . c o  m
      System.out.print(new Main().m(value)); 
   } 
} 
  • A. boolean
  • B. short
  • C. int
  • D. byte
  • E. long
  • F. float


B, D, F.

Note

The compiler will broaden the data type on a numeric value until it finds a compatible signature.

Option A is incorrect because boolean cannot be converted to either of these types.

Option B is correct because the data type short matches our message signature.

Options C and E are incorrect.

int and long are larger than short and will trigger different overloaded versions of m() to be called, one that returns 5 and one that returns 2, respectively.

Option D is correct.

The byte value can be implicitly converted to short, and there are no other matching method signatures that take a byte value.

Option F is correct because float can be implicitly converted to double, and there is no other version of m() that takes a float value.




PreviousNext

Related