R Programming Questions and Answers – Scoping Rules – 1

This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Scoping Rules – 1”.

1. What will be the output of the following R code snippet?

> lm <- function(x) { x * x }
> lm

a) function(x) { x * x }
b) func(x) { x * x }
c) function(x) { x / x }
d) function { x $ x }
View Answer

Answer: a
Explanation: When R tries to bind a value to a symbol, it searches through a series of environments to find the appropriate value.

2. Point out the correct statement?
a) The search list can be found by using the searchlist() function
b) The search list can be found by using the search() function
c) The global environment or the user’s workspace is always the second element of the search list
d) R has separate namespaces for functions and non-functions
View Answer

Answer: b
Explanation: Base package is always the last element.

advertisement
advertisement

3. A function, together with an environment, makes up what is called a ______ closure.
a) formal
b) function
c) reflective
d) symmetry
View Answer

Answer: b
Explanation: The function closure model can be used to create functions that “carry around” data with them.

4. Which of the variable in the following R code is variable?

Note: Join free Sanfoundry classes at Telegram or Youtube
> f <- function(x, y) {
+            x^2 + y / z
+ }

a) x
b) y
c) z
d) yy
View Answer

Answer: c
Explanation: This function has 2 formal arguments x and y. In the body of the function there is another symbol z. In this case z is called a free variable.

advertisement

5. Point out the wrong statement?
a) The order of the packages on the search list does not matter
b) R has separate namespaces for functions and non-functions
c) Users can configure which packages get loaded on startup so if you are writing a function
d) The search list can be found by using the search() function
View Answer

Answer: a
Explanation: The order of the packages on the search list matters, particularly if there are multiple objects with the same name in different packages.

6. R uses _________ scoping6 0 or static scoping.
a) reflective
b) transitive
c) lexical
d) closure
View Answer

Answer: c
Explanation: Lexical scoping in R means that the values of free variables are searched for in the environment in which the function was defined.

advertisement

7. The only environment without a parent is the ________ environment.
a) full
b) half
c) null
d) empty
View Answer

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

8. The ________ for R are the main feature that make it different from the original S language.
a) scoping rules
b) closure rules
c) environment rules
d) closure & environment rules
View Answer

Answer: a
Explanation: This function never actually uses the argument b, so calling f(2) will not produce an error because the 2 gets positionally matched to a.

9. The _________ function is a kind of “constructor function” that can be used to construct other functions.
a) make.pow()
b) make.power()
c) keep.power()
d) keep.pow()
View Answer

Answer: b
Explanation: Typically, a function is defined in the global environment, so that the values of free variables are just found in the user’s workspace.

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

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

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

Answer: d
Explanation: Object ‘y’ not found error is displayed.

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 Language, 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.