0
0
R Programmingprogramming~20 mins

Return values in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this R function call?
Consider the following R function and its call. What value does it return?
R Programming
multiply_and_add <- function(x, y) {
  result <- x * y
  return(result + 5)
}
multiply_and_add(3, 4)
A17
B7
C12
D15
Attempts:
2 left
💡 Hint
Remember the function multiplies x and y first, then adds 5 before returning.
Predict Output
intermediate
2:00remaining
What does this R function return when called?
Look at this R function and its call. What is the returned value?
R Programming
check_even <- function(num) {
  if (num %% 2 == 0) {
    return(TRUE)
  } else {
    return(FALSE)
  }
}
check_even(7)
ATRUE
BNULL
C7
DFALSE
Attempts:
2 left
💡 Hint
The function returns TRUE if the number is even, otherwise FALSE.
Predict Output
advanced
2:00remaining
What is the output of this nested return function?
Analyze the following R code. What value does the function return when called with input 5?
R Programming
nested_return <- function(x) {
  inner <- function(y) {
    return(y * 2)
  }
  return(inner(x) + 3)
}
nested_return(5)
A10
B13
C15
D8
Attempts:
2 left
💡 Hint
The inner function doubles the input, then the outer adds 3.
Predict Output
advanced
2:00remaining
What does this function return when no explicit return is used?
What is the output of this R function call?
R Programming
no_return <- function(x) {
  y <- x + 2
  y * 3
}
no_return(4)
ANULL
B6
C18
D12
Attempts:
2 left
💡 Hint
In R, the last evaluated expression is returned if no return() is used.
Predict Output
expert
3:00remaining
What is the output of this recursive function call?
Consider this recursive R function. What value does factorial(4) return?
R Programming
factorial <- function(n) {
  if (n == 1) {
    return(1)
  } else {
    return(n * factorial(n - 1))
  }
}
factorial(4)
A24
B10
C16
D1
Attempts:
2 left
💡 Hint
Factorial multiplies n by factorial of n-1 until n is 1.