Recall & Review
beginner
What is lexical scoping in R?
Lexical scoping means that R looks for a variable's value in the environment where the function was defined, not where it is called.
Click to reveal answer
beginner
In R, where does the interpreter look first when resolving a variable inside a function?
It first looks inside the function's own environment (local environment) for the variable.
Click to reveal answer
intermediate
What happens if a variable is not found in the local environment in R?
R looks in the parent environment, continuing up the chain until it finds the variable or reaches the empty environment.
Click to reveal answer
beginner
Explain the difference between
local and global variables in R.Local variables exist only inside a function and are not visible outside. Global variables exist in the global environment and can be accessed anywhere unless shadowed by local variables.
Click to reveal answer
intermediate
What is the output of this R code?<br>
f <- function() { x <- 10; function() x }<br>Then calling g <- f() and g()?The output is 10. The inner function remembers the value of
x from the environment where it was created (lexical scoping).Click to reveal answer
In R, if a variable is not found inside a function, where does R look next?
✗ Incorrect
R searches the parent environment if the variable is not found locally, following lexical scoping rules.
What does lexical scoping depend on in R?
✗ Incorrect
Lexical scoping means variable lookup depends on the environment where the function was defined.
Which environment contains variables defined inside a function in R?
✗ Incorrect
Variables inside a function live in the local environment specific to that function call.
What will happen if a variable is not found in any environment in R?
✗ Incorrect
If R cannot find a variable in any environment, it throws an error indicating the variable is not found.
What is a closure in R related to lexical scoping?
✗ Incorrect
A closure is a function that captures and remembers the environment where it was created.
Explain how R finds the value of a variable inside a function using lexical scoping.
Think about the chain of environments R checks.
You got /5 concepts.
Describe the difference between local and global variables in R and how lexical scoping affects their visibility.
Consider where variables are created and accessed.
You got /4 concepts.