Rust Data Types Quiz

Rust
0 Passed
0% acceptance

45 comprehensive questions on Rust's type system, covering scalar types, compound types, type conversions, numeric operations, and string types — with 15 code examples demonstrating practical type usage patterns.

45 Questions
~90 minutes
1

Question 1

What are the four primary scalar types in Rust?

A
integers, floating-point numbers, booleans, and characters
B
strings, arrays, tuples, and structs
C
i32, f64, bool, and char
D
signed, unsigned, float, and boolean
2

Question 2

How many bits does an i32 integer type represent?

A
16 bits
B
32 bits
C
64 bits
D
8 bits
3

Question 3

What is the default integer type in Rust when you don't specify one?

A
i32
B
i64
C
usize
D
i8
4

Question 4

What happens when you try to add two different integer types without explicit conversion?

A
The operation succeeds with automatic conversion
B
Compilation error: mismatched types
C
Runtime error
D
The smaller type is promoted to the larger type
5

Question 5

Which of these is a valid floating-point literal in Rust?

A
3.14159
B
3.14159f32
C
Both A and B
D
Neither is valid
6

Question 6

What is the difference between f32 and f64 floating-point types?

A
f32 is single precision (32 bits), f64 is double precision (64 bits)
B
f32 is faster, f64 is more precise
C
f64 is the default for literals
D
All of the above
7

Question 7

How do you represent a single character in Rust?

A
Using single quotes: 'A'
B
Using double quotes: "A"
C
Using backticks: `A`
D
Characters are not supported
8

Question 8

What will this code output?

rust
fn main() {
    let c = 'z';
    let z = 'ℤ';
    let heart_eyed_cat = '😻';
    println!("Characters: {}, {}, {}", c, z, heart_eyed_cat);
}
A
Characters: z, ℤ, 😻
B
Compilation error
C
Characters: z, ?, ?
D
Runtime error
9

Question 9

What are compound types in Rust?

A
Types that group multiple values into one type
B
Types that can be converted automatically
C
Types that are stored on the heap
D
Types that cannot be copied
10

Question 10

How do you declare a tuple in Rust?

A
let tup: (i32, f64, u8) = (500, 6.4, 1);
B
let tup = [500, 6.4, 1];
C
let tup = {500, 6.4, 1};
D
let tup = <500, 6.4, 1>;
11

Question 11

How do you access elements of a tuple?

A
Using dot notation: tuple.0, tuple.1, etc.
B
Using bracket notation: tuple[0], tuple[1], etc.
C
Using arrow notation: tuple->0, tuple->1, etc.
D
Tuples cannot be indexed
12

Question 12

What is destructuring in the context of tuples?

A
Breaking a tuple into individual variables
B
Converting a tuple to an array
C
Sorting tuple elements
D
Removing elements from a tuple
13

Question 13

How do you declare an array in Rust?

A
let a = [1, 2, 3, 4, 5];
B
let a: [i32; 5] = [1, 2, 3, 4, 5];
C
Both A and B are valid
D
Neither is valid
14

Question 14

What happens if you try to access an array element beyond its bounds?

A
The program panics at runtime
B
Compilation error
C
Returns a default value
D
Wraps around to the beginning
15

Question 15

How do you create an array with the same value repeated?

A
let a = [3; 5]; // creates [3, 3, 3, 3, 3]
B
let a = [3].repeat(5);
C
let a = vec![3; 5];
D
This is not possible with arrays
16

Question 16

What is the key difference between arrays and vectors in Rust?

A
Arrays have fixed size known at compile time, vectors can grow dynamically
B
Arrays are heap-allocated, vectors are stack-allocated
C
Vectors are faster than arrays
D
Arrays can contain different types, vectors cannot
17

Question 17

How do you perform explicit type conversion (casting) in Rust?

A
Using the 'as' keyword: value as TargetType
B
Using parentheses: (TargetType) value
C
Using angle brackets: <TargetType> value
D
Automatic conversion happens
18

Question 18

When building a graphics application that needs to convert floating-point colors to integer pixel values, how should you handle the conversion?

A
Use 'as' casting: (color * 255.0) as u8
B
Use automatic conversion
C
Use a conversion function
D
This requires unsafe code
19

Question 19

What happens when you cast a large integer to a smaller type?

