Challenge - 5 Problems
Logical Operator Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that & works element-wise on vectors, while && only checks the first element.
✗ Incorrect
The & operator compares each element of x and y, producing a vector of logicals. The && operator only compares the first element of each vector, returning a single logical value.
❓ Predict Output
intermediate1: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)Attempts:
2 left
💡 Hint
The | operator compares each element and returns TRUE if either is TRUE.
✗ Incorrect
Element-wise OR returns TRUE if at least one element in the pair is TRUE. So positions: FALSE|TRUE=TRUE, FALSE|FALSE=FALSE, TRUE|FALSE=TRUE.
❓ Predict Output
advanced1: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)Attempts:
2 left
💡 Hint
The ! operator flips TRUE to FALSE and FALSE to TRUE.
✗ Incorrect
The NOT operator reverses each logical value in the vector.
❓ Predict Output
advanced2: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") }
Attempts:
2 left
💡 Hint
Remember that if expects a single TRUE or FALSE, && returns a single logical, & returns a vector.
✗ Incorrect
The if with (x & TRUE) produces c(TRUE, FALSE), R warns and uses the first element TRUE, printing "A". The if with (x && TRUE) evaluates first elements TRUE && TRUE = TRUE, printing "C".
🧠 Conceptual
expert1:30remaining
Understanding short-circuit behavior of && and || in R
Which statement best describes the behavior of the && and || operators in R?
Attempts:
2 left
💡 Hint
Think about how && and || behave differently from & and | when used in conditions.
✗ Incorrect
In R, && and || only check the first element of logical vectors and stop evaluating further if the result is already determined (short-circuit). This makes them suitable for control flow statements like if.