R Programming Language Multiple Choice Questions on “Scoping Rules ”.
1. What will be the output of the following R code snippet?
> lm <- function(x) { x * x } > lm
a) function(x) { x * x }
b) func(x) { x * x }
c) function(x) { x / x }
d) function { x $ x }
Answer: a
Clarification: When R tries to bind a value to a symbol, it searches through a series of environments to find the appropriate value.
2. Point out the correct statement?
a) The search list can be found by using the searchlist() function
b) The search list can be found by using the search() function
c) The global environment or the user’s workspace is always the second element of the search list
d) R has separate namespaces for functions and non-functions
Answer: b
Clarification: Base package is always the last element.
3. A function, together with an environment, makes up what is called a ______ closure.
a) formal
b) function
c) reflective
d) symmetry
Answer: b
Clarification: The function closure model can be used to create functions that “carry around” data with them.
4. Which of the variable in the following R code is variable?
> f <- function(x, y) { + x^2 + y / z + }
a) x
b) y
c) z
d) yy
Answer: c
Clarification: This function has 2 formal arguments x and y. In the body of the function there is another symbol z. In this case z is called a free variable.
5. Point out the wrong statement?
a) The order of the packages on the search list does not matter
b) R has separate namespaces for functions and non-functions
c) Users can configure which packages get loaded on startup so if you are writing a function
d) The search list can be found by using the search() function
Answer: a
Clarification: The order of the packages on the search list matters, particularly if there are multiple objects with the same name in different packages.
6. R uses _________ scoping6 0 or static scoping.
a) reflective
b) transitive
c) lexical
d) closure
Answer: c
Clarification: Lexical scoping in R means that the values of free variables are searched for in the environment in which the function was defined.
7. The only environment without a parent is the ________ environment.
a) full
b) half
c) null
d) empty
Answer: d
Clarification: Every environment has a parent environment and it is possible for an environment to have multiple “children”.
8. The ________ for R are the main feature that make it different from the original S language.
a) scoping rules
b) closure rules
c) environment rules
d) closure & environment rules
Answer: a
Clarification: This function never actually uses the argument b, so calling f(2) will not produce an error because the 2 gets positionally matched to a.
9. The _________ function is a kind of “constructor function” that can be used to construct other functions.
a) make.pow()
b) make.power()
c) keep.power()
d) keep.pow()
Answer: b
Clarification: Typically, a function is defined in the global environment, so that the values of free variables are just found in the user’s workspace.
10. What will be the output of the following R code snippet?
> g <- function(x) { + a <- 3 + x+a+y + ## 'y' is a free variable + } > g(2)
a) 9
b) 42
c) 8
d) Error
Answer: d
Clarification: Object ‘y’ not found error is displayed.