MCQs on Control Flow | Rust

Rust is a systems programming language that emphasizes performance, safety, and concurrency. Control flow in Rust allows you to manage program execution using structures like if expressions, loops, and pattern matching. These tools enable clear and efficient handling of conditional logic and iteration.


Control Flow in Rust – MCQs

Part 1: If Expressions

  1. What type of expression is an if statement in Rust?
    • A) Function
    • B) Expression
    • C) Macro
    • D) Statement
  2. Which of the following is correct syntax for an if expression in Rust?
    • A) if x > 10 { println!("x is greater than 10"); }
    • B) if (x > 10) { println!("x is greater than 10"); }
    • C) if x > 10: println!("x is greater than 10");
    • D) if x > 10 { println[x is greater than 10]; }
  3. In Rust, can an if expression return a value?
    • A) Yes
    • B) No
    • C) Only if else is used
    • D) Only in for loops
  4. What is the correct way to use an else block with an if expression in Rust?
    • A) if condition { ... } else condition { ... }
    • B) if condition { ... } else { ... }
    • C) if { ... } else { ... }
    • D) if condition -> { ... } else { ... }
  5. Which of the following is NOT a valid use of an if expression in Rust?
    • A) As a boolean statement
    • B) To return a value from a function
    • C) Inside another if expression
    • D) As a statement without a block
  6. What happens if the condition in an if expression is false in Rust?
    • A) The code inside the if block will execute.
    • B) The code inside the else block will execute.
    • C) An error will be thrown.
    • D) The code inside the else if block will execute.
  7. Which statement is true about Rust’s if expressions?
    • A) if expressions must always return a value.
    • B) if expressions can optionally return values.
    • C) if expressions cannot return values.
    • D) if expressions are only valid for comparison operations.
  8. How do you ensure that both if and else branches return a value in Rust?
    • A) Use else inside if without a return statement.
    • B) Use the same type of value in both branches.
    • C) Use different types in each branch.
    • D) Ensure no return is used in either branch.
  9. Can an if expression be used as a condition for a loop in Rust?
    • A) Yes, it can evaluate to a boolean.
    • B) No, only while and for can be used.
    • C) Yes, but only in conjunction with match expressions.
    • D) No, an if expression cannot be used in this context.
  10. Which of the following is the correct syntax for a conditional if expression returning a value?
    • A) let result = if x > 10 { "Greater" } else { "Lesser" };
    • B) let result = if x > 10 => "Greater" else => "Lesser";
    • C) let result = if x > 10 -> "Greater"; else -> "Lesser";
    • D) let result = if (x > 10) { "Greater" } else "Lesser";

Part 2: Loop, While, and For Loops

  1. Which of the following is the correct syntax for a loop in Rust?
    • A) loop { ... }
    • B) loop => { ... }
    • C) loop { ... end }
    • D) loop: { ... }
  2. In Rust, what will happen if you don’t include a break in a loop?
    • A) The loop will run indefinitely.
    • B) The loop will stop after one iteration.
    • C) A compilation error will occur.
    • D) The loop will execute for a specified number of iterations.
  3. Which keyword is used to terminate a loop early in Rust?
    • A) exit
    • B) break
    • C) continue
    • D) return
  4. How can you specify the number of iterations in a for loop in Rust?
    • A) for i in 0..10 { ... }
    • B) for i in 10..0 { ... }
    • C) for i = 0 to 10 { ... }
    • D) for i until 10 { ... }
  5. Which of the following best describes a while loop in Rust?
    • A) It runs indefinitely until explicitly stopped with break.
    • B) It iterates over a collection until all items are processed.
    • C) It runs as long as a specified condition is true.
    • D) It executes a set number of iterations.
  6. What happens when a continue statement is encountered in a loop in Rust?
    • A) The loop terminates immediately.
    • B) The next iteration of the loop starts.
    • C) The loop will stop after the current iteration.
    • D) The program will exit the loop.
  7. How can you make a for loop iterate over an array in Rust?
    • A) for element in array { ... }
    • B) for array { ... }
    • C) for i in range { ... }
    • D) for element in range { ... }
  8. Which of the following statements is true about Rust’s loop?
    • A) It is the same as a while loop.
    • B) It always runs exactly 10 times.
    • C) It is an infinite loop unless break is used.
    • D) It requires a specific termination condition.
  9. Which loop is typically used to iterate over a range in Rust?
    • A) while
    • B) for
    • C) loop
    • D) continue
  10. How do you skip the current iteration in a for loop in Rust?
    • A) Use exit.
    • B) Use break.
    • C) Use continue.
    • D) Use skip.

