Rust Async Programming Quiz

Rust
0 Passed
0% acceptance

Master asynchronous programming in Rust with futures, async/await, and concurrent execution patterns

40 Questions
~80 minutes
1

Question 1

What is a Future in Rust?

A
A value that may not be ready yet but will be at some point
B
A completed computation
C
A thread
D
Futures don't exist in Rust
2

Question 2

What does async fn do?

A
Creates function returning Future that executes asynchronously
B
Creates synchronous function
C
Creates thread
D
async fn doesn't exist
3

Question 3

What does await do?

A
Suspends function until Future completes, yields control back
B
Blocks current thread
C
Creates new thread
D
await doesn't exist
4

Question 4

What does this basic async example do?

rust
async fn hello() {
    println!("Hello");
}

#[tokio::main]
async fn main() {
    hello().await;
}
A
Defines async function, calls it with await in tokio runtime
B
Compilation error
C
Blocks main thread
D
Runtime error
5

Question 5

What is the difference between async blocks and async functions?

A
Both create Futures, but blocks can be used in expressions
B
Functions are async, blocks are sync
C
They are identical
D
Async blocks don't exist
6

Question 6

What does this async block example show?

rust
let future = async {
    println!("In async block");
    42
};

#[tokio::main]
async fn main() {
    let result = future.await;
    println!("Result: {}", result);
}
A
Async block creates Future that can be awaited later
B
Compilation error
C
Block executes immediately
D
Runtime error
7

Question 7

What is tokio?

A
Popular async runtime for Rust providing executor and I/O
B
Sync runtime
C
Threading library
D
tokio doesn't exist
8

Question 8

What is the difference between tokio::spawn and std::thread::spawn?

A
tokio::spawn creates async tasks, thread::spawn creates OS threads
B
They are identical
C
tokio::spawn is slower
D
tokio::spawn doesn't exist
9

Question 9

What does this tokio::spawn example do?

rust
#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        println!("In spawned task");
        42
    });
    let result = handle.await.unwrap();
    println!("Result: {}", result);
}
A
Spawns async task concurrently, awaits its completion
B
Compilation error
C
Blocks main thread
D
Runtime error
10

Question 10

What is async I/O?

A
Non-blocking I/O operations that don't waste threads waiting
B
Blocking I/O
C
Synchronous I/O
D
Async I/O doesn't exist
11

Question 11

What does tokio::fs provide?

A
Async file system operations
B
Sync file operations
C
Thread pool for I/O
D
tokio::fs doesn't exist
12

Question 12

What is a Stream in async Rust?

A
Async iterator that yields values over time
B
Sync iterator
C
Channel
D
Streams don't exist
13

Question 13

What does this Stream example show?

rust
use tokio_stream::{self as stream, StreamExt};

#[tokio::main]
async fn main() {
    let mut stream = stream::iter(vec![1, 2, 3]);
    while let Some(value) = stream.next().await {
        println!("Got: {}", value);
    }
}
A
Async iteration over collection values
B
Compilation error
C
Sync iteration
D
Runtime error
14

Question 14

What is select!?

A
Macro for waiting on multiple Futures simultaneously
B
Loop construct
C
Channel operation
D
select! doesn't exist
15

Question 15

What does this select! example do?

rust
use tokio::select;

#[tokio::main]
async fn main() {
    let task1 = tokio::spawn(async { tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; 1 });
    let task2 = tokio::spawn(async { tokio::time::sleep(tokio::time::Duration::from_secs(2)).await; 2 });
    
    select! {
        result = task1 => println!("Task 1 completed: {:?}", result),
        result = task2 => println!("Task 2 completed: {:?}", result),
    }
}
A
Waits for first task to complete, cancels other
B
Compilation error
C
Waits for both tasks
D
Runtime error
16

Question 16

What is join!?

A
Macro for waiting on multiple Futures concurrently
B
String concatenation
C
Channel operation
D
join! doesn't exist
17

Question 17

What does this join! example show?

rust
use tokio::join;

#[tokio::main]
async fn main() {
    async fn task(n: u32) -> u32 {
        tokio::time::sleep(tokio::time::Duration::from_secs(n)).await;
        n
    }
    
    let (a, b, c) = join!(task(1), task(2), task(3));
    println!("Results: {}, {}, {}", a, b, c);
}
A
Runs three tasks concurrently, waits for all to complete
B
Compilation error
C
Runs tasks sequentially
D
Runtime error
18

Question 18

What is async trait methods?

A
Trait methods that can be async
B
Sync trait methods
C
Trait objects
D
Async traits don't exist
19

Question 19

