nerdexam
Oracle

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.

Working with Methods and Encapsulation

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)
  • A
    88% (30)
  • B
    3% (1)
  • C
    9% (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.

Aomission of the return statementCorrect

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.

Breturn null;

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.

Creturn 0;

This is incorrect because `void` methods cannot return any value, and attempting to return an integer literal like `0` will cause a compilation error.

Dreturn;Correct

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

#void methods#return statement#method signature

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice