Java OCA OCP Practice Question 949

Question

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

  • A. public void methodA() { return;}
  • B. public void methodB() { return null;}
  • C. public void methodD() {}
  • D. public int methodD() { return 9;}
  • E. public int methodE() { return 9.0;}
  • F. public int methodF() { return;}
  • G. public int methodG() { return null;}


A, C, D.

Note

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

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

void is not a reference object since it is a marker for no return type.

int is not a reference object since it is a primitive.

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.

Since a double cannot be assigned to an int, it cannot be returned as one either.

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




PreviousNext

Related