These multiple choice questions relate to the lessons on functions in R, loops in R, and conditional statements in R.
I had in mind that these might go at the end of the lesson on
conditional statements and might lead to discussion of statements like
sum(x <= 10)
. Maybe there’s too much going on here.
Question 1
Consider the following function.
f <-
function(vec, threshold=10)
{
number <- 0
for(v in vec) {
if(v <= threshold)
number <- number + 1
}
return(number)
}
What value would f(c(3, 10, 5, 8, 28), 5)
return?
- 1
- 2
- 3
- 4
Question 2
Consider the function defined in the previous question.
What value would f(NULL)
return?
- This will give an error.
- 0
- 1
NULL
Discussion of Question 2
-
This is incorrect. I had in mind that a student might thing that a
for
loop wouldn’t work with an empty set of values. -
This is the correct answer. The
for
loop ends up getting skipped, sincevec
is empty. -
This is incorrect. I had in mind that a student might think that
v
would take the valueNULL
and thatNULL <= threshold
might be interpreted asTRUE
. -
I guess this is the “fish” answer. Writing these MCQs is hard.