What does this print?
fn main() {
let age = 20;
let height = 1.75;
let city = "Delhi";
println!("{age} {height} {city}");
}Running it prints:
20 1.75 Delhi
Practice 03
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.
0 of 50 answered·0 correct
What does this print?
fn main() {
let age = 20;
let height = 1.75;
let city = "Delhi";
println!("{age} {height} {city}");
}Running it prints:
20 1.75 Delhi
What does this print?
fn main() {
let mut score = 10;
println!("{score}");
score = 15;
println!("{score}");
score += 5;
println!("{score}");
}Running it prints:
10 15 20
What does this print?
fn main() {
let word = "cat";
let word = word.len();
println!("{word}");
}Running it prints:
3
What does this print?
fn main() {
let y = 2;
let y = y + 3;
{
let y = y * 10;
println!("inside: {y}");
}
println!("outside: {y}");
}Running it prints:
inside: 50 outside: 5
Does this compile?
fn main() {
let total = 100;
println!("{total}");
total = 200;
println!("{total}");
}The compiler says:
error[E0384]: cannot assign twice to immutable variable `total`
Does this compile?
fn main() {
let mut label = "abc";
label = label.len();
println!("{label}");
}The compiler says:
error[E0308]: mismatched types
What does this print?
const MAX_SPEED: u32 = 120;
fn main() {
println!("{MAX_SPEED}");
println!("{}", MAX_SPEED - 20);
}Running it prints:
120 100
Does this compile? What does it print?
fn main() {
let total = 42;
println!("finished");
}Running it prints:
finished
println!What does this print?
fn main() {
let item = "pen";
let cost = 12;
println!("{} costs {}", item, cost);
println!("{item} costs {cost}");
}Running it prints:
pen costs 12 pen costs 12
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:?}");
}Running it prints:
0.125 [ 9] [0009] 1001 00001001 (3, 4)
What does this print?
fn main() {
let n: u8 = 200;
println!("{n:08b}");
}Running it prints:
11001000
What does this print?
fn main() {
let mark = 15;
println!("{}", mark * 2);
}Running it prints:
30
What does this print?
fn main() {
let total = 2_500_000;
let flags = 0b0000_1010;
let small = 9u8;
println!("{total}");
println!("{flags}");
println!("{small}");
}Running it prints:
2500000 10 9
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);
}Running it prints:
13 5 36 2 1 -2 -1
What does this print? % gives the remainder of a division.
fn main() {
let n = 14;
println!("{}", n % 2);
let m = 15;
println!("{}", m % 2);
}Running it prints:
0 1
What does this print? * is worked out before +.
fn main() {
println!("{}", 2 + 3 * 4);
println!("{}", (2 + 3) * 4);
}Running it prints:
14 20
What does this print?
fn main() {
let mut total = 0;
total += 12;
total -= 3;
total *= 2;
println!("{total}");
}Running it prints:
18
Does this compile?
fn main() {
let x: u8 = 250;
let y = x + 10;
println!("{y}");
}The compiler says:
error: this arithmetic operation will overflow
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));
}Running it prints:
205
Then it panics: attempt to add with overflow
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));
}Running it prints:
0 11
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);
}Running it prints:
1.25 0.3333333333333333 0.33333334 0.33 0.30000000000000004
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);
}Does this compile?
fn main() {
let qty = 3;
let rate = 4.5;
println!("{}", qty * rate);
}The compiler says:
error[E0277]: cannot multiply `{integer}` by `{float}`Does this compile?
fn main() {
let a: u8 = 5;
let b: u32 = 12;
println!("{}", a + b);
}The compiler says:
error[E0308]: mismatched types error[E0277]: cannot add `u32` to `u8`
Does this compile?
fn main() {
let a: i32 = 5;
let b: i64 = 10;
println!("{}", a + b);
}The compiler says:
error[E0308]: mismatched types error[E0277]: cannot add `i64` to `i32`
asWhat 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);
}Running it prints:
26 244 251 4 -4 4.5
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);
}charWhat does this print?
fn main() {
let letter = 'B';
let accented = 'ü';
println!("{letter} {accented}");
println!("{}", letter as u8);
println!("{}", 98u8 as char);
}Running it prints:
B ü 66 b
Does this compile?
fn main() {
let letter: char = "B";
println!("{letter}");
}The compiler says:
error[E0308]: mismatched types
boolWhat does this print?
fn main() {
println!("{}", 10 > 3);
println!("{}", 10 < 3);
}Running it prints:
true false
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}");
}Running it prints:
lab
4
9.5
("lab", 4, 9.5)
lab 4 9.5Does this compile?
fn main() {
let pair = (1, 2);
println!("{}", pair);
}The compiler says:
error[E0277]: `({integer}, {integer})` doesn't implement `std::fmt::Display`What does this print?
fn main() {
let marks = [88, 41, 67, 95, 52];
println!("{}", marks[0]);
println!("{}", marks[4]);
println!("{}", marks.len());
println!("{marks:?}");
}Running it prints:
88 52 5 [88, 41, 67, 95, 52]
What does this print?
fn main() {
let mut counts = [0; 5];
counts[3] = 9;
println!("{counts:?}");
}Running it prints:
[0, 0, 0, 9, 0]
Does this compile?
fn main() {
let marks = [88, 41, 67];
println!("{}", marks[3]);
}The compiler says:
error: this operation will panic at runtime
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));
}Running it prints:
41
Then it panics: index out of bounds: the len is 3 but the index is 3
Does this compile?
fn main() {
let arr: [i32; 3] = [1, 2];
println!("{arr:?}");
}The compiler says:
error[E0308]: mismatched types
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>());
}Running it prints:
bool 1 i8 1 i64 8 f32 4 char 4
What does this print?
fn main() {
println!("{}", size_of::<(u8, u8, u8)>());
println!("{}", size_of::<[u8; 6]>());
println!("{}", size_of::<(i32, i32, i32)>());
}Running it prints:
3 6 12
What does this print?
fn greet(name: &str) {
println!("hi {name}");
}
fn main() {
greet("Rohan");
greet("Meera");
}Running it prints:
hi Rohan hi Meera
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);
}Running it prints:
6 * 7 = 42 9 * 3 = 27
What does this print?
fn triple(n: i32) -> i32 {
n * 3
}
fn main() {
println!("{}", triple(14));
}Running it prints:
42
Does this compile?
fn triple(n: i32) -> i32 {
n * 3;
}
fn main() {
println!("{}", triple(14));
}The compiler says:
error[E0308]: mismatched types
What does this print?
fn area(width: f64, height: f64) -> f64 {
return width * height;
}
fn main() {
println!("{}", area(3.0, 4.5));
}Running it prints:
13.5
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}");
}Running it prints:
23 / 6 = 3 remainder 5
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));
}Running it prints:
75.00
What does this print?
fn shout() {
println!("hi");
}
fn main() {
let result = shout();
println!("{result:?}");
}Running it prints:
hi ()
Does this compile?
fn compute(n: i32) {
n * 2
}
fn main() {
println!("{}", compute(5));
}The compiler says:
error[E0308]: mismatched types error[E0277]: `()` doesn't implement `std::fmt::Display`
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));
}Running it prints:
true false
What does this print?
fn first_of(pair: (i32, i32)) -> i32 {
pair.0
}
fn main() {
let p = (7, 20);
println!("{}", first_of(p));
}Running it prints:
7