nerdexam
Oracle

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…

Object-Oriented Programming Principles

Question

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 code fragment, when inserted at line n1, enables the code to print sum is 30?

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)
  • A
    6% (1)
  • B
    6% (1)
  • D
    88% (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: b has no type (syntax error), and int[] a expects an array, not the literal 10 passed 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

#method signature#parameter declaration#syntax rules#method calls

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice