Bird
Raised Fist0
R Programmingprogramming~20 mins

Logical operators (&, |, !, &&, ||) in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logical Operator Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of vectorized logical AND (&) vs. element-wise AND (&&)
What is the output of the following R code snippet?
R Programming
x <- c(TRUE, FALSE, TRUE)
y <- c(FALSE, TRUE, TRUE)
result <- x & y
result2 <- x && y
print(result)
print(result2)
A
[1] FALSE FALSE TRUE
[1] FALSE
B
[1] TRUE TRUE TRUE
[1] TRUE
C
[1] FALSE TRUE FALSE
[1] TRUE
D
[1] TRUE FALSE TRUE
[1] FALSE
Attempts:
2 left
💡 Hint
Remember that & works element-wise on vectors, while && only checks the first element.
❓ Predict Output
intermediate
1:30remaining
Output of logical OR (|) with mixed TRUE and FALSE vectors
What will be printed by this R code?
R Programming
a <- c(FALSE, FALSE, TRUE)
b <- c(TRUE, FALSE, FALSE)
print(a | b)
A[1] FALSE TRUE TRUE
B[1] TRUE FALSE TRUE
C[1] TRUE TRUE TRUE
D[1] FALSE FALSE FALSE
Attempts:
2 left
💡 Hint
The | operator compares each element and returns TRUE if either is TRUE.
❓ Predict Output
advanced
1:30remaining
Effect of ! (NOT) operator on a logical vector
What is the output of this R code?
R Programming
flags <- c(TRUE, FALSE, TRUE, FALSE)
negated <- !flags
print(negated)
A[1] FALSE TRUE FALSE TRUE
B[1] TRUE FALSE TRUE FALSE
C[1] FALSE FALSE FALSE FALSE
D[1] TRUE TRUE TRUE TRUE
Attempts:
2 left
💡 Hint
The ! operator flips TRUE to FALSE and FALSE to TRUE.
❓ Predict Output
advanced
2:00remaining
Difference between & and && in conditional statements
What will this R code print?
R Programming
x <- c(TRUE, FALSE)
if (x & TRUE) {
  print("A")
} else {
  print("B")
}
if (x && TRUE) {
  print("C")
} else {
  print("D")
}
A
B
C
B
B
D
C
A
C
D
A
D
Attempts:
2 left
💡 Hint
Remember that if expects a single TRUE or FALSE, && returns a single logical, & returns a vector.
🧠 Conceptual
expert
1:30remaining
Understanding short-circuit behavior of && and || in R
Which statement best describes the behavior of the && and || operators in R?
A&& and || cannot be used in if statements because they return vectors.
B&& and || are identical to & and | in all contexts.
C&& and || evaluate all elements of logical vectors and return a vector of results.
D&& and || evaluate only the first element of logical vectors and perform short-circuit evaluation.
Attempts:
2 left
💡 Hint
Think about how && and || behave differently from & and | when used in conditions.

Practice

(1/5)
1. Which logical operator in R checks every element of a vector and returns a vector of results?
easy
A. ==
B. & and |
C. !
D. && and ||

Solution

  1. Step 1: Understand vectorized logical operators

    In R, & and | operate element-wise on vectors, returning a vector of TRUE/FALSE values for each element.
  2. Step 2: Compare with single-element operators

    && and || only check the first element of the vectors and return a single TRUE/FALSE value.
  3. Final Answer:

    & and | -> Option B
  4. Quick Check:

    Element-wise operators = & and | [OK]
Hint: Use & and | for vectors, && and || for single values [OK]
Common Mistakes:
  • Confusing && and & as the same
  • Using || for vector comparisons
  • Thinking ! works on multiple elements like &
2. Which of the following is the correct syntax to negate a logical vector v in R?
easy
A. !v
B. not(v)
C. ~v
D. v != TRUE

Solution

  1. Step 1: Identify the negation operator in R

    The logical NOT operator in R is !, which negates each element of a logical vector.
  2. Step 2: Check other options

    not() is not a base R function, ~ is a formula operator, and v != TRUE works but is less direct and less clear.
  3. Final Answer:

    !v -> Option A
  4. Quick Check:

    Negate vector with ! [OK]
