R Functions MCQ

1. What is a user-defined function in R?

a) A function provided by R packages
b) A function created by the user
c) A predefined function in R
d) A function automatically loaded during R startup

Answer:

b) A function created by the user

Explanation:

A user-defined function in R refers to a function that is created by the user to perform specific tasks or calculations, as opposed to built-in functions that are provided by R.

2. Which keyword is used to define a function in R?

a) func
b) define
c) function
d) create

Answer:

c) function

Explanation:

In R, the keyword 'function' is used to define a new function.

3. How do you call a function named "myFunction" in R?

a) myFunction[]
b) myFunction()
c) call myFunction
d) execute myFunction

Answer:

b) myFunction()

Explanation:

Functions in R are called using their name followed by parentheses. Arguments, if any, are placed inside the parentheses.

4. Which of these is a correct way to define a function in R that takes no arguments?

a) myFunction <- () { }
b) myFunction <- function() { }
c) function myFunction() { }
d) create myFunction() { }

Answer:

b) myFunction <- function() { }

Explanation:

Functions in R are defined using the 'function' keyword, followed by parentheses. In this case, no arguments are specified inside the parentheses.

5. How can you return a value from a function in R?

a) return(value)
b) output(value)
c) send(value)
d) All of the above

Answer:

a) return(value)

Explanation:

The return() function is used in R to return a value from a function.

6. What is the scope of a variable defined inside a function in R?

a) Global
b) Local
c) External
d) Both Local and Global

Answer:

b) Local

Explanation:

Variables defined inside a function in R have local scope, meaning they are only accessible within the function.

7. What does the ellipsis (…) argument in a function definition signify in R?

a) An error in the function
b) The function takes no arguments
c) The function can take any number of arguments
d) The function is deprecated

Answer:

c) The function can take any number of arguments

Explanation:

The ellipsis (…) is used in a function definition to indicate that the function can accept a variable number of arguments.

8. How are default values assigned to arguments in R functions?

a) arg = value
b) default arg: value
c) arg <- value
d) set arg = value

Answer:

a) arg = value

Explanation:

In R, default values for function arguments are assigned using the '=' operator within the function definition.

9. What is the result of using the next statement inside a loop within a function in R?

a) Exits the loop
b) Exits the function
c) Skips to the next iteration of the loop
d) Pauses the loop execution

Answer:

c) Skips to the next iteration of the loop

Explanation:

The next statement in a loop within a function causes the loop to skip the rest of the current iteration and proceed to the next iteration.

10. How do you make a function parameter optional in R?

a) By declaring it outside the function
b) By using the optional keyword
c) By assigning a default value
d) By placing it in square brackets

Answer:

c) By assigning a default value

Explanation:

In R, a function parameter is made optional by providing a default value in the function definition.

11. What does the debug() function do when applied to a function in R?

a) Optimizes the function
b) Tests the function for errors
c) Allows step-by-step execution of the function
d) Compiles the function for faster execution

Answer:

c) Allows step-by-step execution of the function

Explanation:

The debug() function in R is used for debugging, allowing the user to step through the execution of the function one line at a time.

12. What is the purpose of the stopifnot() function in R?

a) To stop a loop
b) To terminate the R session
c) To assert conditions are true
d) To stop execution if an argument is not provided

Answer:

c) To assert conditions are true

Explanation:

The stopifnot() function in R is used to assert that specified conditions are true; if any condition is false, an error is generated.

13. How can you source an external R script inside a function?

a) use(script.R)
b) source("script.R")
c) include("script.R")
d) load("script.R")

Answer:

b) source("script.R")

Explanation:

The source() function in R is used to run all the commands contained in an external R script file.

14. What is the difference between library() and require() functions in R when loading a package?

a) There is no difference
b) library() loads the package, require() checks if the package is available
c) require() loads the package, library() checks if the package is available
d) library() throws an error if the package is not available, require() gives a warning

Answer:

d) library() throws an error if the package is not available, require() gives a warning

Explanation:

The library() function throws an error if the package is not available, whereas require() returns FALSE and gives a warning.

15. How do you document a function in R?

a) Using # before each line of documentation
b) Using the documentation() function
c) Using Roxygen comments
d) Documentation is automatically generated

Answer:

c) Using Roxygen comments

Explanation:

In R, functions are often documented using Roxygen comments, which are special comments that can be used to generate documentation.

Leave a Comment

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

Scroll to Top