CMSC330_11/17

CMSC 330 — Fall 2026 · November 17, 2026 · 179 players · Max 10 pts

Questions & Results

Q1 of 10Multiple ChoiceAccuracy45s · 1 pt

What happens with this code?

let s1 = String::from("hi");
let s2 = s1;
println!("{} {}", s1, s2);
A Prints `hi hi`
15
B Prints `hi` followed by an empty string
3
C Compile error: borrow of moved value `s1`
148
✓ Correct
D Runtime error: double free
2
168 / 179 answered · 88% correct
Q2 of 10Multiple ChoiceAccuracy45s · 1 pt

What is printed?

let x = (1, 'a');
let y = x;
println!("{:?} {:?}", x, y);
A `(1, 'a') (1, 'a')`
155
✓ Correct
B Compile error: borrow of moved value `x`
15
C `(1, 'a')` followed by `()`
5
D Compile error: tuples cannot be printed
175 / 179 answered · 89% correct
Q3 of 10Multiple ChoiceAccuracy45s · 1 pt

What is printed?

let a = String::from("cat");
let mut b = a.clone();
b.push_str("s");
println!("{} {}", a, b);
A `cats cats`
5
B `cat cats`
167
✓ Correct
C Compile error: `a` was moved into `b`
3
D Compile error: `a` is not `mut`
2
177 / 179 answered · 94% correct
Q4 of 10Multiple ChoiceAccuracy60s · 1 pt

What is printed?

fn shout(mut s: String) -> String {
  s.push_str("!");
  s
}
fn main() {
  let a = String::from("hi");
  let b = shout(a);
  println!("{}", b);
}
A `hi`
3
B `hi!`
113
✓ Correct
C Compile error: `a` is not declared `mut`
61
D Compile error: `s` is dropped when `shout` returns
1
178 / 179 answered · 63% correct
Q5 of 10Multiple ChoiceAccuracy60s · 1 pt

Who owns the heap data originally passed in as s at HERE?

fn f(s: String) {
  let a = s;
  let b = &a;
  let c = a.clone();
  let d = b;
  // HERE
}
A `s`
6
B `a`
165
✓ Correct
C `c`
D `d`
3
174 / 179 answered · 95% correct
Q6 of 10Multiple ChoiceAccuracy60s · 1 pt

What happens with this code?

let mut s = String::from("a");
let r1 = &mut s;
let r2 = &mut s;
r1.push_str("b");
r2.push_str("c");
println!("{}", s);
A Prints `abc`
1
B Prints `ac`
1
C Compile error: cannot borrow `s` as mutable more than once at a time
172
✓ Correct
D Compile error: `r1` and `r2` must be declared `mut`
1
175 / 179 answered · 98% correct
Q7 of 10Multiple ChoiceAccuracy60s · 1 pt

What happens with this code?

let mut s = String::from("a");
let r1 = &s;
let r2 = &s;
println!("{} {}", r1, r2);
let r3 = &mut s;
r3.push_str("b");
println!("{}", r3);
A Compile error: cannot borrow `s` as mutable because it is also borrowed as immutable
3
B Prints `a a`, then `ab`
169
✓ Correct
C Compile error: cannot have two immutable references
2
D Prints `a a`, then `a`
174 / 179 answered · 97% correct
Q8 of 10Multiple ChoiceAccuracy60s · 1 pt

What is printed?

let mut x = 1;
let mut y = 2;
let mut r = &mut x;
*r = 3;
r = &mut y;
*r += x;
println!("{} {}", x, y);
A `1 3`
3
B `3 2`
3
C `3 5`
171
✓ Correct
D Compile error
177 / 179 answered · 97% correct
Q9 of 10Multiple ChoiceAccuracy45s · 1 pt

What happens with this code?

let r;
{
  let x = 5;
  r = &x;
}
println!("{}", r);
A Prints `5`
B Prints a garbage value
C Compile error: `r` must be initialized when declared
1
D Compile error: `x` does not live long enough
169
✓ Correct
170 / 179 answered · 99% correct
Q10 of 10Multiple ChoiceAccuracy60s · 1 pt

Given

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
  if x.len() > y.len() { x } else { y }
}

what happens with this code?

let x = String::from("hello");
let z;
{
  let y = String::from("hi");
  z = longest(&x, &y);
}
println!("{}", z);
A Prints `hello`, because `z` refers to `x`, which is still alive
B Prints `hi`
1
C Compile error: `y` does not live long enough
171
✓ Correct
D Runtime panic: dangling reference
1
173 / 179 answered · 99% correct

Score Distribution

2
1
5
8
21
79
63
0
1
2
3
4
5
6
7
8
9
10

Score (points) →

Players
179
Average Score
8.9
High Score
10
Max Possible
10