MCQs on Foreign Function Interface (FFI) | Rust

Rust’s Foreign Function Interface (FFI) allows Rust code to interact with other programming languages, such as C, Python, and JavaScript. This enables developers to leverage existing libraries and improve performance by combining Rust’s speed with the flexibility of other languages. The following MCQs cover essential topics related to FFI, including calling C code, unsafe operations, and creating bindings.

1. Calling C Code from Rust

  1. Which keyword is used in Rust to declare an external function, often for FFI with C?
    a) extern
    b) unsafe
    c) ffi
    d) foreign
  2. In Rust, how can you link to a C function from an external library?
    a) By using extern crate
    b) By using extern with a #[link] attribute
    c) By defining the C function inside a mod block
    d) By using unsafe only
  3. How would you call a C function that returns an integer in Rust?
    a) By declaring the function as fn c_func() -> i32;
    b) By using unsafe and calling c_func() directly
    c) By importing the C function with the use keyword
    d) By writing a wrapper function in Rust
  4. What type of pointer does Rust use when calling C functions that return pointers?
    a) *mut T
    b) *const T
    c) &T
    d) Box<T>
  5. How would you declare a C function that takes a pointer to an integer as an argument in Rust?
    a) fn c_func(x: &i32);
    b) fn c_func(x: *const i32);
    c) fn c_func(x: *mut i32);
    d) fn c_func(x: i32);

2. Rust’s Unsafe and FFI Safety

  1. What does the unsafe keyword signify when calling FFI functions in Rust?
    a) The code will be compiled without type checks
    b) The function may violate memory safety rules
    c) The function is not allowed to access memory directly
    d) The function can only be called from the main thread
  2. Why is Rust’s unsafe necessary for calling C code?
    a) To allow the use of raw pointers and manual memory management
    b) To ensure type safety
    c) To enable garbage collection
    d) To optimize function calls for performance
  3. In Rust, what does unsafe allow you to do?
    a) Call functions with raw pointers
    b) Bypass the borrow checker
    c) Access fields of a struct directly without checking for ownership
    d) All of the above
  4. When interfacing with C code in Rust, how does the compiler ensure safety?
    a) By automatically detecting unsafe code at compile time
    b) Rust does not ensure safety when calling C code
    c) By requiring manual checks in unsafe blocks
    d) Through runtime checks on every function call
  5. What kind of errors can arise when using unsafe in FFI?
    a) Memory corruption
    b) Data races
    c) Undefined behavior
    d) All of the above

3. Creating Rust Bindings for C Libraries

  1. What is the primary purpose of Rust bindings in FFI?
    a) To enable Rust to interact with C libraries
    b) To compile C code into Rust binaries
    c) To protect Rust code from memory leaks
    d) To automatically convert C code into Rust syntax
  2. What tool is commonly used in Rust for generating bindings to C libraries?
    a) bindgen
    b) cargo ffi
    c) rust-bind
    d) cc-rs
  3. Which Rust crate is often used to automate the generation of FFI bindings?
    a) ffi-lib
    b) bindgen
    c) cbind
    d) rust-bindings
  4. How would you include a C library in a Rust project?
    a) Use #[link] attribute in an extern block
    b) Directly include the C code in the Rust project
    c) Modify the Rust build script with a C compiler option
    d) Automatically compile the C code during Rust compilation
  5. In Rust, when linking to an external C library, which attribute is used to specify the library’s name?
    a) #[link(name = "libname")]
    b) #[lib("libname")]
    c) #[extern(name = "libname")]
    d) #[import("libname")]

4. Interfacing with Other Languages

  1. How can Rust be interfaced with Python?
    a) By using PyO3 or rust-cpython crates
    b) By writing Python code directly in the Rust project
    c) By compiling Python code into Rust
    d) By using FFI directly with Python’s C API
  2. What crate is commonly used to call Python code from Rust?
    a) pybind11
    b) PyO3
    c) rustpython
    d) pyrust
  3. Which of the following is true when using FFI between Rust and JavaScript?
    a) Rust can directly execute JavaScript in the browser
    b) Rust can use WebAssembly to interact with JavaScript
    c) Rust cannot interact with JavaScript
    d) Rust and JavaScript can share memory directly without any intermediary
  4. How does Rust interface with JavaScript in web applications?
    a) By using the wasm-bindgen crate
    b) By calling JavaScript functions directly in Rust
    c) By importing JavaScript libraries as Rust modules
    d) By compiling JavaScript into Rust code
  5. Which feature does Rust offer for FFI between Rust and JavaScript?
    a) rust-web
    b) wasm-bindgen
    c) js-ffi
    d) cargo-js

