Rust Modules Quiz

Rust
0 Passed
0% acceptance

40 comprehensive questions on Rust's module system, covering module creation, visibility, imports, paths, and the use keyword — with 11 code examples demonstrating practical module organization patterns.

40 Questions
~80 minutes
1

Question 1

What is a module in Rust?

A
A namespace that contains definitions (functions, structs, traits, etc.) and can control their visibility
B
A file that contains Rust code
C
A package or crate
D
A type of function
2

Question 2

How do you define a module in Rust?

A
Using the mod keyword: mod module_name { ... }
B
Using the module keyword: module module_name { ... }
C
Using the namespace keyword: namespace module_name { ... }
D
Modules are defined automatically
3

Question 3

What is the default visibility of items in a module?

A
Private (only accessible within the module)
B
Public (accessible from anywhere)
C
Depends on the item type
D
Inherited from parent module
4

Question 4

How do you make an item public in a module?

A
Using the pub keyword: pub fn function_name() { ... }
B
Using the public keyword
C
Using the export keyword
D
Items are public by default
5

Question 5

What will this module code do?

rust
mod my_module {
    pub fn public_function() {
        println!("This is public");
    }
    
    fn private_function() {
        println!("This is private");
    }
}

fn main() {
    my_module::public_function();
    my_module::private_function();
}
A
Prints 'This is public' and fails to compile
B
Prints both messages successfully
C
Fails to compile because modules can't have functions
D
Prints 'This is private' and fails to compile
6

Question 6

How do you access items from a module?

A
Using path syntax: module_name::item_name
B
Using dot notation: module_name.item_name
C
Using bracket notation: module_name[item_name]
D
Items are automatically available
7

Question 7

What is the use keyword used for?

A
Bringing items into scope to avoid repetitive path typing
B
Defining new modules
C
Making items public
D
Importing external crates
8

Question 8

How do you use the use keyword to import items?

A
use module_name::item_name;
B
import module_name::item_name;
C
using module_name::item_name;
D
include module_name::item_name;
9

Question 9

What will this use statement do?

rust
mod network {
    pub mod client {
        pub fn connect() {}
    }
}

use network::client;

fn main() {
    client::connect();
}
A
Allows calling client::connect() instead of network::client::connect()
B
Compilation error
C
Imports the connect function directly
D
Creates a new module
10

Question 10

How do you import multiple items with a single use statement?

A
use module_name::{item1, item2, item3};
B
use module_name::item1, module_name::item2;
C
use module_name::all;
D
Multiple items cannot be imported at once
11

Question 11

What is an absolute path in Rust modules?

A
A path that starts from the crate root using crate::
B
A path that starts from the current module
C
A path that uses relative navigation
D
A path that includes the full filesystem path
12

Question 12

What is a relative path in Rust modules?

A
A path that starts from the current module and uses self or super
B
A path that starts from the crate root
C
A path that uses absolute filesystem paths
D
A path that includes the full module hierarchy
13

Question 13

What does super:: refer to in a module path?

A
The parent module
B
The current module
C
The crate root
D
A sibling module
14

Question 14

What does self:: refer to in a module path?

A
The current module
B
The parent module
C
The crate root
D
A child module
15

Question 15

How do you organize modules across multiple files?

A
Create module_name.rs files or module_name/ directories with mod.rs
B
All modules must be in a single file
C
Use separate directories for each module
D
Modules cannot span multiple files
16

Question 16

What is the purpose of mod.rs in a module directory?

A
It defines the module's public interface and re-exports items
B
It contains all the module's code
C
It imports external dependencies
D
It defines the module's tests
17

Question 17

How do you declare a submodule in mod.rs?

A
pub mod submodule_name;
B
mod submodule_name;
C
use submodule_name;
D
include submodule_name;
18

Question 18

What is re-exporting in modules?

A
Making items from submodules available at higher levels using pub use
B
Moving modules to different files
C
Renaming modules
D
Converting private items to public
19

Question 19

What will this re-export code do?

rust
mod network {
    pub mod client {
        pub fn connect() {}
    }
}

pub use network::client::connect;

fn main() {
    connect(); // Can now call directly
}
A
Allows calling connect() directly without module path
B
Compilation error
C
Creates a new function
D
Moves the function to a different module
20

Question 20

What is the crate root in Rust?

