A swap method that does not swap
Predict the outputMediumJava only
class Box {
int value;
Box(int value) {
this.value = value;
}
}
public class Main {
static void swap(Box p, Box q) {
Box t = p;
p = q;
q = t;
}
static void swapValues(Box p, Box q) {
int t = p.value;
p.value = q.value;
q.value = t;
}
public static void main(String[] args) {
Box a = new Box(1);
Box b = new Box(2);
swap(a, b);
System.out.println(a.value + " " + b.value);
swapValues(a, b);
System.out.println(a.value + " " + b.value);
}
}
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.