Questions & Results
Q1 of 10Multiple ChoiceAccuracy45s · 1 pt
What happens with this code?
let s1 = String::from("hi");
let s2 = s1;
println!("{} {}", s1, s2);
B Prints `hi` followed by an empty string C Compile error: borrow of moved value `s1`✓ Correct D Runtime error: double free 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')`✓ Correct B Compile error: borrow of moved value `x` C `(1, 'a')` followed by `()` 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);
C Compile error: `a` was moved into `b` D Compile error: `a` is not `mut` 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);
}
C Compile error: `a` is not declared `mut` D Compile error: `s` is dropped when `shout` returns 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
}
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);
C Compile error: cannot borrow `s` as mutable more than once at a time✓ Correct D Compile error: `r1` and `r2` must be declared `mut` 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 B Prints `a a`, then `ab`✓ Correct C Compile error: cannot have two immutable references 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);
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);
C Compile error: `r` must be initialized when declared D Compile error: `x` does not live long enough✓ 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 C Compile error: `y` does not live long enough✓ Correct D Runtime panic: dangling reference 173 / 179 answered · 99% correct
Score Distribution
Score (points) →