nerdexam
Oracle

1Z0-808 · Question #76

What is the proper way to defined a method that take two int values and returns their sum as an int value?

The correct answer is D. int sum(int first, int second) { return first + second; }. Option D is correct because a valid method definition requires three things: a return type (int), a method name (sum), parameters with explicit types for each (int first, int second), and a return statement matching the declared return type. A is missing the return keyword…

Working with Methods and Encapsulation

Question

What is the proper way to defined a method that take two int values and returns their sum as an int value?

Options

  • Aint sum(int first, int second) { first + second; }
  • Bint sum(int first, second) { return first + second; }
  • Csum(int first, int second) { return first + second; }
  • Dint sum(int first, int second) { return first + second; }
  • Evoid sum (int first, int second) { return first + second; }

How the community answered

(25 responses)
  • A
    4% (1)
  • D
    88% (22)
  • E
    8% (2)

Explanation

Option D is correct because a valid method definition requires three things: a return type (int), a method name (sum), parameters with explicit types for each (int first, int second), and a return statement matching the declared return type.

  • A is missing the return keyword - first + second is just an expression that gets discarded, so nothing is actually returned.
  • B omits the type for second - every parameter must declare its own type independently; you can't share a type across parameters like int first, second.
  • C is missing the return type before the method name - Java requires an explicit return type (or void) on every method signature.
  • E uses void as the return type but then tries to return a value - void means the method returns nothing, making return first + second a compile error.

Memory tip: Think of the acronym RNPP - Return type, Name, Parameters (each typed), Produce a return value. If any of those four elements is broken or missing, the method won't compile.

Topics

#method declaration#return types#method parameters#method syntax

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice