← 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);
}
}
- A.2 1
- B.1 2
- C.2 2
- 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 →