250+ TOP MCQs on Functions and Answers

R Programming Multiple Choice Questions & Answers focuses on “Functions”.

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

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

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

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

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

b)

c)

d) Error

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

Answer: a
Clarification: 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()

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

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

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

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

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

Leave a Reply

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