250+ TOP MCQs on Scoping Rules and Answers

R Programming online test focuses on “Scoping Rules”.

1. ________ functions can be “built which contain all of the necessary data for evaluating the function.
a) Objective
b) reflective
c) Nested
d) Symmetry

Answer: a
Clarification: However, an objective function that needs to be minimized might depend on a host of other things besides its parameters.

2. Point out the wrong statement?
a) Dynamic scoping turns out to be particularly useful for simplifying statistical computations
b) Lexical scoping turns out to be particularly useful for simplifying statistical computations
c) The scoping rules of a language determine how values are assigned to free variables
d) An environment is a collection of (symbol, value) pairs, i.e. x is a symbol and 3.14 might be its value

Answer: a
Clarification: Free variables are not formal arguments and are not local variables (assigned inside the function body).

3. Which of the following language supports lexical scoping?
a) Perl
b) Python
c) Common Lisp
d) All of the mentioned

Answer: d
Clarification: Lexical scoping in R has consequences beyond how free variables are looked up.

4. _________ require you to pass a function whose argument is a vector of parameters.
a) optimize()
b) optimise()
c) opt()
d) opm()

Answer: c
Clarification: Optimization routines in R like optim(), nlm(), and optimize() require you to pass a function.

5. Point out the correct statement?
a) An environment is a collection of (symbol, value) pairs, i.e. x is a symbol and 3.14 might be its value
b) If the value of a symbol is not found in the environment in which a function was defined, then the search is continued in the child environment
c) After the top-level environment, the search continues down the search list until we hit the parent environment
d) Dynamic scoping turns out to be particularly useful for simplifying statistical computations

  250+ TOP MCQs on Textual Data Formats and Answers

Answer: a
Clarification: Every environment has a parent environment and it is possible for an environment to have multiple “children”.

6. What will be the output of the following R code?

function(p) {
          params[!fixed] <- p
          mu <- params[1]
          sigma <- params[2]
## Calculate the Normal density
          a <- -0.5*length(data)*log(2*pi*sigma^2)
          b <- -0.5*sum((data-mu)^2) / (sigma^2)
          -(a + b)
}
> ls(environment(nLL))

a) “data” “fixed” “param”
b) “data” “variable” “params”
c) “data” “fixed” “params”
d) “data” “param”

Answer: c
Clarification: nLL is one of the optimization functions in R.

7. What will be the output of the following R code?

> nLL <- make.NegLogLik(normals, c(FALSE, 2))
> optimize(nLL, c(-1, 3))$minimum

a) 1.217775
b) 2.217775
c) 3
d) empty

Answer: d
Clarification: optimize() function can be used rather than optim().

8. What will be the output of the following R code?

> nLL <- make.NegLogLik(normals, c(1, FALSE))
> optimize(nLL, c(1e-6, 10))$minimum

a) 1.217775
b) 1.800596
c) 3.73424
d) empty

Answer: b
Clarification: Sigma is estimated by holding mu fixed at 1.

9. The _________ function is used to plot negative likelihood.
a) plot()
b) graph()
c) graph.plot()
d) plot.graph()

Answer: a
Clarification: For simple scatter plots, plot.default will be used.

10. What will be the output of the following R code?

> g <- function(x) {
+               a <- 3
+               x+a+y
+             ## 'y' is a free variable
+ }
> y <- 3
> g(2)

a) 9
b) 42
c) 8
d) Error

Answer: c
Clarification: Here, y is defined in the global environment, which also happens to be where the function g() is defined.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top