Bird
Raised Fist0
R Programmingprogramming~20 mins

Variable scope (lexical scoping) in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Lexical Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested function with lexical scope
What is the output of this R code?
R Programming
x <- 10
f <- function() {
  x <- 20
  g <- function() {
    x
  }
  g()
}
f()
A[1] 20
B[1] 10
CError: object 'x' not found
D[1] NULL
Attempts:
2 left
💡 Hint
Remember that inner functions use variables from their defining environment unless shadowed.
❓ Predict Output
intermediate
2:00remaining
Value of variable after function call
What is the value of x after running this code?
R Programming
x <- 5
foo <- function() {
  x <- 10
}
foo()
x
A[1] 10
B[1] 5
CNULL
DError: object 'x' not found
Attempts:
2 left
💡 Hint
Does the function foo() change the global x or only a local copy?
❓ Predict Output
advanced
2:00remaining
Output of function modifying global variable with <<-
What is the output of this code?
R Programming
x <- 1
modify <- function() {
  x <<- 2
}
modify()
x
ANULL
B[1] 1
CError: object 'x' not found
D[1] 2
Attempts:
2 left
💡 Hint
The <<- operator assigns to a variable in the parent environment.
❓ Predict Output
advanced
2:00remaining
Error caused by variable scope in nested functions
What error does this code produce?
R Programming
outer <- function() {
  inner <- function() {
    y
  }
  inner()
}
outer()
AError: object 'y' not found
BNULL
C[1] 0
D[1] NA
Attempts:
2 left
💡 Hint
Is y defined anywhere in the function or global environment?
🧠 Conceptual
expert
3:00remaining
How many variables are in the environment after execution?
Consider this code. How many variables are in the environment of function f after it runs?
R Programming
f <- function() {
  a <- 1
  b <- 2
  g <- function() {
    c <- 3
    a + b + c
  }
  g()
}
A4 variables: a, b, c, and g
B3 variables: a, b, and c
C2 variables: a and b
D1 variable: g
Attempts:
2 left
💡 Hint
Variables inside g() exist only during g() execution, not in f()'s environment after g() finishes.

Practice

(1/5)
1. What does lexical scoping mean in R programming?
easy
A. Variables are looked up where the function is defined, not where it is called.
B. Variables are always global and accessible everywhere.
C. Variables inside functions overwrite global variables automatically.
D. Variables are found only inside loops.

Solution

  1. Step 1: Understand lexical scoping definition

    Lexical scoping means R looks for variables in the environment where the function was written, not where it is called.
  2. Step 2: Compare options with definition

    Only Variables are looked up where the function is defined, not where it is called. correctly describes this behavior; others are incorrect or unrelated.
  3. Final Answer:

    Variables are looked up where the function is defined, not where it is called. -> Option A
  4. Quick Check:

    Lexical scoping = lookup where defined [OK]
Hint: Remember: R finds variables where code is written, not run [OK]
Common Mistakes:
  • Thinking variables are always global
  • Confusing lexical with dynamic scoping
  • Assuming function variables overwrite globals automatically
2. Which of the following is the correct way to define a local variable x inside a function in R?
easy
A. global x = 5
B. x <- 5 inside the function body
C. var x = 5
D. x := 5

Solution

  1. Step 1: Recall R syntax for variable assignment

    In R, variables are assigned using the arrow operator (<-), especially inside functions.
  2. Step 2: Check each option

    x <- 5 inside the function body uses correct R syntax. Options B, C, and D use syntax from other languages or invalid forms.
  3. Final Answer:

    x <- 5 inside the function body -> Option B
  4. Quick Check:

    Use <- for assignment in R [OK]
Hint: Use <- to assign variables inside functions in R [OK]
Common Mistakes:
  • Using global or var keywords (not R syntax)
  • Using := which is not valid in base R
  • Confusing R with other languages like JavaScript or Python
3. What is the output of this R code?
y <- 10
f <- function() {
  y <- 5
  return(y)
}
f()
medium
A. 10
B. NULL
C. 5
D. Error

Solution

  1. Step 1: Identify variable scopes

    Global y is 10, but inside function f, y is locally assigned 5.
  2. Step 2: Determine returned value

    The function returns the local y, which is 5, not the global y.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    Local variable y inside function = 5 [OK]
Hint: Inside function, local variables override globals [OK]
Common Mistakes:
  • Assuming function returns global y (10)
  • Thinking function returns NULL by default
  • Expecting an error due to variable shadowing
4. Consider this R code:
z <- 3
f <- function() {
  z <- z + 1
  return(z)
}
f()

What is the result?
medium
A. 4
B. 3
C. NULL
D. Error: object 'z' not found

Solution

  1. Step 1: Analyze variable usage inside function

    Inside f, z <- z + 1 tries to read local z before assignment, but local z doesn't exist yet.
  2. Step 2: Understand lexical scoping rules

    R looks for z in local scope first; it is not found, so it does not use global z for the right side in assignment.
  3. Step 3: Resulting error

    This causes an error: object 'z' not found.
  4. Final Answer:

    Error: object 'z' not found -> Option D
  5. Quick Check:

    Local variable used before assignment causes error [OK]
Hint: Local vars must exist before use; else error occurs [OK]
Common Mistakes:
  • Assuming global z is used automatically
  • Expecting output 4 without error
  • Ignoring variable initialization order
5. Given the code below, what will g() return?
a <- 2
f <- function() {
  a <- 3
  h <- function() {
    a <- 4
    return(a)
  }
  return(h())
}
g <- function() {
  return(f())
}
g()
hard
A. 4
B. 3
C. Error
D. 2

Solution

  1. Step 1: Trace nested function variable scopes

    Global a = 2, f() sets local a = 3, h() inside f sets local a = 4.
  2. Step 2: Determine return value of g()

    g() calls f(), which calls h(), which returns its local a = 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Innermost local variable a = 4 returned [OK]
Hint: Innermost function's local variable shadows outer ones [OK]
Common Mistakes:
  • Returning outer function's a (3) instead of inner (4)
  • Returning global a (2)
  • Expecting an error due to nested functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard