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.
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?
Consider the function defined in the previous question.
What value would f(NULL)
return?
NULL
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, since vec
is empty.
This is incorrect. I had in mind that a student might think that v
would take the value NULL
and that NULL <= threshold
might be interpreted as TRUE
.
I guess this is the "fish" answer. Writing these MCQs is hard.