The questions relate to the call stack section in the lesson Creating Functions.
The two questions probe understanding of how variables are used across environments in R
Question 1
After running the following code, what will R return (answer without running)?
a<-10
b<-20
function1<-function(){
a<-1
b<-2
c(a,b)
}
c(function1(),a,b)
- 10,20,10,20
- 1,2,1,2
- 10,20,1,2
- 1,2,10,20
Question 2
After running the following code, what will R return (answer without running)?
rm(a,b)
a<-10
b<-20
function2<-function(){
a<-1
c(a,b)
}
c(function2(),a,b)
- 10,20,10,20
- 1,2,10,20
- R will produce an error - Error: object ‘b’ not found
- 1,20,10,20
Discussion of Question 2
-
This will test the understanding of the function first looking in its own environment for variables
-
This answer is a red herring using the previous question - if the student has not noticed b was not defined in the new function, they will choose this
-
This tests the students understanding of how R searches for variables - if they dont understand it goes up in environments, they will choose this
-
This is the correct answer.