Questions & Results
Q1 of 10Multiple ChoiceAccuracy45s · 1 pt
What happens with this code?
let r;
{
let x = 5;
r = &x;
}
println!("{}", r);
C Compile error: `r` must be initialized when declared D Compile error: `x` does not live long enough✓ 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` B It compiles: the result gets the `'static` lifetime C Compile error: missing lifetime specifier on the return type✓ 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 C Compile error: `y` does not live long enough✓ 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`✓ Correct C Compile error: `y` does not live long enough 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 B A group of objects that point to each other in a cycle but are unreachable from the roots✓ 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 D `C`, `D`, and `E`✓ 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✓ Correct C The amount of dead data 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✓ Correct C Mark-and-sweep can reclaim cycles and copying GC cannot 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✓ Correct C Old objects can't contain pointers to young objects D The old generation uses reference counting, so it never needs collecting 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✓ Correct B The array is never freed with `delete` C `index` can go negative, which corrupts the heap D The GC can't trace pointers that are stored inside arrays 120 / 120 answered · 98% correct
Score Distribution
Score (points) →