The function add takes two inputs and returns their sum.
Step 2: Calculate the function call
Calling add(3, 4) returns 3 + 4 = 7, which is stored in result and printed.
Final Answer:
7 -> Option A
Quick Check:
3 + 4 = 7 [OK]
Hint: Functions return values; print shows the result [OK]
Common Mistakes:
Thinking the function concatenates numbers as strings
Expecting an error due to missing parentheses
Assuming the function prints automatically without print()
4. Identify the error in this R function definition:
multiply <- function(x, y) {
product = x * y
return product
}
medium
A. Function name cannot be multiply
B. Using = instead of <- for assignment
C. No error, code is correct
D. Missing parentheses in return statement
Solution
Step 1: Check the return statement syntax
In R, return must be followed by parentheses enclosing the value to return.
Step 2: Identify the error
The code uses return product without parentheses, which causes a syntax error.
Final Answer:
Missing parentheses in return statement -> Option D
Quick Check:
return() needs parentheses [OK]
Hint: Always use return(value) with parentheses in R [OK]
Common Mistakes:
Omitting parentheses in return
Confusing assignment operators
Thinking function names are restricted
5. You want to write a function in R that calculates the average of a numeric vector and returns it. Which of these function definitions correctly organizes the code to do this?
hard
A. average <- function(nums) {
return(sum(nums) / length(nums))
}
B. average <- function(nums) {
for (n in nums) {
sum <- sum + n
}
return(sum / length(nums))
}
C. average <- function(nums) {
mean <- sum(nums) * length(nums)
return(mean)
}
D. average <- function(nums) {
for (n in nums) {
sum <- sum + n
}
return(sum)
}
Solution
Step 1: Analyze each function's logic
average <- function(nums) {
for (n in nums) {
sum <- sum + n
}
return(sum / length(nums))
} attempts to manually sum numbers but does not initialize sum, causing an error on undefined sum. average <- function(nums) {
return(sum(nums) / length(nums))
} uses built-in functions sum() and length() correctly. average <- function(nums) {
mean <- sum(nums) * length(nums)
return(mean)
} multiplies sum by length, which is wrong. average <- function(nums) {
for (n in nums) {
sum <- sum + n
}
return(sum)
} sums but never initializes sum and returns sum without averaging.
Step 2: Choose the correct function
average <- function(nums) {
return(sum(nums) / length(nums))
} is concise, uses built-in functions, and returns the correct average.
Final Answer:
average <- function(nums) { return(sum(nums) / length(nums)) } -> Option A
Quick Check:
Use built-in sum and length for average [OK]
Hint: Use sum(nums)/length(nums) for average in one line [OK]
Common Mistakes:
Forgetting to initialize sum before loop
Multiplying sum by length instead of dividing
Returning sum without dividing by length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.