nerdexam
Oracle

1Z0-808 · Question #98

Given: ``java public class Test2 { public static void doChange(int[] arr) { for (int pos = 0; pos < arr.length; pos++){ arr[pos] = arr[pos] + 1; } } public static void main(String[] args) { int[]…

The correct answer is E. Compilation fails. Compilation fails because doChange(arr[0], arr[1], arr[2]) attempts to call a method that expects a single int[] argument with three separate int values - no such overload exists, so the compiler rejects the code before it ever runs. This eliminates A, B, C, and D entirely…

Working with Methods and Encapsulation

Question

Given:
public class Test2 {
 public static void doChange(int[] arr) {
 for (int pos = 0; pos < arr.length; pos++){
 arr[pos] = arr[pos] + 1;
 }
 }
 public static void main(String[] args) {
 int[] arr = {10, 20, 30};
 doChange(arr);
 for (int x : arr) {
 System.out.print(x + ", ");
 }
 
 doChange(arr[0], arr[1], arr[2]);
 System.out.print(arr[0] + ", " + arr[1] + ", " + arr[2]);
 }
}
What is the result?

Options

  • A11, 21, 31, 11, 21, 31
  • B11, 21, 31, 12, 22, 32
  • C12, 22, 32, 12, 22, 32
  • D10, 20, 30, 10, 20, 30
  • ECompilation fails

How the community answered

(30 responses)
  • A
    3% (1)
  • B
    13% (4)
  • C
    7% (2)
  • E
    77% (23)

Explanation

Compilation fails because doChange(arr[0], arr[1], arr[2]) attempts to call a method that expects a single int[] argument with three separate int values - no such overload exists, so the compiler rejects the code before it ever runs. This eliminates A, B, C, and D entirely: none of those outputs are reachable because the program never executes. A and D would require the second call to either leave values unchanged or increment them, B would require it to increment a second time, and C would require both calls to succeed - all impossible when compilation itself fails.

Memory tip: Individual array elements like arr[0] are just primitives (int), not arrays - passing them where an int[] is expected is a type mismatch the compiler catches immediately. When you see a method called with elements instead of the array itself, always check the method signature first.

Topics

#method signatures#array vs primitive types#parameter matching#compilation errors

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice