0
0
Rustprogramming~20 mins

Logical operators in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Logical Operators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using logical AND and OR?

Consider the following Rust code snippet. What will it print?

Rust
fn main() {
    let a = true;
    let b = false;
    let c = true;
    if a && b || c {
        println!("Result is true");
    } else {
        println!("Result is false");
    }
}
ACompilation error
BResult is false
CResult is true
DNo output
Attempts:
2 left
๐Ÿ’ก Hint

Remember the precedence of logical operators: && has higher precedence than ||.

โ“ Predict Output
intermediate
2:00remaining
What does this Rust code print with negation and logical AND?

Analyze the following Rust code and determine its output.

Rust
fn main() {
    let x = false;
    let y = true;
    if !x && y {
        println!("Condition met");
    } else {
        println!("Condition not met");
    }
}
ACondition not met
BCompilation error
CNo output
DCondition met
Attempts:
2 left
๐Ÿ’ก Hint

Negation ! flips the boolean value.

๐Ÿ”ง Debug
advanced
2:00remaining
What error does this Rust code produce?

Examine this Rust code snippet. What error will it cause when compiled?

Rust
fn main() {
    let a = true;
    let b = false;
    if a & b {
        println!("True");
    } else {
        println!("False");
    }
}
AType mismatch error: cannot use bitwise AND on bool
BNo error, prints "False"
CSyntax error: invalid operator
DType mismatch error: expected bool, found integer
Attempts:
2 left
๐Ÿ’ก Hint

In Rust, & is a bitwise operator and && is a logical operator.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust code with mixed logical operators?

What will this Rust program print?

Rust
fn main() {
    let p = false;
    let q = true;
    let r = false;
    if p || q && r {
        println!("Yes");
    } else {
        println!("No");
    }
}
ANo
BYes
CCompilation error
DNo output
Attempts:
2 left
๐Ÿ’ก Hint

Remember that && has higher precedence than ||.

โ“ Predict Output
expert
2:00remaining
What is the value of variable result after this Rust code runs?

After running this Rust code, what is the value of result?

Rust
fn main() {
    let a = true;
    let b = false;
    let c = true;
    let result = (a && !b) || (b && c);
    println!("{}", result);
}
Afalse
Btrue
CCompilation error
DRuntime panic
Attempts:
2 left
๐Ÿ’ก Hint

Evaluate each part carefully using logical rules.