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.
Question
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)- A87% (41)
- B2% (1)
- C4% (2)
- D6% (3)
Why each option
This question assesses the understanding of Java method signatures, specifically matching return types and parameter types with a method invocation.
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`.
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`.
This method signature's first parameter `int src` does not match the `int[]` passed from `arr`, even though the return type matches.
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
Community Discussion
No community discussion yet for this question.