CMSC330_11/24

CMSC 330 — Fall 2026 · November 24, 2026 · 120 players · Max 10 pts

Questions & Results

Q1 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
114
✓ Correct
115 / 120 answered · 99% correct
Q2 of 10Multiple ChoiceAccuracy45s · 1 pt

What happens when you compile this function?

fn longest(x: &str, y: &str) -> &str {
  if x.len() > y.len() { x } else { y }
}
A It compiles: the result gets the lifetime of `x`
1
B It compiles: the result gets the `'static` lifetime
2
C Compile error: missing lifetime specifier on the return type
115
✓ Correct
D Compile error: `&str` values can't be compared by length
118 / 120 answered · 97% correct
Q3 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
10
B Prints `hi`
C Compile error: `y` does not live long enough
37
✓ Correct
D Runtime panic: dangling reference
47 / 120 answered · 79% correct
Q4 of 10Multiple ChoiceAccuracy60s · 1 pt

Given

fn first<'a, 'b>(x: &'a str, _y: &'b str) -> &'a str {
  x
}

what happens with this code?

let x = String::from("hello");
let z;
{
  let y = String::from("hi");
  z = first(&x, &y);
}
println!("{}", z);
A Prints `hello`
115
✓ Correct
B Prints `hi`
1
C Compile error: `y` does not live long enough
3
D Compile error: `'b` is never used in the return type
119 / 120 answered · 97% correct
Q5 of 10Multiple ChoiceAccuracy45s · 1 pt

Which garbage can reference counting, on its own, NOT reclaim?

A An object pointed to only by a local variable that goes out of scope
11
B A group of objects that point to each other in a cycle but are unreachable from the roots
108
✓ Correct
C A very large object on the heap
D An object whose last reference is overwritten
119 / 120 answered · 91% correct
Q6 of 10Multiple ChoiceAccuracy60s · 1 pt

The heap has these pointers. The only root is a stack variable that points to A.

root -> A
A -> B
C -> D
D -> C
E -> A

Which objects does a mark-and-sweep collector reclaim?

A None, since every object has at least one pointer to it
1
B Only `E`
1
C `C` and `D` only
3
D `C`, `D`, and `E`
114
✓ Correct
119 / 120 answered · 96% correct
Q7 of 10Multiple ChoiceAccuracy45s · 1 pt

The work done by one copying (stop-and-copy) collection is proportional to what?

A The total size of the heap
B The amount of live (reachable) data
118
✓ Correct
C The amount of dead data
1
D The number of pointer writes since the last collection
119 / 120 answered · 99% correct
Q8 of 10Multiple ChoiceAccuracy60s · 1 pt

A conservative collector for C can't be sure which words are pointers. Why is it usually built on mark-and-sweep instead of copying?

A Mark-and-sweep uses half as much memory
B Mark-and-sweep never moves objects, so it never has to rewrite a word that might not really be a pointer
118
✓ Correct
C Mark-and-sweep can reclaim cycles and copying GC cannot
1
D Mark-and-sweep doesn't need to know the roots
119 / 120 answered · 99% correct
Q9 of 10Multiple ChoiceAccuracy45s · 1 pt

Why does a generational collector collect the young generation more often than the old generation?

A Young objects are smaller, so they are cheaper to copy
B Most objects die young, so collecting the young generation frees a lot of memory for little work
116
✓ Correct
C Old objects can't contain pointers to young objects
1
D The old generation uses reference counting, so it never needs collecting
2
119 / 120 answered · 97% correct
Q10 of 10Multiple ChoiceAccuracy60s · 1 pt

This Java stack leaks memory even though Java has a garbage collector. Why?

class Stack {
  private Object[] stack;
  private int index;
  public void push(Object o) {
    stack[index++] = o;
  }
  public Object pop() {
    return stack[--index];
  }
}
A `pop()` leaves a reference in the array, so the popped object stays reachable and the GC can't reclaim it
117
✓ Correct
B The array is never freed with `delete`
1
C `index` can go negative, which corrupts the heap
2
D The GC can't trace pointers that are stored inside arrays
120 / 120 answered · 98% correct

Score Distribution

1
5
18
69
27
0
1
2
3
4
5
6
7
8
9
10

Score (points) →

Players
120
Average Score
8.9
High Score
10
Max Possible
10