Week 2 Quiz:R Programming(Data Science Specialization):Answers2025
Question 1
Suppose I define the following function:
cube <- function(x, n) {
x^3
}
cube(3)
✅ The number 27 is returned
❌ An error is returned because ‘n’ is not specified
❌ The user is prompted to specify ‘n’
❌ A warning is given
Explanation:
The function ignores n. It simply computes x^3, so cube(3) → 3³ = 27.
Question 2
x <- 1:10
if(x > 5) {
x <- 0
}
✅ ‘x’ is a vector of length 10 and ‘if’ can only test a single logical statement.
❌ Syntax incorrect
❌ No elements greater than 5
❌ Can’t assign scalar to vector
Explanation:if() expects a single TRUE/FALSE value, not a logical vector. Hence R issues a warning.
Question 3
f <- function(x) {
g <- function(y) {
y + z
}
z <- 4
x + g(x)
}
z <- 10
f(3)
✅ 10
❌ 7
❌ 16
❌ 4
Explanation:
Inside f, z = 4 (local variable).g(x) = 3 + 4 = 7; then x + g(x) = 3 + 7 = 10.
Outer z=10 is ignored due to lexical scoping.
Question 4
x <- 5
y <- if(x < 3) {
NA
} else {
10
}
✅ 10
❌ 5
❌ 3
❌ NA
Explanation:x < 3 is FALSE, so the else block runs — y = 10.
Question 5
Function:
h <- function(x, y = NULL, d = 3L) {
z <- cbind(x, d)
if(!is.null(y))
z <- z + y
else
z <- z + f
g <- x + y / z
if(d == 3L)
return(g)
g <- g + 10
}
✅ f
❌ z
❌ d
❌ g
❌ L
Explanation:f is referenced but never defined inside or passed as argument — hence it’s a free variable.
Question 6
✅ A collection of symbol/value pairs
❌ A list of functions
❌ A special type of function
❌ An R package
Explanation:
An environment is a collection (like a map) of variable names (symbols) bound to values.
Question 7
✅ Lexical scoping
❌ Global
❌ Dynamic
❌ Compilation
Explanation:
R uses lexical (static) scoping — free variables are resolved from where the function was defined, not called.
Question 8
✅ The values of free variables are searched for in the environment in which the function was defined.
❌ Environment where called
❌ Working directory
❌ Global environment
Explanation:
Lexical scoping means lookup happens in the defining environment, not the calling one.
Question 9
✅ All objects must be stored in memory
❌ Cannot be >100 MB
❌ Functions cannot be nested
❌ All objects can be stored on disk
Explanation:
R primarily keeps objects in-memory while running (exceptions use external memory techniques).
Question 10
✅ It is the environment in which a function was called.
❌ Always global
❌ Package search list
❌ Environment where defined
Explanation:
The parent frame refers to the calling environment, not the defining environment.
🧾 Summary Table
| Q# | ✅ Correct Answer | Key Concept |
|---|---|---|
| 1 | The number 27 | Function ignores unused arg |
| 2 | if only tests single logical |
Vector vs scalar logic |
| 3 | 10 | Lexical scoping, local variable |
| 4 | 10 | if-else evaluation |
| 5 | f | Free variable concept |
| 6 | Symbol/value pairs | Environment structure |
| 7 | Lexical scoping | Variable resolution rule |
| 8 | Searched where defined | Function environment |
| 9 | Stored in memory | Memory model of R |
| 10 | Called environment | Parent frame meaning |