250+ TOP MCQs on Functions and Answers

R Programming Language Multiple Choice Questions on “Functions”.

1. Which of the following is apply function in R?
a) apply()
b) tapply()
c) fapply()
d) rapply()

Answer: b
Clarification: Functions can be passed as arguments to other functions.

2. Point out the correct statement?
a) Writing functions is a core activity of an R programmer
b) Functions are often used to encapsulate a sequence of expressions that need to be executed numerous times
c) Functions are also often written when code must be shared with others or the public
d) All of the mentioned

Answer: d
Clarification: It represents the key step of the transition from a mere “user” to a developer who creates new functionality for R.

3. Functions are defined using the _________ directive and are stored as R objects.
a) function()
b) funct()
c) functions()
d) fun()

Answer: a
Clarification: In particular, they are R objects of class “function”.

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

> f <- function() {
+             ## This is an empty function
+ }
> f()

a) 0
b) No result
c) NULL
d) 1

Answer: c
Clarification: This function takes no arguments and does nothing.

5. Point out the wrong statement?
a) Functions in R are “second class objects”
b) The writing of a function allows a developer to create an interface to the code, that is explicitly specified with a set of parameters
c) Functions provides an abstraction of the code to potential users
d) Writing functions is a core activity of an R programmer

Answer: a
Clarification: Functions in R are “first class objects”, which means that they can be treated much like any other R object.

6. What will be the output of the following R code?

> f <- function() {
+              ## This is an empty function
+ }
> class(f)

a) “function”
b) “class”
c) “procedure”
d) “system”

Answer: a
Clarification: Functions have their own class.

7. Which of the following R code will print “Hello, world!”?
a)

> f <- function() {
+              cat("Hello, world!n")
+ }
> f()

b)

>  f <- function() {
+              cat("Hello, World!n")
+ }
< f()

c)

> f <- function() {
+             cat("Hello world!n")
+ }
>>= f()

d)

> f <- function() {
-             cat("Hello world!n")
+ }
<= f()

View Answer

Answer: a
Clarification: This function has a non-trivial function body.

 

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

> f <- function(num) {
+ for(i in seq_len(num)) {
+               cat("Hello, world!n")
+ }
+ }
> f(3)

a)

Hello, world!
Hello, world!

b)

Hello, world!
Hello, world!
Hello, world!

c)

d)

Hello, world!
Hello, world!
Hello, world!
Hello, world!

View Answer

Answer: b
Clarification: In general, if you find yourself doing a lot of cutting and pasting, that’s usually a good sign that you might need to write a function.

 

9. What will be the output of the following R code?

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

a) 32
b) 42
c) 52
d) 46

Answer: a
Clarification: This function returns the total number of characters printed to the console.

Leave a Reply

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