Recall & Review
beginner
What is an environment in R?
An environment in R is like a box that stores variables and their values. It keeps track of where to find variables when you use them in your code.
Click to reveal answer
beginner
What is a closure in R?
A closure is a function that remembers the environment where it was created. It can use variables from that environment even after the environment is no longer active.
Click to reveal answer
intermediate
How does R find a variable inside nested functions?
R looks for the variable in the current environment first. If it is not found, it checks the parent environment, and keeps going up until it finds the variable or reaches the empty environment.
Click to reveal answer
intermediate
Explain why closures are useful with an example.
Closures let you create functions that keep some data inside. For example, a function that adds a fixed number to its input can remember that number inside its environment.Click to reveal answer
advanced
What happens if you change a variable inside a closure's environment?
Changing a variable inside a closure's environment updates the stored value. The closure will use the new value the next time it runs.
Click to reveal answer
What does an environment in R store?
✗ Incorrect
An environment stores variables and their values so R knows where to find them.
What is a closure in R?
✗ Incorrect
A closure is a function that keeps access to the environment where it was created.
If a variable is not found in the current environment, where does R look next?
✗ Incorrect
R searches the parent environment next, moving up until it finds the variable or reaches the global environment.
Why are closures useful?
✗ Incorrect
Closures keep data inside functions, allowing the function to remember values between calls.
What happens when you change a variable inside a closure's environment?
✗ Incorrect
Changing the variable updates the stored value, so the closure uses the new value.
Describe what an environment is in R and how it helps with variable lookup.
Think of it as a box where R keeps your variables.
You got /3 concepts.
Explain closures in R and why they are useful with a simple example.
Imagine a function that remembers a secret number to add.
You got /3 concepts.