This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Scoping Rules – 2”.
1. ________ functions can be “built which contain all of the necessary data for evaluating the function.
a) Objective
b) reflective
c) Nested
d) Symmetry
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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()
View Answer
Explanation: 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
View Answer
Explanation: 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”
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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()
View Answer
Explanation: 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
View Answer
Explanation: Here, y is defined in the global environment, which also happens to be where the function g() is defined.
Sanfoundry Global Education & Learning Series – R Programming Language.
Here’s the list of Best Books in R Programming Language.
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check R Programming Books