0
0
R Programmingprogramming~10 mins

Variable scope (lexical scoping) in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign the value 10 to variable x.

R Programming
x <- [1]
Drag options to blanks, or click blank then click option'
ATRUE
B"x"
C10
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around 10, which makes it a string instead of a number.
Using TRUE or NULL instead of a number.
2fill in blank
medium

Complete the function to return the value of y defined outside the function.

R Programming
y <- 5
my_func <- function() {
  return([1])
}
Drag options to blanks, or click blank then click option'
Ay
Bx
Cz
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a variable not defined outside the function.
Returning a literal number instead of the variable.
3fill in blank
hard

Fix the error by completing the code to access the outer variable a inside the function.

R Programming
a <- 3
outer_func <- function() {
  inner_func <- function() {
    return([1])
  }
  inner_func()
}
Drag options to blanks, or click blank then click option'
Aa
Binner_func
C3
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not defined in any scope.
Returning the function name instead of a variable.
4fill in blank
hard

Complete the function to compute compound interest using the outer 'rate' variable.

R Programming
rate <- 1.05
compound_interest <- function(principal) {
  return([1])
}
Drag options to blanks, or click blank then click option'
Aprincipal * 1.05
Bprincipal * rate
Cprincipal + rate
Drate * principal + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Hardcoding the rate value like 1.05.
Adding instead of multiplying.
5fill in blank
hard

Complete the function to use the outer 'exponent' despite the local shadowing variable.

R Programming
exponent <- 3
power_calc <- function(base) {
  exponent_local <- 2
  return([1])
}
Drag options to blanks, or click blank then click option'
Aexponent ^ base
Bbase ^ exponent_local
Cbase ** 3
Dbase ^ exponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using the local shadowed variable exponent_local.
Hardcoding the exponent value.