Part 3: Match Expressions

  1. What is the purpose of a match expression in Rust?
    • A) To loop through a collection
    • B) To handle conditional branching based on multiple patterns
    • C) To perform bitwise operations
    • D) To catch errors during runtime
  2. What keyword is used to specify the possible patterns in a match expression in Rust?
    • A) case
    • B) switch
    • C) match
    • D) pattern
  3. How does Rust’s match expression handle non-exhaustive patterns?
    • A) It automatically raises an error.
    • B) It returns a default value.
    • C) It requires a default case.
    • D) It results in an infinite loop.
  4. Which of the following is a valid pattern in a match expression in Rust?
    • A) match x { 1..5 => "Low", _ => "High" }
    • B) match x { 1..5 -> "Low", _ => "High" }
    • C) match x { _ => "Any" }
    • D) match { x > 5 } { _ => "High" }
  5. What is the role of the _ pattern in Rust’s match expression?
    • A) To specify a case for a specific value
    • B) To create a fallback or catch-all pattern
    • C) To match an empty value
    • D) To match the first possible value
  6. What happens if no pattern matches the value in a match expression in Rust?
    • A) The program crashes.
    • B) It defaults to the first pattern.
    • C) The code inside match will not execute.
    • D) The program will skip the match statement.
  7. How would you use a match expression to match on an enum in Rust?
    • A) match EnumType::Variant { ... }
    • B) match Enum { Variant => ... }
    • C) match { EnumType } { Variant => ... }
    • D) match Enum { ... }
  8. Can you use match expressions with integers in Rust?
    • A) Yes, it is a common use case.
    • B) No, match expressions are only for strings.
    • C) No, only floats can be matched.
    • D) Yes, but only with binary numbers.
  9. How do you ensure that a match expression in Rust covers all cases?
    • A) Use the default keyword.
    • B) Ensure all possible patterns are specified.
    • C) Always add an else clause.
    • D) Use the * wildcard.
  10. Which of the following statements about the match expression is correct?
    • A) match expressions require an else block.
    • B) match expressions always result in a true/false evaluation.
    • C) match is exhaustive and must account for all possibilities.
    • D) match expressions are only used for string matching.

Answer Key

QnoAnswer
1B) Expression
2A) if x > 10 { println!("x is greater than 10"); }
3A) Yes
4B) if condition { ... } else { ... }
5D) As a statement without a block
6B) The code inside the else block will execute.
7B) if expressions can optionally return values.
8B) Use the same type of value in both branches.
9A) Yes, it can evaluate to a boolean.
10A) let result = if x > 10 { "Greater" } else { "Lesser" };
11A) loop { ... }
12A) The loop will run indefinitely.
13B) break
14A) for i in 0..10 { ... }
15C) It runs as long as a specified condition is true.
16B) The next iteration of the loop starts.
17A) for element in array { ... }
18C) It is an infinite loop unless break is used.
19B) for
20C) Use continue.
21B) To handle conditional branching based on multiple patterns
22C) match
23A) It automatically raises an error.
24A) match x { 1..5 => "Low", _ => "High" }
25B) To create a fallback or catch-all pattern
26A) The program crashes.
27A) match EnumType::Variant { ... }
28A) Yes, it is a common use case.
29B) Ensure all possible patterns are specified.
30C) match is exhaustive and must account for all possibilities.

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