Practice 05

Stack, Heap and Memory

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 5.

Course

0 of 50 answered·0 correct

Sizes on the Stack

1.

What does this print?

fn main() {
    println!("{}", size_of::<i64>());
    println!("{}", size_of::<bool>());
}
2.

What does this print?

fn main() {
    println!("{}", size_of::<[bool; 50]>());
}
3.

What does this print? The three fields of a String sit on the stack even when the text is on the heap.

fn main() {
    println!("{}", size_of::<String>());
    println!("{}", size_of::<&str>());
}
4.

What does this print?

fn main() {
    let n: i32 = 10;
    let pair = (n, n);
    println!("{}", size_of_val(&n));
    println!("{}", size_of_val(&pair));
}
5.

What does this print?

fn main() {
    println!("{}", size_of::<i32>());
    println!("{}", size_of::<i64>());
    println!("{}", size_of::<bool>());
    println!("{}", size_of::<(i32, i32)>());
}
6.

What does this print?

fn main() {
    println!("{}", size_of::<[u8; 10]>());
    println!("{}", size_of::<[u32; 10]>());
}
7.

What does this print? A char holds one character, in any language, so it always takes four bytes.

fn main() {
    println!("{}", size_of::<char>());
    println!("{}", size_of::<[char; 3]>());
}
8.

What does this print?

fn main() {
    println!("{}", size_of::<u8>());
    println!("{}", size_of::<u16>());
    println!("{}", size_of::<u32>());
    println!("{}", size_of::<u64>());
}
9.

What does this print? () is the tuple with nothing in it.

fn main() {
    println!("{}", size_of::<i8>());
    println!("{}", size_of::<i128>());
    println!("{}", size_of::<()>());
}
10.

What does this print?

fn main() {
    println!("{}", size_of::<f32>());
    println!("{}", size_of::<f64>());
}
11.

usize is the type for a size or a position. What does this print?

fn main() {
    let n: usize = 1000;
    println!("{}", n);
    println!("{}", size_of::<usize>());
}
12.

What does this print?

fn main() {
    println!("{}", size_of::<(bool, bool)>());
    println!("{}", size_of::<[bool; 5]>());
    println!("{}", size_of::<(i32, i32, i32)>());
    println!("{}", size_of::<(f64, f64)>());
    println!("{}", size_of::<(char, char)>());
}
13.

What does this print?

fn main() {
    println!("{}", size_of::<[i64; 5]>());
    println!("{}", size_of::<(u16, u16, u16, u16)>());
}

Compound Sizes Add Up

14.

What does this print?

fn main() {
    println!("{}", size_of::<[(i32, i32); 3]>());
}
15.

What does this print?

fn main() {
    println!("{}", size_of::<((i32, i32), (i32, i32))>());
}
16.

What does this print? The two lines ask the same question in two different ways.

fn main() {
    println!("{}", size_of::<[i32; 3]>() + size_of::<[i32; 3]>());
    println!("{}", size_of::<[i32; 6]>());
}
17.

What does this print? This is an array of three arrays, each holding two numbers.

fn main() {
    println!("{}", size_of::<[[i32; 2]; 3]>());
}
18.

What does this print?

fn main() {
    println!("{}", size_of::<((u8, u8), (u8, u8))>());
}
19.

What does this print?

fn main() {
    let x_size = size_of::<i16>() + size_of::<i16>();
    let y_size = size_of::<i32>();
    println!("{}", x_size == y_size);
}
20.

Does this compile?

fn make_array(n: usize) {
    let arr = [0; n];
    println!("{:?}", arr);
}

fn main() {
    make_array(5);
}
21.

Does this compile?

fn total(marks: [i32; 3]) -> i32 {
    marks[0] + marks[1] + marks[2]
}

fn main() {
    let marks = [72, 45, 90, 61];
    println!("{}", total(marks));
}

What an Assignment Copies

22.

What does this print?

fn main() {
    let a = 5;
    let b = a;
    println!("{a} {b}");
}
23.

What does this print?

fn main() {
    let a = 5;
    let mut b = a;
    b += 10;
    println!("{a} {b}");
}
24.

What does this print?

fn main() {
    let ready = true;
    let copy = ready;
    println!("{ready} {copy}");
}
25.

What does this print?

fn main() {
    let letter = 'x';
    let copy = letter;
    println!("{letter}{copy}");
}
26.

What does this print?

fn main() {
    let a = (3, 4);
    let b = a;
    println!("{} {}", a.0, b.1);
}
27.

What does this print?

fn main() {
    let point = (1, 2);
    let mut moved = point;
    moved.0 = 9;
    println!("{:?} {:?}", point, moved);
}
28.

What does this print?

fn main() {
    let a = [1, 2, 3];
    let mut b = a;
    b[0] = 99;
    println!("{:?} {:?}", a, b);
}
29.

What does this print?

fn main() {
    let a = 5;
    let a = a + 1;
    let b = a;
    println!("{a} {b}");
}
30.

What does this print?

fn change(mut n: i32) {
    n += 100;
    println!("inside: {n}");
}

fn main() {
    let n = 5;
    change(n);
    println!("outside: {n}");
}
31.

What does this print?

fn zero(mut arr: [i32; 3]) {
    arr[0] = 0;
    println!("inside: {:?}", arr);
}

fn main() {
    let marks = [72, 45, 90];
    zero(marks);
    println!("outside: {:?}", marks);
}

Function Calls and the Stack

32.

What does this print?

fn one() -> i32 {
    println!("one runs");
    1
}

fn two() -> i32 {
    println!("two runs");
    2
}

fn main() {
    println!("{}", one() + two());
}
33.

What does this print?

fn double(n: i32) -> i32 {
    n * 2
}

fn main() {
    let a = 3;
    let b = double(double(a));
    println!("{a} {b}");
}
34.

What does this print?

fn square(n: i32) -> i32 {
    n * n
}

fn sum_of_squares(a: i32, b: i32) -> i32 {
    square(a) + square(b)
}

fn main() {
    println!("{}", sum_of_squares(3, 4));
}
35.

What does this print?

fn a() {
    println!("a starts");
    b();
    println!("a ends");
}

fn b() {
    println!("b starts");
    println!("b ends");
}

fn main() {
    println!("main starts");
    a();
    println!("main ends");
}
36.

What does this print?

fn outer() {
    println!("outer starts");
    inner();
    inner();
    println!("outer ends");
}

fn inner() {
    println!("inner");
}

fn main() {
    outer();
}
37.

What does this print?

fn countdown(n: i32) {
    println!("{n}");
    if n > 0 {
        countdown(n - 1);
    }
}

fn main() {
    countdown(3);
}
38.

This calls itself the same way question 37 does, but the println! has moved. What does it print?

fn countdown(n: i32) {
    if n > 0 {
        countdown(n - 1);
    }
    println!("{n}");
}

fn main() {
    countdown(3);
}
39.

What does this print?

fn factorial(n: u32) -> u32 {
    if n == 0 {
        1
    } else {
        n * factorial(n - 1)
    }
}

fn main() {
    println!("{}", factorial(5));
}
40.

What does this print?

fn sum_to(n: u32) -> u32 {
    if n == 0 {
        0
    } else {
        n + sum_to(n - 1)
    }
}

fn main() {
    println!("{}", sum_to(4));
}
41.

What does this print?

fn fib(n: u32) -> u32 {
    if n < 2 {
        n
    } else {
        fib(n - 1) + fib(n - 2)
    }
}

fn main() {
    println!("{}", fib(6));
}
42.

What does this print?

fn triple(n: i32) -> i32 {
    n * 3
}

fn main() {
    println!("{}", triple(triple(2)));
}
43.

What does this print?

fn frame_bytes(n: u32) -> u32 {
    let a = 0u8;
    let b = 0u8;
    let local = (size_of::<u8>() + size_of::<u8>()) as u32;
    println!("{a}{b}");
    if n == 0 {
        local
    } else {
        local + frame_bytes(n - 1)
    }
}

fn main() {
    println!("{}", frame_bytes(2));
}

Adding Up Stack Bytes

44.

What does this print?

fn main() {
    let total = size_of::<i32>() + size_of::<u8>() + size_of::<bool>();
    println!("{total}");
}
45.

What does this print?

fn main() {
    let total = size_of::<[i32; 3]>() + size_of::<f64>();
    println!("{total}");
}
46.

What does this print?

fn main() {
    let a_size = size_of::<u8>();
    let b_size = size_of::<u8>();
    let c_size = size_of::<[u8; 8]>();
    println!("{}", a_size + b_size + c_size);
}
47.

What does this print?

fn main() {
    let total = size_of::<[u8; 4]>() + size_of::<u32>();
    println!("{total}");
}
48.

What does this print?

fn main() {
    let a_size = size_of::<i32>();
    let b_size = size_of::<i32>();
    let c_size = size_of::<i32>();
    println!("{}", a_size + b_size + c_size);
}
49.

The stack starts at address 1000 for this one. What does this print?

fn main() {
    const STACK_START: u32 = 1000;
    let a_size = size_of::<u32>() as u32;
    let b_size = size_of::<bool>() as u32;
    println!("{}", STACK_START + a_size + b_size);
}
50.

Each call adds one u32 to the running total. What does this print?

fn frame(n: u32) -> u32 {
    let local = size_of::<u32>() as u32;
    if n == 0 {
        local
    } else {
        local + frame(n - 1)
    }
}

fn main() {
    println!("{}", frame(3));
}