OCA Java SE 8 Method - OCA Mock Question Method 19








Question

Which of the following methods compile? (Choose all that apply)

  1. public void methodA() { return;}
  2. public void methodB() { return null;}
  3. public void methodD() {}
  4. public int methodD() { return 9;}
  5. public int methodE() { return 9.0;}
  6. public int methodF() { return;}
  7. public int methodG() { return null;}




Answer



A, C, D.

Note

A and C are correct because a void method is allowed to have a return statement as long as it doesn't return a value.

B and G do not compile because null requires a reference object as the return type.

Option D is correct because it returns an int value.

Option E does not compile because it tries to return a double when the return type is int.

Option F does not compile because no value is actually returned.