A
The main.rs file or lib.rs file that defines the crate
B
The src/ directory
C
The Cargo.toml file
D
The target/ directory
21

Question 21

In a scenario where you're building a large application with networking, database, and UI components, how should you organize the modules?

A
Create top-level modules like network, db, ui, each with submodules
B
Put all code in a single large file
C
Create separate crates for each component
D
Use functions instead of modules
22

Question 22

What is the difference between pub and pub(crate)?

A
pub is public to external crates, pub(crate) is public within the current crate
B
pub(crate) is public to external crates, pub is public within the crate
C
They are identical
D
pub(crate) doesn't exist
23

Question 23

How do you make a struct's fields public?

A
pub struct StructName { pub field1: Type1, pub field2: Type2 }
B
pub struct StructName { field1: Type1, field2: Type2 }
C
struct StructName { pub field1: Type1, field2: Type2 }
D
Fields cannot be made public individually
24

Question 24

What happens if you try to access a private field of a public struct?

A
Compilation error: field is private
B
The field is accessible
C
Runtime error
D
The struct becomes private
25

Question 25

How do you organize tests in modules?

A
Using #[cfg(test)] modules and #[test] functions
B
Tests must be in separate files
C
Tests cannot be in modules
D
Using test modules without attributes
26

Question 26

What is the purpose of the #[cfg(test)] attribute?

A
Conditionally compiles code only when running tests
B
Marks functions as tests
C
Runs tests in parallel
D
Generates test documentation
27

Question 27

How do you access the crate root from anywhere in the crate?

A
Using crate:: at the start of paths
B
Using root::
C
Using :: at the start
D
This is not possible
28

Question 28

What is the difference between use and mod declarations?

A
mod declares modules, use brings items into scope
B
use declares modules, mod brings items into scope
C
They are interchangeable
D
use is for external crates, mod is for local modules
29

Question 29

How do you handle name conflicts when importing items?

A
Use as keyword: use module::Item as NewName;
B
Use different module names
C
Name conflicts are not possible
D
Use the rename keyword
30

Question 30

What will this code with name aliasing do?

rust
mod math {
    pub fn add(x: i32, y: i32) -> i32 { x + y }
}

use math::add as sum;

fn main() {
    println!("Result: {}", sum(3, 4));
}
A
Prints 'Result: 7' using the aliased name
B
Compilation error
C
Prints 'Result: 0'
D
Creates a new function
31

Question 31

How do you import all public items from a module?

A
use module_name::*;
B
use module_name::all;
C
use module_name;
D
This is not recommended
32

Question 32

When should you avoid using glob imports (use module::*)?

A
When it could cause name conflicts or reduce clarity
B
When importing from the standard library
C
When the module has many items
D
Glob imports should always be avoided
33

Question 33

How do you structure a large project with many modules?

A
Use a hierarchical structure with clear separation of concerns
B
Put everything in the root module
C
Use one file per function
D
Avoid modules entirely
34

Question 34

What is the main.rs file in a binary crate?

A
The crate root that contains the main function
B
A module file
C
The Cargo.toml equivalent
D
The test file
35

Question 35

What is the lib.rs file in a library crate?

A
The crate root that defines the library's public API
B
A module file
C
Contains the main function
D
The test file
36

Question 36

How do you make a module accessible from tests?

A
Use pub(crate) for items that tests need to access
B
Make everything pub
C
Tests can access private items
D
Use special test attributes
37

Question 37

What is the purpose of module documentation comments?

A
To document the module's purpose and usage with /// comments
B
To define the module's visibility
C
To import external documentation
D
To generate module diagrams
38

Question 38

How do you handle cyclic dependencies between modules?

A
Restructure to avoid cycles, or use traits/interfaces
B
Use special syntax to allow cycles
C
Cyclic dependencies are allowed
D
Move code to a common parent module
39

Question 39

What is the benefit of using modules for code organization?

A
Encapsulation, reusability, and maintainability
B
Faster compilation
C
Smaller binary size
D
Better performance
40

Question 40

In a complex web application with authentication, routing, database access, and API endpoints, how would you structure the module hierarchy?

A
Create modules like auth, routing, db, api with appropriate submodules for related functionality
B
Put all code in a single module
C
Create one module per file
D
Use random module organization

QUIZZES IN Rust