Hint: Use ! to flip TRUE/FALSE values in R [OK]
Common Mistakes:
  • Using not() which is not a base R function
  • Confusing ~ with negation
  • Using != TRUE instead of !
3. What is the output of the following R code?
v1 <- c(TRUE, FALSE, TRUE)
v2 <- c(FALSE, FALSE, TRUE)
v1 & v2
v1 && v2
medium
A. [1] TRUE TRUE TRUE [1] TRUE
B. [1] TRUE FALSE TRUE [1] TRUE
C. [1] FALSE FALSE FALSE [1] FALSE
D. [1] FALSE FALSE TRUE [1] FALSE

Solution

  1. Step 1: Evaluate element-wise AND with &

    v1 & v2 compares each element:
    TRUE & FALSE = FALSE
    FALSE & FALSE = FALSE
    TRUE & TRUE = TRUE
    Result: [FALSE FALSE TRUE]
  2. Step 2: Evaluate single-element AND with &&

    v1 && v2 checks only first elements:
    TRUE && FALSE = FALSE
    Result: [FALSE]
  3. Final Answer:

    [1] FALSE FALSE TRUE [1] FALSE -> Option D
  4. Quick Check:

    & compares all elements, && only first [OK]
Hint: & works element-wise; && checks first element only [OK]
Common Mistakes:
  • Thinking && works element-wise
  • Mixing up & and && results
  • Ignoring vector length in &&
4. Identify the error in this R code snippet:
v <- c(TRUE, FALSE, TRUE)
if (v & TRUE) {
  print("Yes")
} else {
  print("No")
}
medium
A. Using & inside if condition causes error due to vector length
B. Missing parentheses around v & TRUE
C. Should use | instead of &
D. No error, code runs fine

Solution

  1. Step 1: Understand if condition requirements

    The if statement expects a single TRUE or FALSE value, not a vector.
  2. Step 2: Analyze v & TRUE

    v & TRUE returns a logical vector of length 3, which causes an error in if because it cannot handle multiple values.
  3. Final Answer:

    Using & inside if condition causes error due to vector length -> Option A
  4. Quick Check:

    if needs single TRUE/FALSE, & returns vector [OK]
Hint: Use && in if conditions for single TRUE/FALSE [OK]
Common Mistakes:
  • Using & instead of && in if
  • Assuming if accepts vector
  • Ignoring error message about length
5. You have two logical vectors:
a <- c(TRUE, FALSE, TRUE, FALSE)
b <- c(FALSE, TRUE, TRUE, TRUE)

Write an expression using logical operators to find elements where either a is TRUE and b is FALSE, or a is FALSE and b is TRUE. What is the resulting vector?
hard
A. a | b resulting in [TRUE TRUE TRUE TRUE]
B. a & b resulting in [FALSE FALSE TRUE FALSE]
C. (a & !b) | (!a & b) resulting in [TRUE TRUE FALSE TRUE]
D. !(a | b) resulting in [FALSE FALSE FALSE FALSE]

Solution

  1. Step 1: Understand the condition

    We want TRUE where exactly one of a or b is TRUE, but not both. This is the XOR condition.
  2. Step 2: Write expression and evaluate

    (a & !b) | (!a & b) checks where a is TRUE and b is FALSE, or vice versa.
    Evaluate:
    a: TRUE FALSE TRUE FALSE
    b: FALSE TRUE TRUE TRUE
    a & !b: TRUE FALSE FALSE FALSE
    !a & b: FALSE TRUE FALSE TRUE
    OR: TRUE TRUE FALSE TRUE
  3. Final Answer:

    (a & !b) | (!a & b) resulting in [TRUE TRUE FALSE TRUE] -> Option C
  4. Quick Check:

    XOR = (a & !b) | (!a & b) [OK]
Hint: Use (a & !b) | (!a & b) for XOR logic [OK]
Common Mistakes:
  • Using & or | alone instead of XOR logic
  • Forgetting to negate one vector
  • Confusing XOR with OR
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard