0
0
R Programmingprogramming~20 mins

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

Choose your learning style9 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.