Practice 08

Vectors and Slices

Read each program and pick an answer. Some programs do not compile, and that can be the right answer. Your choice is checked as soon as you pick it, and saved in this browser.Need a refresher? Read lesson 8.

Course

0 of 50 answered·0 correct

A List That Grows

1.

What does this print?

fn main() {
    let mut v: Vec<i32> = Vec::new();
    v.push(10);
    v.push(20);
    println!("{v:?}");
}
2.

What does this print?

fn main() {
    let mut v: Vec<i32> = Vec::new();
    v.push(1);
    v.push(2);
    v.push(3);
    println!("{}", v.len());
}
3.

What does this print?

fn main() {
    let v = vec![5, 10, 15];
    println!("{v:?}");
}
4.

What does this print?

fn main() {
    let v = vec![7; 3];
    println!("{v:?}");
}
5.

Does this compile?

fn main() {
    let v = Vec::new();
    println!("{v:?}");
}
6.

What does this print?

fn main() {
    let v: Vec<i32> = Vec::new();
    println!("{v:?}");
    println!("{}", v.is_empty());
}

A Vector in Memory

7.

The numbers may differ on your machine, so write what your reasoning says, not what you remember from the lesson.

fn main() {
    let mut v: Vec<i32> = Vec::new();
    v.push(1);
    println!("{}", v.capacity());
    v.push(2);
    v.push(3);
    v.push(4);
    println!("{}", v.capacity());
    v.push(5);
    println!("{}", v.capacity());
}
8.

vec! already knows how many items it needs room for.

fn main() {
    let v = vec![1, 2, 3];
    println!("{} {}", v.len(), v.capacity());
}
9.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    v.push(4);
    let cap_before = v.capacity();
    v.clear();
    println!("{} {}", v.len(), v.capacity());
    println!("{}", v.capacity() == cap_before);
}
10.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3, 4];
    v.remove(0);
    println!("{} {}", v.len(), v.capacity());
}
11.

What does this print?

fn main() {
    let mut v: Vec<i32> = Vec::new();
    for i in 1..=6 {
        v.push(i);
        println!("{} {}", v.len(), v.capacity());
    }
}

Reading and Changing Items

12.

What does this print?

fn main() {
    let v = vec![10, 20, 30, 40];
    println!("{}", v[0]);
    println!("{}", v[v.len() - 1]);
}
13.

What does this print?

fn main() {
    let v = vec![1, 2, 3];
    let i = v.len();
    println!("{}", v[i]);
}
14.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    v[1] = 99;
    println!("{v:?}");
}
15.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    v.insert(0, 100);
    println!("{v:?}");
}
16.

What does this print?

fn main() {
    let mut v = vec![5, 10, 15, 20];
    let taken = v.remove(1);
    println!("{taken} {v:?}");
}
17.

What does this print?

fn main() {
    let mut v = vec![4, 1, 3, 2];
    v.sort();
    println!("{v:?}");
    v.reverse();
    println!("{v:?}");
}
18.

What does this print?

fn main() {
    let v = vec![2, 4, 6, 8];
    println!("{}", v.contains(&6));
    println!("{}", v.contains(&5));
}
19.

Does this compile?

fn main() {
    let v = vec![2, 4, 6, 8];
    println!("{}", v.contains(6));
}
20.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    v.clear();
    println!("{} {}", v.len(), v.is_empty());
}

Stepping Through a List

21.

What does this print?

fn main() {
    let v = vec![3, 6, 9];
    for x in &v {
        println!("{x}");
    }
}
22.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    for x in &mut v {
        *x *= 10;
    }
    println!("{v:?}");
}
23.

Does this compile?

fn main() {
    let mut v = vec![1, 2, 3];
    for x in &mut v {
        x *= 10;
    }
    println!("{v:?}");
}
24.

What does this print?

fn main() {
    let v = vec!["a", "b", "c"];
    for (i, s) in v.iter().enumerate() {
        println!("{i}: {s}");
    }
}
25.

What does this print?

fn main() {
    let v = vec![4, 5, 6];
    let total: i32 = v.iter().sum();
    println!("{total}");
}
26.

What does this print?

fn main() {
    let v = vec![3, 8, 1, 9, 4];
    let mut smallest = v[0];
    for x in &v {
        if *x < smallest {
            smallest = *x;
        }
    }
    println!("{smallest}");
}
27.

Does this compile?

fn main() {
    let v = vec![1, 2, 3];
    for x in v {
        println!("{x}");
    }
    println!("{}", v.len());
}
28.

What does this print?

fn main() {
    let mut v = vec![10, 20, 30];

    for x in &mut v {
        *x += 1;
    }

    for (i, x) in v.iter().enumerate() {
        println!("{i}: {x}");
    }

    let total: i32 = v.iter().sum();
    println!("total {total}");
}

Ownership of a List

29.

Does this compile?

fn main() {
    let v = vec![1, 2, 3];
    let w = v;
    println!("{w:?}");
    println!("{v:?}");
}
30.

What does this print?

fn main() {
    let v = vec![1, 2, 3];
    let w = v.clone();
    println!("{w:?}");
    println!("{v:?}");
}
31.

What does this print?

fn main() {
    let v = vec![1, 2, 3];
    let mut w = v.clone();
    w.push(4);
    println!("{v:?}");
    println!("{w:?}");
}
32.

What does this print?

fn takes(v: Vec<i32>) {
    println!("{v:?}");
}

fn main() {
    let v = vec![1, 2, 3];
    takes(v);
    println!("done");
}
33.

Does this compile?

fn takes(v: Vec<i32>) {
    println!("{v:?}");
}

fn main() {
    let v = vec![1, 2, 3];
    takes(v);
    println!("{v:?}");
}
34.

Does this compile?

fn main() {
    let mut v = vec![1, 2, 3];
    let w = v;
    v = vec![9, 9];
    println!("{v:?}");
    println!("{w:?}");
}

Slices

35.

What does this print?

fn total(items: &[i32]) -> i32 {
    let mut sum = 0;
    for item in items {
        sum += item;
    }
    sum
}

fn main() {
    let a = [1, 2, 3];
    let v = vec![10, 20, 30, 40];
    println!("{}", total(&a));
    println!("{}", total(&v));
}
36.

What does this print?

fn main() {
    let v = vec![10, 20, 30, 40, 50];
    let part = &v[1..3];
    println!("{part:?}");
}
37.

What does this print?

fn first(items: &[i32]) -> i32 {
    items[0]
}

fn main() {
    let v = vec![7, 8, 9];
    println!("{}", first(&v[1..]));
}
38.

What does this print?

fn main() {
    let v = vec![1, 2, 3, 4, 5];
    let s = &v[..3];
    println!("{} {:?}", s.len(), s);
}
39.

Does this compile?

fn total(items: &Vec<i32>) -> i32 {
    let mut sum = 0;
    for item in items {
        sum += item;
    }
    sum
}

fn main() {
    let a = [1, 2, 3];
    println!("{}", total(&a));
}
40.

What does this print?

fn main() {
    let v = vec![1, 2, 3];
    let s = &v[..];
    println!("{s:?}");
}
41.

What does this print?

fn make_end(n: usize) -> usize {
    n + 10
}

fn main() {
    let v = vec![1, 2, 3];
    let end = make_end(0);
    let s = &v[0..end];
    println!("{s:?}");
}

A Reference Into a List That Grows

42.

Does this compile?

fn main() {
    let mut v = vec![1, 2, 3];
    let first = &v[0];
    v.push(4);
    println!("{first}");
}
43.

What does this print?

fn main() {
    let mut v = vec![1, 2, 3];
    let first = &v[0];
    println!("{first}");
    v.push(4);
    println!("{v:?}");
}
44.

Does this compile?

fn main() {
    let mut v = vec![1, 2, 3];
    let last = &v[v.len() - 1];
    v.insert(0, 99);
    println!("{last}");
}
45.

Does this compile?

fn main() {
    let mut v = vec![1, 2, 3];
    let first = v[0];
    v.push(4);
    println!("{first}");
    println!("{v:?}");
}

A List of Text

46.

What does this print?

fn main() {
    let mut names: Vec<String> = Vec::new();
    names.push(String::from("Ravi"));
    names.push("Meera".to_string());
    for name in &names {
        println!("{name} has {} letters", name.len());
    }
}
47.

What does this print?

fn main() {
    let line = "learning rust is fun";
    let words: Vec<&str> = line.split_whitespace().collect();
    println!("{words:?}");
    println!("{}", words.len());
}
48.

What does this print?

fn main() {
    let line = "one two three";
    let words: Vec<&str> = line.split_whitespace().collect();
    println!("{}", words[0]);
    println!("{}", words[words.len() - 1]);
}
49.

What does this print?

fn pick(n: usize) -> usize {
    n + 5
}

fn main() {
    let line = "red green";
    let words: Vec<&str> = line.split_whitespace().collect();
    let i = pick(0);
    println!("{}", words[i]);
}
50.

What does this print?

fn main() {
    let names = vec![
        String::from("Ann"),
        String::from("Bartholomew"),
        String::from("Cy"),
    ];

    let mut total = 0;
    let mut longest = &names[0];

    for name in &names {
        total += name.len();
        if name.len() > longest.len() {
            longest = name;
        }
    }

    println!("total letters {total}");
    println!("longest {longest}");
}