5. Miscellaneous FFI Topics

  1. In Rust, what type of function is typically used when interfacing with a C library?
    a) Unsafe functions
    b) Safe functions
    c) Public functions
    d) Static functions
  2. How do you pass a string from Rust to C using FFI?
    a) By passing a &str reference
    b) By passing a String object
    c) By using a raw pointer to a null-terminated C string
    d) By using a &String reference
  3. How does Rust ensure that a pointer passed to C is not null?
    a) Rust automatically checks all pointers
    b) It relies on the programmer to ensure null safety
    c) Rust uses a MaybeNull type for pointers
    d) Rust automatically converts None into a null pointer
  4. In Rust, when using unsafe for FFI, what must be done to ensure proper memory management?
    a) Manually manage memory allocation and deallocation
    b) Use Rust’s garbage collector
    c) Trust the external code to handle memory
    d) Rust automatically manages memory in unsafe blocks
  5. Which of the following is true about Rust’s approach to FFI safety?
    a) Rust guarantees memory safety even when calling unsafe code
    b) Rust performs runtime checks on all FFI calls
    c) Rust requires the developer to manually ensure FFI safety
    d) Rust does not allow FFI at all
  6. How would you call a function from a C library that accepts a double argument in Rust?
    a) fn c_func(x: f64);
    b) fn c_func(x: i32);
    c) fn c_func(x: &f64);
    d) fn c_func(x: double);
  7. What is the #[repr(C)] attribute used for in Rust FFI?
    a) To ensure that Rust structs are laid out in memory the same way as C structs
    b) To automatically generate C bindings
    c) To declare a function as callable from C
    d) To convert a C function into a Rust closure
  8. How does the Rust toolchain assist in creating FFI bindings for C?
    a) By automatically generating the necessary C code
    b) By linking C libraries to Rust at compile time
    c) By providing a build.rs script to configure FFI
    d) By converting C syntax to Rust syntax
  9. Which of the following is a primary challenge when working with FFI in Rust?
    a) Ensuring memory safety in unsafe code
    b) Writing bindings for incompatible C libraries
    c) Understanding Rust’s ownership and borrowing system
    d) All of the above
  10. When calling FFI functions, which Rust feature should be avoided for memory safety?
    a) unsafe blocks
    b) extern declarations
    c) #[link] attributes
    d) & references

Answer Key (Tabular Form)

QnoAnswer
1a) extern
2b) By using extern with a #[link] attribute
3a) By declaring the function as fn c_func() -> i32;
4b) *const T
5c) fn c_func(x: *mut i32);
6b) The function may violate memory safety rules
7a) To allow the use of raw pointers and manual memory management
8d) All of the above
9c) By requiring manual checks in unsafe blocks
10d) All of the above
11a) To enable Rust to interact with C libraries
12a) bindgen
13b) bindgen
14c) Modify the Rust build script with a C compiler option
15a) #[link(name = "libname")]
16a) By using PyO3 or rust-cpython crates
17b) PyO3
18b) Rust can use WebAssembly to interact with JavaScript
19a) By using the wasm-bindgen crate
20b) wasm-bindgen
21a) Unsafe functions
22c) By using a raw pointer to a null-terminated C string
23b) It relies on the programmer to ensure null safety
24a) Manually manage memory allocation and deallocation
25c) Rust requires the developer to manually ensure FFI safety
26a) fn c_func(x: f64);
27a) To ensure that Rust structs are laid out in memory the same way as C structs
28c) By providing a build.rs script to configure FFI
29d) All of the above
30a) unsafe blocks

4o mini

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top