Bird
Raised Fist0
R Programmingprogramming~20 mins

Debugging tools in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 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().

Practice

(1/5)
1. Which R function allows you to step through a function line-by-line to find errors?
easy
A. traceback()
B. debug()
C. browser()
D. stop()

Solution

  1. Step 1: Understand the purpose of debug()

    The debug() function lets you step through a function line-by-line to see what happens inside.
  2. Step 2: Compare with other functions

    traceback() shows the call stack after an error, browser() pauses execution but debug() is designed for stepping through.
  3. Final Answer:

    debug() -> Option B
  4. Quick Check:

    Step through function = debug() [OK]
Hint: Use debug() to step through function code line-by-line [OK]
Common Mistakes:
  • Confusing traceback() with stepping through code
  • Thinking browser() automatically steps through all lines
  • Using stop() which just halts execution
2. Which of the following is the correct way to start debugging a function named myFunc in R?
easy
A. debug myFunc()
B. debug:myFunc()
C. debug->myFunc()
D. debug(myFunc)

Solution

  1. Step 1: Recall correct debug() syntax

    The debug() function is called with the function name as an argument: debug(myFunc).
  2. Step 2: Identify invalid syntax

    Options A, B, and D use invalid R syntax for function calls or operators.
  3. Final Answer:

    debug(myFunc) -> Option D
  4. Quick Check:

    Correct debug syntax = debug(myFunc) [OK]
Hint: Use parentheses with debug() and function name [OK]
Common Mistakes:
  • Omitting parentheses after debug
  • Using colon or arrow instead of parentheses
  • Adding spaces incorrectly in function call
3. What will be the output after running this R code?
f <- function(x) {
  debug()
  y <- x + 1
  return(y)
}
f(3)
medium
A. Starts debugging mode and waits for user input
B. Returns 4 immediately without debugging
C. Throws an error because debug() needs a function name
D. Prints 3 and exits

Solution

  1. Step 1: Understand debug() usage

    debug() is used with a function name outside the function, not inside it.
  2. Step 2: Analyze code behavior

    Calling debug() inside a function without arguments causes an error because debug expects a function name.
  3. Final Answer:

    Throws an error because debug() needs a function name -> Option C
  4. Quick Check:

    debug() inside function without name = error [OK]
Hint: debug() needs a function name, not called inside function body [OK]
Common Mistakes:
  • Thinking debug() works inside function without arguments
  • Expecting immediate return without debugging
  • Confusing debug() with browser()
4. You run a function and get an error. Which R command helps you see the sequence of function calls that led to the error?
medium
A. traceback()
B. debug()
C. browser()
D. stop()

Solution

  1. Step 1: Identify purpose of traceback()

    traceback() shows the call stack, listing functions called before the error.
  2. Step 2: Differentiate from other tools

    debug() and browser() help step through code, stop() halts execution but does not show call history.
  3. Final Answer:

    traceback() -> Option A
  4. Quick Check:

    Call stack after error = traceback() [OK]
Hint: Use traceback() to see error call sequence [OK]
Common Mistakes:
  • Using debug() to see call stack after error
  • Confusing browser() with traceback()
  • Expecting stop() to show call history
5. You want to debug a complex function calcStats that calls other functions internally. Which approach helps you pause execution at any line inside calcStats and inspect variables interactively?
hard
A. Use browser() inside calcStats at desired lines
B. Use debug(calcStats) only once before calling
C. Use traceback() after running calcStats
D. Use stop() to halt execution at errors

Solution

  1. Step 1: Understand browser() usage

    Inserting browser() pauses execution at that line and lets you inspect variables interactively.
  2. Step 2: Compare with debug() and traceback()

    debug() steps through entire function but cannot pause at arbitrary lines; traceback() only shows call stack after error.
  3. Step 3: Recognize stop() role

    stop() halts execution with an error but does not allow interactive inspection.
  4. Final Answer:

    Use browser() inside calcStats at desired lines -> Option A
  5. Quick Check:

    Pause anywhere interactively = browser() [OK]
Hint: Insert browser() where you want to pause and inspect [OK]
Common Mistakes:
  • Relying only on debug() to pause anywhere
  • Using traceback() before error occurs
  • Expecting stop() to allow variable inspection
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