Challenge - 5 Problems
R Debugging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of debugged function with browser()
Consider the following R function that uses
browser() for debugging. What will be the output when calling sum_squares(3)?R Programming
sum_squares <- function(n) {
browser()
total <- 0
for (i in 1:n) {
total <- total + i^2
}
return(total)
}
sum_squares(3)Attempts:
2 left
💡 Hint
Remember that the function sums squares of numbers from 1 to n.
✗ Incorrect
The function sums 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14. The browser() pauses execution but does not change the output.
❓ Predict Output
intermediate2:00remaining
Effect of traceback() after error
What will be the output of the following R code snippet?
R Programming
f <- function(x) {
g(x)
}
g <- function(y) {
stop("Error occurred")
}
tryCatch(f(5), error = function(e) {traceback(); print("Handled error")})Attempts:
2 left
💡 Hint
traceback() shows the call stack at the error point.
✗ Incorrect
traceback() prints the call stack showing g called by f, then the error handler prints "Handled error".
❓ Predict Output
advanced2:00remaining
Output of debug() with step-through
Given the code below, what will be printed when
debugged_sum(2) is called and the user steps through the function until it returns?R Programming
debugged_sum <- function(n) {
debug()
s <- 0
for (i in 1:n) {
s <- s + i
}
return(s)
}
debugged_sum(2)Attempts:
2 left
💡 Hint
Check how debug() is used in R.
✗ Incorrect
debug() must be called with a function name as argument, calling debug() inside a function without argument causes an error.
❓ Predict Output
advanced2:00remaining
Behavior of options(error = recover)
What happens when you run the following code and an error occurs?
R Programming
options(error = recover)
f <- function(x) {
g(x)
}
g <- function(y) {
stop("Oops")
}
f(1)Attempts:
2 left
💡 Hint
options(error = recover) triggers a debugging menu on error.
✗ Incorrect
When an error occurs, recover() lets you select which function frame to inspect interactively.
🧠 Conceptual
expert3:00remaining
Identifying the cause of silent failure in R script
You run an R script that uses
try() to catch errors but notice that some errors do not appear and the script continues silently. Which of the following best explains this behavior?Attempts:
2 left
💡 Hint
Think about how try() handles errors differently than stop().
✗ Incorrect
try() catches errors and returns an object indicating failure but does not stop the script, so errors may be silent unless checked.