MCQ Related “Rust”

  1. What is the main purpose of Rust?
    • A) Web Development
    • B) Systems Programming
    • C) Game Development
    • D) Data Science
  2. Which keyword is used to define a variable in Rust?
    • A) let
    • B) var
    • C) const
    • D) define
  3. Which of the following is a valid way to create a function in Rust?
    • A) function my_func() {}
    • B) def my_func() {}
    • C) fn my_func() {}
    • D) func my_func() {}
  4. What does the mut keyword signify in Rust?
    • A) Immutable variable
    • B) Mutable variable
    • C) Constant variable
    • D) Read-only variable
  5. What indicates a block of code in Rust?
    • A) Parentheses
    • B) Curly Braces
    • C) Square Brackets
    • D) Angle Brackets
  6. How do you create an array in Rust?
    • A) let arr = [1, 2, 3];
    • B) let arr = (1, 2, 3);
    • C) let arr = {1, 2, 3};
    • D) let arr: [1, 2, 3];
  7. Which of the following is NOT a primitive type in Rust?
    • A) i32
    • B) f64
    • C) String
    • D) bool
  8. What is the output of println!("Hello, {}!", "World");?
    • A) Hello, World!
    • B) “Hello, World!”
    • C) Hello, World
    • D) “Hello, {}!”
  9. Which trait is used for formatting strings in Rust?
    • A) Format
    • B) Display
    • C) Stringify
    • D) Print
  10. How can you define a struct in Rust?
    • A) struct MyStruct {}
    • B) structure MyStruct {}
    • C) MyStruct = struct {}
    • D) define MyStruct {}
  11. What is the purpose of the match keyword in Rust?
    • A) To create cases
    • B) To compare values
    • C) To handle control flow
    • D) To declare variables
  12. How do you define an enum in Rust?
    • A) enum MyEnum {}
    • B) enumeration MyEnum {}
    • C) MyEnum = enum {}
    • D) define MyEnum {}
  13. Which command is used to compile a Rust program?
    • A) rust build
    • B) rustc
    • C) cargo run
    • D) compile rust
  14. What is the purpose of Cargo in Rust?
    • A) Package manager
    • B) Library manager
    • C) Compiler
    • D) Framework
  15. Which symbol is used for comments in Rust?
    • A) //
    • B) /*
    • C) //
    • D) #
  16. What does the ; signify in Rust?
    • A) End of statement
    • B) Declaration
    • C) Separator
    • D) Comment
  17. What is the default return type of a function in Rust?
    • A) i32
    • B) ()
    • C) null
    • D) void
  18. Which of the following is used to handle errors in Rust?
    • A) Result
    • B) Exception
    • C) Error
    • D) Issue
  19. How do you declare a constant in Rust?
    • A) const MY_CONST: i32 = 10;
    • B) let MY_CONST: i32 = 10;
    • C) constant MY_CONST: i32 = 10;
    • D) define MY_CONST: i32 = 10;
  20. What are lifetimes used for in Rust?
    • A) Compile-time checking
    • B) Memory management
    • C) Type safety
    • D) Both A and B
  21. How is string concatenation done in Rust?
    • A) “hello” + “world”
    • B) format!(“hello {}”, “world”)
    • C) “hello”.concat(“world”)
    • D) Both B and C
  22. Which of the following is NOT a collection type in Rust?
    • A) Vectors
    • B) HashMaps
    • C) Strings
    • D) Arrays
  23. What does the #[derive(Debug)] attribute do in Rust?
    • A) Implements default characteristics
    • B) Allows for debug formatting
    • C) Automatically generates functions
    • D) All of the above
  24. How do you handle multiple ownership in Rust?
    • A) Using closures
    • B) Using smart pointers
    • C) Using references
    • D) All of the above
  25. Which operator is used for pattern matching in Rust?
    • A) in
    • B) match
    • C) ==
    • D) switch
  26. What is the purpose of the self keyword?
    • A) Referring to a module
    • B) Referring to the current instance
    • C) Referring to a variable
    • D) Referring to lifetime
  27. How can mutable references be created?
    • A) &mut variable
    • B) variable.mutable()
    • C) &variable
    • D) mut &variable
  28. How do you access a value from a HashMap?
    • A) map[key]
    • B) map.get(key)
    • C) map.get_value(key)
    • D) Both A and B
  29. What is a closure in Rust?
    • A) A function that captures its environment
    • B) A struct
    • C) An enum
    • D) A type
  30. Which keyword is used to define a trait in Rust?
    • A) trait
    • B) interface
    • C) type
    • D) struct
  31. What is the size of a Rust bool type?
    • A) 1 byte
    • B) 4 bytes
    • C) 8 bytes
    • D) 2 bytes
  32. Which function is used to create a new thread?
    • A) spawn()
    • B) thread::new()
    • C) std::thread::spawn()
    • D) create_thread()
  33. What is unsafe code in Rust?
    • A) Code that may result in an error
    • B) Code that cannot be checked by the compiler
    • C) Code that requires additional permissions
    • D) Code that uses obsolete features
  34. Which macro is used for benchmarking in Rust?
    • A) #[bench]
    • B) #[test]
    • C) #[macro]
    • D) #[profile]
  35. How do you create an iterator from a collection?
    • A) collection.iter()
    • B) collection.new_iterator()
    • C) iteration(collection)
    • D) create_iterator(collection)
  36. What does Option<T> signify in Rust?
    • A) A mandatory value
    • B) An optional value
    • C) A default value
    • D) None of the above
  37. How can you convert a string to an integer in Rust?
    • A) string.parse::<i32>()
    • B) string.to_int()
    • C) string.convert(i32)
    • D) parse(string, i32)
  38. What is the purpose of the unsafe keyword in Rust?
    • A) To write low-level code
    • B) To bypass borrowing rules
    • C) To facilitate raw pointers
    • D) All of the above
  39. Which operator is used for equality checking in Rust?
    • A) =
    • B) ==
    • C) ===
    • D) =!=
  40. How do you create a loop in Rust?
    • A) loop {}
    • B) while true {}
    • C) for infinity {}
    • D) Both A and B
  41. Which of these is a feature of Rust’s ownership model?
    • A) No garbage collection
    • B) Multiple ownership
    • C) Shared mutable state
    • D) Reference cycles
  42. How do you define a variable with a specific type?
    • A) let var: Type;
    • B) var: Type = value;
    • C) Type var;
    • D) var as Type;
  43. What does panic!() do in Rust?
    • A) Causes graceful exit
    • B) Triggers a runtime error
    • C) Handles exceptions
    • D) Restarts the program
  44. Which crate is used for asynchronous programming in Rust?
    • A) async-std
    • B) futures
    • C) tokio
    • D) All of the above
  45. How is a hashmap declared in Rust?
    • A) let mut map = HashMap::new();
    • B) let map = HashMap {};
    • C) let mut map: HashMap = {};
    • D) map = HashMap.new();
  46. What would vec![1, 2, 3] create in Rust?
    • A) An array
    • B) A tuple
    • C) A vector
    • D) A hashmap
  47. How can errors be propagated in Rust?
    • A) Using the ? operator
    • B) Using Result<T, E>
    • C) Both A and B
    • D) Using exceptions
  48. What does std::fs::read_to_string() do in Rust?
    • A) Reads binary data
    • B) Reads a file and returns its contents as a string
    • C) Writes data to a file
    • D) Creates a string variable
  49. How do you implement a trait for a struct in Rust?
    • A) impl Trait for Struct {}
    • B) structure Struct implements Trait {}
    • C) Trait: Struct {}
    • D) Struct implements Trait {}
  50. Which crate is commonly used for testing in Rust?
    • A) test
    • B) rand
    • C) tokio
    • D) reqwest