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

Quiz: Java Interview Questions · Question 9 of 10

What does this print? (code)

What does this print?

public class Main {
    static void swap(Integer a, Integer b) {
        Integer t = a;
        a = b;
        b = t;
    }
    public static void main(String[] args) {
        Integer x = 1, y = 2;
        swap(x, y);
        System.out.println(x + " " + y);
    }
}
  1. A.2 1
  2. B.1 2
  3. C.2 2
  4. D.Compile error
Answer & explanation

Correct answer: 1 2

Java passes copies of the references. Swapping the copies inside the method does not change x and y in main.

Can you get the rest right without peeking?

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

Start the quiz →