250+ TOP MCQs on Loop Functions and Answers

R Programming Language Multiple Choice Questions on “Loop Functions”.

1. ________ loop over a list and evaluate a function on each element.
a) apply()
b) lapply()
c) sapply()
d) mapply()

Answer: a
Clarification: An auxiliary function split is also useful, particularly in conjunction with lapply.

2. Point out the wrong statement?
a) Multi-line expressions with curly braces are just not that easy to sort through when working on the command line
b) lappy() loops over a list, iterating over each element in that list
c) lapply() does not always returns a list
d) You cannot use lapply() to evaluate a function multiple times each with a different argument

Answer: c
Clarification: lapply() always returns a list, regardless of the class of the input.

3. __________ function is same as lapply in R.
a) apply()
b) lapply()
c) sapply()
d) mapply()

Answer: c
Clarification: lapply tries to simplify the result.

4. Which of the following is multivariate version of lapply?
a) apply()
b) lapply()
c) sapply()
d) mapply()

Answer: d
Clarification: The mapply() function is a multivariate apply of sorts which applies a function in parallel over a set of arguments.

5. Point out the correct statement?
a) lapply() takes elements of the list and passes them as the first argument of the function you are applying
b) You can use lapply() to evaluate a function multiple times each with a different argument
c) Functions that you pass to lapply() may have other arguments
d) None of the mentioned

Answer: d
Clarification: The lapply() function and its friends make heavy use of anonymous functions.

6. ________ applies a function over the margins of an array.
a) apply()
b) lapply()
c) tapply()
d) mapply()

  250+ TOP MCQs on Commands and Answers

Answer: a
Clarification: The apply() function is used to a evaluate a function (often an anonymous one) over the margins of an array.

7. _______ is used to apply a function over subsets of a vector.
a) apply()
b) lapply()
c) tapply()
d) mapply()

Answer: c
Clarification: t in tapply stands for table.

8. lappy functions takes _________ arguments in R language.
a) two
b) three
c) four
d) five

Answer: c
Clarification: This function takes three arguments: (1) a list X; (2) a function (or the name of a function) FUN; (3) other arguments via its … argument.

9. Body of lapply function is?
a)

function (X, FUN, ...)
{
    FUN <- match.fun(FUN)
    if (!is.vector(X) || is.object(X))
       X <- as.list(X)
    .Internal(lapply(X, FUN))
}

b)

function (X, FUN, ...)
{
    FUN <- match.fun(FUN)
    if (!is.vector(X) | is.object(X))
      X <- as.list(X)
    .Internal(lapply(X, FUN))
}

c)

function (X, FUN, ...)
{
    FUN <- match.fun(FUN)
    if (is.vector(X) || is.object(X))
      X <- as.list(X)
    .Internal(lapply(X, FUN))
}

d)

function (X, FUN, ...)
{
    FUN <- match.fun(FUN)
    if (is.vector(X) || is.object(X))
}

View Answer

Answer: a
Clarification: Actual looping is done internally in C code for efficiency reasons.

 

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

> x <- list(a = 1:5, b = rnorm(10))
> lapply(x, mean)

a)

$a
[1] 3
$b
[1] 0.1322028

b)

$a
[1] 4
$b
[1] 0.1322028

c)

$a
[1] 5
$b
[1] 0.1322028

d)

View Answer

Answer: a
Clarification: If the original list has names, the the names will be preserved in the output.

Leave a Comment

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

Scroll to Top