A
The value wraps around (two's complement wrapping)
B
Compilation error
C
Runtime panic
D
The value is truncated
20

Question 20

What will this code output?

rust
fn main() {
    let decimal = 65.4321_f32;
    let integer = decimal as u8;
    let character = integer as char;
    println!("{} -> {} -> {}", decimal, integer, character);
}
A
65.4321 -> 65 -> A
B
Compilation error
C
65.4321 -> 65 -> ?
D
Runtime error
21

Question 21

What are the two main string types in Rust?

A
String and &str
B
str and &String
C
String and string
D
str and Str
22

Question 22

What is the difference between String and &str?

A
String owns its data and can be modified, &str is a reference to existing UTF-8 data
B
String is stack-allocated, &str is heap-allocated
C
&str can be modified, String cannot
D
There is no functional difference
23

Question 23

How do you create a new String?

A
String::new() or String::from("text")
B
let s = "text";
C
&str::new()
D
str::from("text")
24

Question 24

What happens when you try to modify a string literal?

A
It works because string literals are mutable
B
Compilation error: string literals are immutable
C
Runtime error
D
The literal becomes a String
25

Question 25

How do you convert a String to a &str?

A
&string_variable or string_variable.as_str()
B
string_variable.to_str()
C
Automatic conversion happens
D
This conversion is not possible
26

Question 26

What will this code output?

rust
fn main() {
    let mut s = String::from("Hello");
    s.push_str(", world!");
    println!("{}", s);
}
A
Hello, world!
B
Hello
C
Compilation error
D
Runtime error
27

Question 27

How do you concatenate strings in Rust?

A
Using the + operator or format! macro
B
Using the & operator
C
Using the concat! macro
D
Strings cannot be concatenated
28

Question 28

What is UTF-8 encoding in the context of Rust strings?

A
The encoding standard that Rust strings use to represent text
B
A compression algorithm
C
A hashing function
D
A sorting algorithm
29

Question 29

When processing user input that contains international characters, what should you be aware of?

A
Rust strings handle UTF-8 automatically, but indexing by bytes vs characters differs
B
International characters are not supported
C
You need to convert to ASCII first
D
Performance is significantly impacted
30

Question 30

What are the basic arithmetic operations available for numeric types?

A
Addition (+), subtraction (-), multiplication (*), division (/), remainder (%)
B
Only addition and subtraction
C
Only multiplication and division
D
Arithmetic operations are not supported
31

Question 31

What happens when integer arithmetic overflows in debug mode?

A
The program panics
B
The value wraps around
C
Compilation error
D
The operation is ignored
32

Question 32

How can you explicitly handle integer overflow?

A
Using methods like checked_add, saturating_add, wrapping_add, overflowing_add
B
Using unsafe code
C
Using larger integer types
D
Overflow cannot be handled explicitly
33

Question 33

In a financial application where you need to prevent monetary value overflow, which overflow handling method should you use?

A
checked_add() to detect and handle overflow
B
wrapping_add() to allow overflow
C
saturating_add() to clamp at maximum
D
overflowing_add() for performance
34

Question 34

What is the difference between f32 and f64 division results?

A
f64 provides more precision than f32
B
f32 is faster but less precise
C
f64 defaults to infinity for division by zero
D
All of the above
35

Question 35

How do you access individual bytes of a string?

A
string.as_bytes()
B
string.bytes()
C
Both A and B work
D
Strings don't have bytes
36

Question 36

What will this code output?

rust
fn main() {
    let s = "Hello, 世界!";
    println!("Bytes: {:?}", s.as_bytes());
    println!("Length: {}", s.len());
}
A
Bytes: [72, 101, 108, 108, 111, 44, 32, 228, 189, 160, 231, 149, 140, 33], Length: 13
B
Bytes: [72, 101, 108, 108, 111, 44, 32, 19990, 30028, 33], Length: 9
C
Compilation error
D
Runtime error
37

Question 37

Why is string indexing by integer potentially dangerous in Rust?

A
Because UTF-8 characters can be multiple bytes, so s[0] might not be a valid character
B
Because strings are immutable
C
Because Rust doesn't support indexing
D
Because strings are heap-allocated
38

Question 38

How do you iterate over characters in a string properly?

A
for c in string.chars() { ... }
B
for i in 0..string.len() { let c = string[i]; ... }
C
for c in string { ... }
D
Characters cannot be iterated
39

Question 39

When implementing a text processing function that needs to handle international text correctly, what approach should you use?

A
Use chars() for character-by-character processing
B
Use byte indexing with bounds checking
C
Convert to ASCII first
D
Use unsafe code for performance
40

Question 40

What is the result of this arithmetic expression?

rust
fn main() {
    let result = 10u32.saturating_add(20u32);
    println!("Result: {}", result);
}
A
Result: 30
B
Result: 0 (wrapped)
C
Compilation error
D
Runtime panic
41

Question 41

How do you create a tuple with mixed types?

A
let mixed = (42, 3.14, true, 'R');
B
let mixed = [42, 3.14, true, 'R'];
C
let mixed: (i32, f64, bool, char) = (42, 3.14, true, 'R');
D
Both A and C are valid
42

Question 42

What happens when you try to access a tuple element that doesn't exist?

A
Compilation error
B
Runtime panic
C
Returns a default value
D
Wraps around to the first element
43

Question 43

In a graphics application where you need to represent RGBA color values, which type would be most appropriate?

A
Tuple: (u8, u8, u8, u8)
B
Array: [u8; 4]
C
Struct with named fields
D
Any of the above could work
44

Question 44

How do you convert between numeric types safely?

A
Using TryFrom and TryInto traits
B
Using the 'as' keyword
C
Using automatic conversion
D
Numeric conversion is always unsafe
45

Question 45

When processing a large dataset where you need to ensure no data loss during type conversions, what approach should you use?

A
Use TryFrom/TryInto and handle conversion errors
B
Use 'as' casting for performance
C
Use larger types to avoid overflow
D
Use unchecked conversions

QUIZZES IN Rust