1Z0-803 · Question #150
You are writing a method that is declared not to return a value. Which two are permitted in the method body?
The correct answer is A. omission of the return statement D. return;. This question asks which statements are permissible within the body of a Java method that is declared with a void return type.
Question
You are writing a method that is declared not to return a value. Which two are permitted in the method body?
Options
- Aomission of the return statement
- Breturn null;
- Creturn 0;
- Dreturn;
How the community answered
(34 responses)- A88% (30)
- B3% (1)
- C9% (3)
Why each option
This question asks which statements are permissible within the body of a Java method that is declared with a `void` return type.
A `void` method can successfully complete its execution without explicitly using a `return` statement; control flow simply exits the method once the last statement is executed.
This is incorrect because `void` methods are specifically declared not to return any value, so attempting to return `null` (which is a value) will result in a compilation error.
This is incorrect because `void` methods cannot return any value, and attempting to return an integer literal like `0` will cause a compilation error.
A `void` method can use `return;` to explicitly terminate its execution at any point within the method body, without returning any value.
Concept tested: Void method return types
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
Topics
Community Discussion
No community discussion yet for this question.