R Programming Questions and Answers – Functions – 2

This set of R Programming Multiple Choice Questions & Answers focuses on “Functions – 2”.

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

 > paste("a", "b", se = ":")

a) “a+b”
b) “a=b”
c) “a b :”
d) none of the mentioned
View Answer

Answer: d
Explanation: With the paste() function, the arguments sep and collapse must be named explicitly and in full if the default values are not going to be used.

2. Point out the correct statement?
a) In R, a function is an object which has the mode function
b) R interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions that are desired
c) Functions are also often written when code must be shared with others or the public
d) All of the mentioned
View Answer

Answer: d
Explanation: An argument list is a comma separated list of formal arguments.

advertisement
advertisement

3. The __________ function returns a list of all the formal arguments of a function.
a) formals()
b) funct()
c) formal()
d) fun()
View Answer

Answer: a
Explanation: Functions have named arguments which can optionally have default values.

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

> f <- function(num = 1) {
+        hello <- "Hello, world!\n"
+        for(i in seq_len(num)) {
+                cat(hello)
+         }
+         chars <- nchar(hello) * num
+         chars
+ }
> f()

a)

advertisement
Hello, world!
[1] 14

b)

advertisement
Hello, world!
[1] 15

c)

Hello, world!
[1] 16

d) Error
View Answer

Answer: a
Explanation: The formal arguments are the arguments included in the function definition.

5. Point out the wrong statement?
a) A formal argument can be a symbol, a statement of the form ‘symbol = expression’, or the special formal argument
b) The first component of the function declaration is the keyword function
c) The value returned by the call to function is not a function
d) Functions are also often written when code must be shared with others or the public
View Answer

Answer: a
Explanation: The value returned by the call to function is a function.

6. You can check to see whether an R object is NULL with the _________ function.
a) is.null()
b) is.nullobj()
c) null()
d) as.nullobj()
View Answer

Answer: a
Explanation: It is sometimes useful to allow an argument to take the NULL value, which might indicate that the function should take some specific action.

7. Which of the following code will print NULL?
a) > args(paste)
b) > arg(paste)
c) > args(pastebin)
d) > arg(bin)
View Answer

Answer: a
Explanation: One catch with … is that any arguments that appear after … on the argument list must be named explicitly and cannot be partially matched or matched positionally.

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

> f <- function(a, b) {
+       a^2
+ }
> f(2)

a) 4
b) 3
c) 2
d) 5
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. What will be the output of the following R code snippet?

 > paste("a", "b", sep = ":")

a) “a+b”
b) “a=b”
c) “a:b”
d) a*b
View Answer

Answer: c
Explanation: With the paste() function, the arguments sep and collapse must be named explicitly and in full if the default values are not going to be used.

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

> f <- function(a, b) {
+              print(a)
+              print(b)
+ }
> f(45)

a) 32
b) 42
c) 52
d) 45
View Answer

Answer: d
Explanation: Arguments to functions are evaluated lazily, so they are evaluated only as needed in the body of the function.

Sanfoundry Global Education & Learning Series – R Programming Language.

Here’s the list of Best Books in R Programming Language.

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.