Bird
Raised Fist0
R Programmingprogramming~20 mins

Logical (boolean) type in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logical Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of logical operations in R
What is the output of the following R code?
R Programming
x <- c(TRUE, FALSE, TRUE)
y <- c(FALSE, FALSE, TRUE)
result <- x & y
print(result)
A[1] TRUE TRUE TRUE
B[1] TRUE FALSE TRUE
C[1] FALSE FALSE TRUE
D[1] FALSE TRUE FALSE
Attempts:
2 left
💡 Hint
Remember that '&' is element-wise AND in R.
❓ Predict Output
intermediate
2:00remaining
Result of logical negation in R
What will be printed by this R code?
R Programming
flag <- c(TRUE, FALSE, FALSE, TRUE)
neg_flag <- !flag
print(neg_flag)
A[1] FALSE FALSE FALSE FALSE
B[1] FALSE TRUE TRUE FALSE
C[1] TRUE FALSE FALSE TRUE
D[1] TRUE TRUE TRUE TRUE
Attempts:
2 left
💡 Hint
The '!' operator flips TRUE to FALSE and vice versa.
❓ Predict Output
advanced
2:00remaining
Logical vector recycling in R
What is the output of this R code snippet?
R Programming
a <- c(TRUE, FALSE)
b <- c(TRUE, TRUE, FALSE, FALSE)
result <- a | b
print(result)
A[1] TRUE FALSE TRUE FALSE
B[1] TRUE TRUE TRUE TRUE
C[1] TRUE TRUE FALSE FALSE
D[1] TRUE TRUE TRUE FALSE
Attempts:
2 left
💡 Hint
Remember R recycles shorter vectors to match the length of longer vectors.
❓ Predict Output
advanced
2:00remaining
Logical indexing with boolean vectors in R
What will this R code print?
R Programming
values <- c(10, 20, 30, 40, 50)
filter <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
selected <- values[filter]
print(selected)
A[1] 10 30 50
B[1] 20 40
C[1] 10 20 30 40 50
D[1] 30 50
Attempts:
2 left
💡 Hint
Logical vectors can be used to pick elements from another vector.
🧠 Conceptual
expert
2:00remaining
Understanding the difference between '&' and '&&' in R
Which statement best describes the difference between '&' and '&&' in R?
A'&' performs element-wise logical AND on vectors; '&&' evaluates only the first element of each vector.
B'&' performs logical OR; '&&' performs logical AND.
C'&' and '&&' are interchangeable and produce the same result for all inputs.
D'&' is used only for single logical values; '&&' works on vectors element-wise.
Attempts:
2 left
💡 Hint
Think about how R treats vectorized logical operations versus single comparisons.

Practice

(1/5)
1. What type of values does the logical type in R store?
easy
A. Numbers only
B. TRUE or FALSE
C. Text strings
D. Dates and times

Solution

  1. Step 1: Understand logical type meaning

    Logical type in R is used to store values that represent truth values.
  2. Step 2: Identify stored values

    These truth values are either TRUE or FALSE, not numbers, text, or dates.
  3. Final Answer:

    TRUE or FALSE -> Option B
  4. Quick Check:

    Logical type = TRUE or FALSE [OK]
Hint: Logical means true or false values only [OK]
Common Mistakes:
  • Confusing logical with numeric type
  • Thinking logical stores text
  • Assuming logical stores dates
2. Which of the following is the correct way to create a logical vector in R?
easy
A. c('TRUE', 'FALSE')
B. c(1, 0, 1)
C. c(TRUE, FALSE, TRUE)
D. c(T, F, 'TRUE')

Solution

  1. Step 1: Check vector creation syntax

    Logical vectors use TRUE or FALSE values without quotes inside c().
  2. Step 2: Validate each option

    c(TRUE, FALSE, TRUE) uses proper logical values TRUE and FALSE. c('TRUE', 'FALSE') creates character vector. c(1, 0, 1) creates numeric vector. c(T, F, 'TRUE') mixes logical shortcuts T, F with string 'TRUE' coercing to character vector.
  3. Final Answer:

    c(TRUE, FALSE, TRUE) -> Option C
  4. Quick Check:

    Logical vector uses TRUE/FALSE without quotes [OK]
Hint: Use TRUE/FALSE without quotes for logical vectors [OK]
Common Mistakes:
  • Using numbers instead of logical values
  • Putting TRUE/FALSE in quotes
  • Mixing logical and string types
3. What is the output of this R code?
result <- 5 > 3
print(result)
medium
A. TRUE
B. "TRUE"
C. FALSE
D. Error

Solution

  1. Step 1: Evaluate the comparison 5 > 3

    Since 5 is greater than 3, the expression returns TRUE as a logical value.
  2. Step 2: Understand print output

    print() outputs the logical value TRUE without quotes.
  3. Final Answer:

    TRUE -> Option A
  4. Quick Check:

    5 > 3 = TRUE [OK]
Hint: Comparison returns logical TRUE or FALSE, not strings [OK]
Common Mistakes:
  • Thinking output is string "TRUE"
  • Confusing TRUE with FALSE
  • Expecting an error from comparison
4. Identify the error in this R code snippet:
flag <- TRUE
if flag == TRUE {
  print("Yes")
}
medium
A. Variable flag is not defined
B. Incorrect logical value TRUE
C. print() function is wrong
D. Missing parentheses around condition in if statement

Solution

  1. Step 1: Check if statement syntax in R

    In R, the condition in if() must be inside parentheses.
  2. Step 2: Identify missing parentheses

    The code uses if flag == TRUE without parentheses, causing syntax error.
  3. Final Answer:

    Missing parentheses around condition in if statement -> Option D
  4. Quick Check:

    if() needs parentheses [OK]
Hint: Always put condition inside parentheses in if() [OK]
Common Mistakes:
  • Omitting parentheses in if condition
  • Misusing TRUE value
  • Assuming print() syntax is wrong
5. Given a numeric vector nums <- c(0, 1, 2, 3, 0), which R expression correctly creates a logical vector indicating which elements are non-zero?
hard
A. nums != 0
B. nums == TRUE
C. is.logical(nums)
D. nums > TRUE

Solution

  1. Step 1: Understand the goal

    We want a logical vector showing TRUE where elements are not zero.
  2. Step 2: Evaluate each option

    nums != 0 returns TRUE for non-zero elements. nums == TRUE compares to 1 (TRUE coerces to 1), TRUE only where nums==1. is.logical(nums) checks type of nums, returns single FALSE since it's numeric. nums > TRUE compares to 1, TRUE only where nums > 1.
  3. Final Answer:

    nums != 0 -> Option A
  4. Quick Check:

    nums != 0 gives logical TRUE for non-zero [OK]
Hint: Use != 0 to find non-zero elements logically [OK]
Common Mistakes:
  • Using == TRUE to compare numbers
  • Using is.logical() to check or create logical vector
  • Comparing numbers directly to TRUE
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