Practice 04

Conditions, Matching and Loops

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

Course

0 of 50 answered·0 correct

Conditions Must Be bool

1.

With the declarations below, does this line compile?

let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;

if count { }
2.

With the declarations below, does this line compile?

let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;

if ready { }
3.

With the declarations below, does this line compile?

let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;

if count > 0 { }
4.

With the declarations below, does this line compile?

let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;

if !n { }
5.

With the declarations below, does this line compile?

let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;

if n == 0 && ready { }
6.

With the declarations below, does this line compile?

let count: i32 = 5;
let ready: bool = true;
let n: u8 = 0;

if count && ready { }
7.

What does this print?

let grade = if 72 >= 50 { "pass" } else { "fail" };
println!("{grade}");

Pattern Matching

8.

n is 3u8. What does this print?

match n {
    0 => println!("zero"),
    1 | 2 => println!("small"),
    _ => println!("big"),
}
9.

score is 64u8. What does this print?

match score {
    0..=49  => println!("fail"),
    50..=64 => println!("pass"),
    _       => println!("merit"),
}
10.

n is 0u8. What does this print?

match n {
    0 => println!("zero"),
    n if n % 2 == 0 => println!("even"),
    _ => println!("odd"),
}
11.

This does not compile. What is missing?

match n {
    0 => println!("zero"),
    1 => println!("one"),
}
12.

a is true and b is false. What does this print?

match (a, b) {
    (true,  true)  => println!("both"),
    (true,  false) => println!("only a"),
    (false, true)  => println!("only b"),
    (false, false) => println!("neither"),
}
13.

Which arm makes this match cover every value of flag?

let flag = true;

match flag {
    true => println!("on"),
}

Harder Pattern Matching

14.

What does this print?

let n = 5u8;
let target = 3u8;

match n {
    target => println!("matched {target}"),
    _ => println!("no match"),
}
15.

What does this print?

let n = 2u8;

match n {
    1 | 2 | 3 if n > 2 => println!("a"),
    _ => println!("b"),
}
16.

What does this print?

let score = 55u8;

match score {
    0..=100 => println!("in range"),
    50..=60 => println!("middle"),
    _ => println!("out"),
}
17.

What does this print?

let n = 4u8;

match n {
    x if x % 3 == 0 => println!("three"),
    x if x % 2 == 0 => println!("two"),
    _ => println!("other"),
}
18.

What does this print?

let n = 13u8;

match n % 3 {
    0 => println!("zero"),
    1 => println!("one"),
    _ => println!("two"),
}
19.

What does this print?

let n = 12u8;

match n & 1 {
    0 => println!("even"),
    _ => println!("odd"),
}
20.

What does this print?

let n = 5u8;

match n << 1 {
    10 => println!("ten"),
    _ => println!("other"),
}
21.

What does this print?

let n = 0u8;

match !n {
    255 => println!("all ones"),
    _ => println!("other"),
}
22.

The same expression, on a signed type. What does this print?

let n = 0i8;

match !n {
    -1 => println!("minus one"),
    _ => println!("other"),
}
23.

The same !n on an i8, matched against 255. What does this print?

let n = 0i8;

match !n {
    255 => println!("all ones"),
    _ => println!("other"),
}
24.

What does this print?

let n = 7u8;

match n {
    x @ 1..=9 if x % 2 == 1 => println!("odd digit {x}"),
    x @ 1..=9 => println!("even digit {x}"),
    x => println!("not a digit {x}"),
}
25.

What does this print?

let a = false;
let b = true;

match (a, b) {
    (_, true) => println!("b is true"),
    (true, _) => println!("a is true"),
    _ => println!("neither"),
}
26.

What does this print?

let p = (1u8, (2u8, 3u8));

match p {
    (1, (2, x)) => println!("x is {x}"),
    _ => println!("other"),
}
27.

This prints two lines. Write both.

let n = 10u8;

match 3u8 {
    n if n > 5 => println!("big {n}"),
    n => println!("small {n}"),
}

println!("{n}");
28.

This has no _ arm. Does it compile, and what does it print?

let t = -3i8;

match t {
    -128..=-1 => println!("below zero"),
    0 => println!("zero"),
    1..=127 => println!("above zero"),
}
29.

In a pattern, 0..5 means 0 up to 5, not including 5. What does this print?

let n = 3u8;

match n {
    0..5 => println!("under five"),
    _ => println!("other"),
}
30.

Does this compile?

let n = 5u8;

match n {
    0..=255 => println!("all"),
}
31.

Does this compile?

let n = 5u8;

match n {
    0..=254 => println!("most"),
}
32.

A bool has two values, and both are written here. Does this compile?

let flag = true;

match flag {
    true if 1 > 0 => println!("on"),
    false => println!("off"),
}
33.

Does this compile?

let n = 5u8;

match n {
    1..=3 => println!("low"),
    4..=6 => println!("mid"),
}
34.

Does this compile?

let n = 1u8;

let label = match n {
    0 => "zero",
    _ => 1,
};
35.

What does this print?

let n = 1u8;

match n {
    _ => println!("anything"),
    1 => println!("one"),
}
36.

What does this print?

let n = 2u8;

let step = match n {
    0 => 1u8,
    1 | 2 => 2,
    _ => 4,
};

println!("{}", step << 2);
37.

What does this print?

let n = 3u8;

if let 0 = n {
    println!("zero");
} else {
    println!("not zero");
}

Loops

38.

What does this print?

let mut i = 0;
while i < 3 {
    println!("{i}");
    i += 1;
}
39.

What does this print?

for n in 2..5 {
    println!("{n}");
}
40.

What does this print?

for n in 2..=5 {
    println!("{n}");
}
41.

What does this print?

let mut total = 0;
for n in 1..=4 {
    total += n;
}
println!("{total}");
42.

What does this print?

let mut count = 0;
loop {
    count += 2;
    if count > 5 {
        break;
    }
}
println!("{count}");
43.

What does this print?

for n in 0..5 {
    if n % 2 == 0 {
        continue;
    }
    println!("{n}");
}
44.

What does this print?

let mut n = 10;
while n > 10 {
    println!("{n}");
    n -= 1;
}
println!("done");
45.

What does this print?

let marks = [70, 80, 90];
for (i, m) in marks.iter().enumerate() {
    println!("{i} {m}");
}
46.

What does this print?

let mut n = 1;
let result = loop {
    n *= 2;
    if n > 20 {
        break n;
    }
};
println!("{result}");
47.

What does this print?

'outer: for a in 1..=3 {
    for b in 1..=3 {
        if a * b == 4 {
            break 'outer;
        }
        println!("{a} {b}");
    }
}
48.

Does this compile? If it does, what does it print?

let marks = [70, 80, 90];
for m in marks {
    println!("{m}");
}
println!("{}", marks.len());
49.

Does this compile? If it does, what does it print?

for n in 0..3 {
    println!("{n}");
}
println!("{n}");
50.

When does this stop?

let mut i = 0;
while i < 3 {
    println!("{i}");
}