Repository navigation
Expand file tree
/
Copy pathQ1_References.java
More file actions
61 lines (47 loc) · 1.71 KB
/
Copy pathQ1_References.java
File metadata and controls
61 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Arrays;
public class Q1_References {
// ── PREDICT: What does main() print? Write your answers, then compile to check. ──
static void addOne(int x) { x += 1; }
static void addOne(int[] arr) { arr[0] += 1; }
static void replace(int[] arr) { arr = new int[]{99}; }
static void upperCase(String s) { s = s.toUpperCase(); }
public static void main(String[] args) {
int a = 5;
addOne(a);
System.out.println("1: " + a);
int[] b = {5};
addOne(b);
System.out.println("2: " + b[0]);
replace(b);
System.out.println("3: " + b[0]);
String s = "hello";
upperCase(s);
System.out.println("4: " + s);
int[] c = {1, 2, 3};
int[] d = c;
d[1] = 99;
System.out.println("5: " + Arrays.toString(c));
// ── WRITE ──
// Implement swap() so the two assertions pass.
// You may NOT change main or the method signature.
// Hint: think about what Java actually passes.
int[] pair = {10, 20};
swap(pair);
assert pair[0] == 20 && pair[1] == 10 : "swap failed";
System.out.println("swap ok");
// Now implement swapStrings() so that after calling it,
// arr[0] is arr[1]'s old value and vice versa.
String[] names = {"Falcon", "Kraken"};
swapStrings(names);
assert names[0].equals("Kraken") && names[1].equals("Falcon") : "swapStrings failed";
System.out.println("swapStrings ok");
}
// TODO: implement
static void swap(int[] pair) {
// your code here
}
// TODO: implement
static void swapStrings(String[] arr) {
// your code here
}
}