nerdexam
Oracle

1Z0-803 · Question #167

Given the code fragment: 1. class Test { 2. public static void main(String[] args) { 3. Test t = new Test(); 4. int[] arr = new int[10]; 5. arr = t.subArray(arr, 0, 2); 6. } 7. // insert code fragment

The correct answer is A. public int[] subArray(int[] src, int start, int end) { return src;. This question assesses the understanding of Java method signatures, specifically matching return types and parameter types with a method invocation.

Working with Methods and Encapsulation

Question

Given the code fragment: 1. class Test { 2. public static void main(String[] args) { 3. Test t = new Test(); 4. int[] arr = new int[10]; 5. arr = t.subArray(arr, 0, 2); 6. } 7. // insert code fragment here 8. } Which method definition can be inserted at line 7 to enable the code to compile?

Options

  • Apublic int[] subArray(int[] src, int start, int end) { return src;
  • Bpublic int subArray(int src, int start, int end) {
  • Cpublic int[] subArray(int src, int start, int end) {
  • Dpublic int subArray(int[] src, int start, int end) {

How the community answered

(47 responses)
  • A
    87% (41)
  • B
    2% (1)
  • C
    4% (2)
  • D
    6% (3)

Why each option

This question assesses the understanding of Java method signatures, specifically matching return types and parameter types with a method invocation.

Apublic int[] subArray(int[] src, int start, int end) { return src;Correct

The method call `t.subArray(arr, 0, 2)` expects a method named `subArray` that accepts an `int[]` as the first argument, an `int` as the second, an `int` as the third, and returns an `int[]` that can be assigned back to `arr`.

Bpublic int subArray(int src, int start, int end) {

This method signature's return type `int` does not match the `int[]` required for assignment to `arr`, and its first parameter `int src` does not match the `int[]` passed from `arr`.

Cpublic int[] subArray(int src, int start, int end) {

This method signature's first parameter `int src` does not match the `int[]` passed from `arr`, even though the return type matches.

Dpublic int subArray(int[] src, int start, int end) {

This method signature's return type `int` does not match the `int[]` required for assignment to `arr`, even though its first parameter matches.

Concept tested: Java method signature matching and overloading

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Topics

#method signature#method parameters#return type#arrays

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice