Practice 03

Variables, Types and Functions

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

Course

0 of 50 answered·0 correct

Variables

1.

What does this print?

fn main() {
    let age = 20;
    let height = 1.75;
    let city = "Delhi";
    println!("{age} {height} {city}");
}
2.

What does this print?

fn main() {
    let mut score = 10;
    println!("{score}");

    score = 15;
    println!("{score}");

    score += 5;
    println!("{score}");
}
3.

What does this print?

fn main() {
    let word = "cat";
    let word = word.len();

    println!("{word}");
}
4.

What does this print?

fn main() {
    let y = 2;
    let y = y + 3;

    {
        let y = y * 10;
        println!("inside: {y}");
    }

    println!("outside: {y}");
}
5.

Does this compile?

fn main() {
    let total = 100;
    println!("{total}");

    total = 200;
    println!("{total}");
}
6.

Does this compile?

fn main() {
    let mut label = "abc";
    label = label.len();

    println!("{label}");
}
7.

What does this print?

const MAX_SPEED: u32 = 120;

fn main() {
    println!("{MAX_SPEED}");
    println!("{}", MAX_SPEED - 20);
}
8.

Does this compile? What does it print?

fn main() {
    let total = 42;
    println!("finished");
}

Printing with println!

9.

What does this print?

fn main() {
    let item = "pen";
    let cost = 12;

    println!("{} costs {}", item, cost);
    println!("{item} costs {cost}");
}
10.

What does this print?

fn main() {
    let mark = 9;
    let ratio = 1.0 / 8.0;
    let pair = (3, 4);

    println!("{ratio:.3}");
    println!("[{mark:>4}]");
    println!("[{mark:04}]");
    println!("{mark:b}");
    println!("{mark:08b}");
    println!("{pair:?}");
}
11.

What does this print?

fn main() {
    let n: u8 = 200;
    println!("{n:08b}");
}
12.

What does this print?

fn main() {
    let mark = 15;
    println!("{}", mark * 2);
}

Integers

13.

What does this print?

fn main() {
    let total = 2_500_000;
    let flags = 0b0000_1010;
    let small = 9u8;

    println!("{total}");
    println!("{flags}");
    println!("{small}");
}

Arithmetic

14.

What does this print?

fn main() {
    println!("{}", 9 + 4);
    println!("{}", 9 - 4);
    println!("{}", 9 * 4);
    println!("{}", 9 / 4);
    println!("{}", 9 % 4);
    println!("{}", -9 / 4);
    println!("{}", -9 % 4);
}
15.

What does this print? % gives the remainder of a division.

fn main() {
    let n = 14;
    println!("{}", n % 2);

    let m = 15;
    println!("{}", m % 2);
}
16.

What does this print? * is worked out before +.

fn main() {
    println!("{}", 2 + 3 * 4);
    println!("{}", (2 + 3) * 4);
}
17.

What does this print?

fn main() {
    let mut total = 0;
    total += 12;
    total -= 3;
    total *= 2;

    println!("{total}");
}

Overflow

18.

Does this compile?

fn main() {
    let x: u8 = 250;
    let y = x + 10;

    println!("{y}");
}
19.

What does the first call print? Does the second call finish?

fn add_five(n: u8) -> u8 {
    n + 5
}

fn main() {
    println!("{}", add_five(200));
    println!("{}", add_five(252));
}
20.

What does this print? wrapping_add never panics.

fn add_wrap(n: u8) -> u8 {
    n.wrapping_add(1)
}

fn main() {
    println!("{}", add_wrap(255));
    println!("{}", add_wrap(10));
}

Floating Point Numbers

21.

What does this print?

fn main() {
    let full = 5.0 / 4.0;
    let third: f64 = 1.0 / 3.0;
    let rough: f32 = 1.0 / 3.0;

    println!("{full}");
    println!("{third}");
    println!("{rough}");
    println!("{third:.2}");
    println!("{}", 0.1 + 0.2);
}
22.

Question 21 prints 0.30000000000000004 on its last line, not 0.3. Why?

fn main() {
    let full = 5.0 / 4.0;
    let third: f64 = 1.0 / 3.0;
    let rough: f32 = 1.0 / 3.0;

    println!("{full}");
    println!("{third}");
    println!("{rough}");
    println!("{third:.2}");
    println!("{}", 0.1 + 0.2);
}

Mixing Types

23.

Does this compile?

fn main() {
    let qty = 3;
    let rate = 4.5;

    println!("{}", qty * rate);
}
24.

Does this compile?

fn main() {
    let a: u8 = 5;
    let b: u32 = 12;

    println!("{}", a + b);
}
25.

Does this compile?

fn main() {
    let a: i32 = 5;
    let b: i64 = 10;

    println!("{}", a + b);
}

Converting with as

26.

What does this print?

