R Programming Questions and Answers – Scoping Rules – 2

This set of R Programming online test 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

Answer: a
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

Answer: a
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

Answer: d
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

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

advertisement
advertisement

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

Answer: a
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?

Note: Join free Sanfoundry classes at Telegram or Youtube
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

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

advertisement

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

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

advertisement

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

Answer: b
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

Answer: a
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

Answer: c
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.

To practice all areas of R Programming for online test, Here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.