MCQs for testing R function knowledge

Mar 7, 2015 • Will Cornwell

Two MCQs to test knowledge about R functions.

In addition to the SWC R function lesson, some ideas for these questions derived from the Rshiny tutorial and also from Hadley Wickham’s excellent introduction to R functions. I really wish someone had taught me about R functions as a grad student–would have saved me years of frustration.

MCQ #1

What will this code return?

x <- 1
f <- function() {
  y <- 2
  return(c(x, y))


a) an error message because x is not defined within the function

b) one number: 12

c) a vector with two elements 1 and 2

d) a vector with two elements "x" and "y"

MCQ #2

What will this code print to screen?

f <- function() {
  return(c(x, y))


a) a vector with two elements 3 and 6

b) 6

c) a vector with two elements "x" and 3

d) An error message saying object y not found

misconceptions corresponding to each wrong answer:

a) Confusion about reading functions in versus calling them

b) Confusion about scoping esp. what happens to variables created within functions

c) Confusion about both reading in functions and about variable assignment

d) Correct answer