fn main() {
    let small: u8 = 6;
    let big: u32 = 20;
    println!("{}", small as u32 + big);

    let wide: u16 = 500;
    println!("{}", wide as u8);

    let neg: i8 = -5;
    println!("{}", neg as u8);

    println!("{}", 4.99 as i32);
    println!("{}", -4.99 as i32);

    let n: i32 = 9;
    println!("{}", n as f64 / 2.0);
}
27.

In question 26, neg as u8 is 251. Why does a negative number become a large positive one?

fn main() {
    let small: u8 = 6;
    let big: u32 = 20;
    println!("{}", small as u32 + big);

    let wide: u16 = 500;
    println!("{}", wide as u8);

    let neg: i8 = -5;
    println!("{}", neg as u8);

    println!("{}", 4.99 as i32);
    println!("{}", -4.99 as i32);

    let n: i32 = 9;
    println!("{}", n as f64 / 2.0);
}

char

28.

What does this print?

fn main() {
    let letter = 'B';
    let accented = 'ü';

    println!("{letter} {accented}");
    println!("{}", letter as u8);
    println!("{}", 98u8 as char);
}
29.

Does this compile?

fn main() {
    let letter: char = "B";
    println!("{letter}");
}

bool

30.

What does this print?

fn main() {
    println!("{}", 10 > 3);
    println!("{}", 10 < 3);
}

Tuples

31.

What does this print?

fn main() {
    let point = ("lab", 4, 9.5);

    println!("{}", point.0);
    println!("{}", point.1);
    println!("{}", point.2);
    println!("{point:?}");

    let (room, floor, area) = point;
    println!("{room} {floor} {area}");
}
32.

Does this compile?

fn main() {
    let pair = (1, 2);
    println!("{}", pair);
}

Arrays

33.

What does this print?

fn main() {
    let marks = [88, 41, 67, 95, 52];

    println!("{}", marks[0]);
    println!("{}", marks[4]);
    println!("{}", marks.len());
    println!("{marks:?}");
}
34.

What does this print?

fn main() {
    let mut counts = [0; 5];
    counts[3] = 9;

    println!("{counts:?}");
}
35.

Does this compile?

fn main() {
    let marks = [88, 41, 67];
    println!("{}", marks[3]);
}
36.

What does the first call print? Does the second call finish?

fn pick(marks: [i32; 3], position: usize) -> i32 {
    marks[position]
}

fn main() {
    let marks = [88, 41, 67];
    println!("{}", pick(marks, 1));
    println!("{}", pick(marks, 3));
}
37.

Does this compile?

fn main() {
    let arr: [i32; 3] = [1, 2];
    println!("{arr:?}");
}

The Size of Each Type

38.

What does this print?

fn main() {
    println!("bool  {}", size_of::<bool>());
    println!("i8    {}", size_of::<i8>());
    println!("i64   {}", size_of::<i64>());
    println!("f32   {}", size_of::<f32>());
    println!("char  {}", size_of::<char>());
}
39.

What does this print?

fn main() {
    println!("{}", size_of::<(u8, u8, u8)>());
    println!("{}", size_of::<[u8; 6]>());
    println!("{}", size_of::<(i32, i32, i32)>());
}

Functions

40.

What does this print?

fn greet(name: &str) {
    println!("hi {name}");
}

fn main() {
    greet("Rohan");
    greet("Meera");
}
41.

What does this print?

fn show_product(a: i32, b: i32) {
    println!("{a} * {b} = {}", a * b);
}

fn main() {
    show_product(6, 7);
    show_product(9, 3);
}
42.

What does this print?

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

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

Does this compile?

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

fn main() {
    println!("{}", triple(14));
}
44.

What does this print?

fn area(width: f64, height: f64) -> f64 {
    return width * height;
}

fn main() {
    println!("{}", area(3.0, 4.5));
}
45.

What does this print?

fn split(a: u32, b: u32) -> (u32, u32) {
    (a / b, a % b)
}

fn main() {
    let (whole, left) = split(23, 6);
    println!("23 / 6 = {whole} remainder {left}");
}
46.

What does this print?

fn average(marks: [u32; 3]) -> f64 {
    let mut total = 0;
    total += marks[0];
    total += marks[1];
    total += marks[2];

    total as f64 / marks.len() as f64
}

fn main() {
    let marks = [60, 75, 90];
    println!("{:.2}", average(marks));
}
47.

What does this print?

fn shout() {
    println!("hi");
}

fn main() {
    let result = shout();
    println!("{result:?}");
}
48.

Does this compile?

fn compute(n: i32) {
    n * 2
}

fn main() {
    println!("{}", compute(5));
}
49.

What does this print?

fn is_multiple(n: i32, of: i32) -> bool {
    n % of == 0
}

fn main() {
    println!("{}", is_multiple(12, 4));
    println!("{}", is_multiple(12, 5));
}
50.

What does this print?

fn first_of(pair: (i32, i32)) -> i32 {
    pair.0
}

fn main() {
    let p = (7, 20);
    println!("{}", first_of(p));
}