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…
Question
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)- A4% (1)
- D88% (22)
- E8% (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
returnkeyword -first + secondis 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 likeint 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
voidas the return type but then tries toreturna value -voidmeans the method returns nothing, makingreturn first + seconda 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
Community Discussion
No community discussion yet for this question.