Challenge - 5 Problems
Assignment Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of compound assignment with addition
What is the output of the following R code?
R Programming
x <- 5 x += 3 print(x)
Attempts:
2 left
💡 Hint
R does not support the '+=' operator like some other languages.
✗ Incorrect
In R, the '+=' operator is not valid syntax. Using it causes an error because R expects '<-' or '=' for assignment.
❓ Predict Output
intermediate2:00remaining
Correct way to add and assign in R
Which option correctly adds 3 to variable x and assigns the result back to x in R?
R Programming
x <- 5 # Add 3 to x and assign back
Attempts:
2 left
💡 Hint
Use the standard assignment operator with an expression.
✗ Incorrect
In R, you add and assign by writing 'x <- x + 3'. The '+=' operator is not supported.
❓ Predict Output
advanced2:00remaining
Output of chained assignment
What is the output of this R code?
R Programming
a <- b <- 10 print(a) print(b)
Attempts:
2 left
💡 Hint
In R, assignment can be chained from right to left.
✗ Incorrect
The expression assigns 10 to b, then assigns b to a, so both become 10.
❓ Predict Output
advanced2:00remaining
Output of assignment with global environment
What is the output of this R code?
R Programming
x <- 5 f <- function() { x <<- 10 print(x) } f() print(x)
Attempts:
2 left
💡 Hint
The '<<-' operator assigns to the variable in the parent environment.
✗ Incorrect
Inside the function, 'x <<- 10' changes the global x to 10, so both prints show 10.
🧠 Conceptual
expert3:00remaining
Behavior of assignment operators in R environments
Which statement best describes the difference between '<-' and '<<-' assignment operators in R?
Attempts:
2 left
💡 Hint
Think about how R searches environments when assigning with '<<-'.
✗ Incorrect
The '<-' operator assigns in the current environment. The '<<-' operator looks up the parent environments and assigns to the first found variable or creates it in the global environment if none found.