1Z0-811 · Question #25
Given the code: public class Calc { // line n1 return a + b; } public static void main (String[] args) { Calc obj = new Calc(); int c = obj.sum(10, 20); System.out.println("sum is " + c); } } Which…
The correct answer is D. int sum(int[] a, b) {. There is an error in the stated correct answer. The actual correct answer is B, not D - and explaining otherwise would teach you wrong information for your exam. Here's the accurate breakdown: B (int sum(int a, int b)) is correct because it declares a method named sum with two…
Question
Options
- Aint sum(a, b) {
- Bint sum(int a, int b) {
- Cint sum(int, int) {
- Dint sum(int[] a, b) {
How the community answered
(16 responses)- A6% (1)
- B6% (1)
- D88% (14)
Explanation
There is an error in the stated correct answer. The actual correct answer is B, not D - and explaining otherwise would teach you wrong information for your exam.
Here's the accurate breakdown:
B (int sum(int a, int b)) is correct because it declares a method named sum with two typed int parameters and returns an int, which perfectly matches the call obj.sum(10, 20) and the expression return a + b.
Why the others are wrong:
- A -
int sum(a, b)is invalid Java; parameters must have explicit types. - C -
int sum(int, int)is invalid Java; parameters must also have names (unlike some languages). - D -
int sum(int[] a, b)has two problems:bhas no type (syntax error), andint[] aexpects an array, not the literal10passed in the call.
Memory tip: In Java, every method parameter requires both a type and a name - no exceptions, no shortcuts. Think of it as type name pairs, always in that order.
Double-check your source material - this question's answer key appears to have a typo.
Topics
Community Discussion
No community discussion yet for this question.