What is the async_trait crate used for?

A
Making async trait methods object-safe
B
Making traits async
C
Performance optimization
D
async_trait doesn't exist
20

Question 20

What is Pin in async Rust?

A
Type that prevents moving after certain operations
B
Performance optimization
C
Memory allocation
D
Pin doesn't exist
21

Question 21

What is cooperative vs preemptive multitasking?

A
Cooperative yields control at await points, preemptive can interrupt
B
They are identical
C
Cooperative is better
D
Preemptive doesn't exist in async
22

Question 22

What is async recursion?

A
Recursive async functions that can cause stack overflow
B
Sync recursion
C
Tail recursion
D
Async recursion doesn't exist
23

Question 23

What is the solution to async recursion stack overflow?

A
Use Box::pin() or convert to iterative algorithm
B
Use more stack space
C
Use sync recursion
D
No solution exists
24

Question 24

What is async closure?

A
Closure that returns a Future
B
Sync closure
C
Async function
D
Async closures don't exist
25

Question 25

What does this async closure example do?

rust
#[tokio::main]
async fn main() {
    let async_closure = async |x: i32| {
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        x * 2
    };
    
    let result = async_closure(5).await;
    println!("Result: {}", result);
}
A
Creates async closure that can be called with await
B
Compilation error
C
Closure executes immediately
D
Runtime error
26

Question 26

What is the difference between tokio::time::sleep and std::thread::sleep?

A
tokio::time::sleep is async and doesn't block thread
B
They are identical
C
std::thread::sleep is async
D
tokio::time::sleep doesn't exist
27

Question 27

What is timeout in tokio?

A
Cancels Future if it takes too long
B
Delays execution
C
Speeds up execution
D
timeout doesn't exist
28

Question 28

What does this timeout example show?

rust
use tokio::time::{timeout, Duration};

#[tokio::main]
async fn main() {
    let result = timeout(Duration::from_secs(1), async {
        tokio::time::sleep(Duration::from_secs(2)).await;
        42
    }).await;
    
    match result {
        Ok(value) => println!("Got: {}", value),
        Err(_) => println!("Timeout!"),
    }
}
A
Cancels async operation after 1 second timeout
B
Compilation error
C
Sleeps for 1 second
D
Runtime error
29

Question 29

What is async channel?

A
Async version of mpsc channel for inter-task communication
B
Sync channel
C
Network socket
D
Async channels don't exist
30

Question 30

What is the difference between bounded and unbounded channels?

A
Bounded has limited capacity, unbounded grows dynamically
B
They are identical
C
Bounded is faster
D
Unbounded doesn't exist
31

Question 31

What is async mutex?

A
Async-aware mutex that doesn't block threads
B
Sync mutex
C
Channel
D
Async mutex doesn't exist
32

Question 32

What is the key difference between std::sync::Mutex and tokio::sync::Mutex?

A
tokio::Mutex is async-aware and doesn't block OS threads
B
They are identical
C
std::sync::Mutex is async
D
tokio::sync::Mutex doesn't exist
33

Question 33

What is blocking code in async context?

A
Sync operations that block threads, hurting async performance
B
Async code
C
Fast code
D
Blocking code doesn't exist
34

Question 34

What does spawn_blocking do?

A
Runs blocking code on separate thread pool without blocking async runtime
B
Makes code async
C
Blocks current thread
D
spawn_blocking doesn't exist
35

Question 35

What is async main?

A
Main function that can use await and run async code
B
Sync main function
C
Thread entry point
D
Async main doesn't exist
36

Question 36

What is the async ecosystem in Rust?

A
Collection of crates like tokio, async-std, futures, reqwest
B
Sync libraries
C
Threading libraries
D
Async ecosystem doesn't exist
37

Question 37

What is the difference between tokio and async-std?

A
Both async runtimes, tokio more feature-rich, async-std simpler
B
They are identical
C
tokio is sync, async-std is async
D
async-std doesn't exist
38

Question 38

What is async testing?

A
Testing async functions using #[tokio::test] attribute
B
Sync testing
C
Manual testing
D
Async testing doesn't exist
39

Question 39

What is the performance advantage of async?

A
Single thread can handle many concurrent connections
B
Faster than sync code
C
Uses less memory
D
No performance advantage
40

Question 40

In a web server handling thousands of concurrent HTTP requests, each making database queries and external API calls, which async patterns would you use and why?

A
Async I/O for all operations, join! for concurrent DB/API calls, timeout for external APIs
B
Thread-per-request model
C
Blocking I/O with thread pools
D
Sequential processing

QUIZZES IN Rust