0
0
R Programmingprogramming~20 mins

Debugging tools in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
R Debugging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1] 14
B[1] 6
CError in sum_squares(3) : object 'total' not found
D[1] 9
Attempts:
2 left
💡 Hint
Remember that the function sums squares of numbers from 1 to n.
Predict Output
intermediate
2: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")})
ANo output, program stops
BError in g(x) : Error occurred
C
1: stop("Error occurred")
[1] "Handled error"
D
3: g(x)
2: f(5)
[1] "Handled error"
Attempts:
2 left
💡 Hint
traceback() shows the call stack at the error point.
Predict Output
advanced
2: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)
A[1] 3
B[1] 2
CError: debug() requires a function argument
D[1] 0
Attempts:
2 left
💡 Hint
Check how debug() is used in R.
Predict Output
advanced
2: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)
ANo output, silently fails
BInteractive menu to choose a frame to debug
CProgram stops with error message 'Oops'
DPrints traceback and continues execution
Attempts:
2 left
💡 Hint
options(error = recover) triggers a debugging menu on error.
🧠 Conceptual
expert
3: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?
A<code>try()</code> suppresses errors and returns an object of class 'try-error' without stopping execution
B<code>try()</code> always stops execution on error and prints the error message
C<code>try()</code> converts errors into warnings that are printed immediately
D<code>try()</code> logs errors to a file but does not affect console output
Attempts:
2 left
💡 Hint
Think about how try() handles errors differently than stop().