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
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
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
Explanation: 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
View Answer
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
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?
> f <- function() { + ## This is an empty function + } > class(f)
a) “function”
b) “class”
c) “procedure”
d) “system”
View Answer
Explanation: 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()
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!
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
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.
- Practice Programming MCQs
- Apply for Programming Internship
- Check R Programming Books
- Check Information Technology Books