🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Methods: all 10 questions

Quiz: Java Methods · Question 3 of 10

What does this print? (code)

What does this print?

public class Main {
    static void fill(int[] a) { a[0] = 42; }
    public static void main(String[] args) {
        int[] arr = new int[2];
        fill(arr);
        System.out.println(arr[0]);
    }
}
  1. A.0
  2. B.42
  3. C.null
  4. D.Error
Answer & explanation

Correct answer: 42

The reference is copied, but both copies point to the same array, so its contents change.

Can you get the rest right without peeking?

Take the timed Quiz: Java Methods quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →