R Programming Questions and Answers – Functions – 1

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

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

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

Answer: d
Explanation: 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()
View Answer

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

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

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

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

Answer: c
Explanation: 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
View Answer

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

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

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

Answer: a
Explanation: Functions have their own class.

advertisement

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
Explanation: 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)

Hello, world!

d)

Hello, world!
Hello, world!
Hello, world!
Hello, world!
View Answer
Answer: b
Explanation: 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
View Answer

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

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.