250+ TOP MCQs on Loop Functions and Answers

R Programming online quiz focuses on “Loop Functions”.

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

> x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
> lapply(x, mean)

a)

$a
[1] 2.5
$b
[1] 1.248845
$c
[1] 1.9935285
Loop Functions 90
$d
[1] 5.051388

b)

$a
[1] 2.5
$b
[1] 0.248845
$c
[1] 0.9935285
Loop Functions 90
$d
[1] 5.051388

c)

$a
[1] 3.5
$b
[1] 0.248845
$c
[1] 0.9935285
Loop Functions 90
$d
[1] 5.051388

d) Error

Answer: b
Clarification: You can use lapply() to evaluate a function multiple times each with a different argument.

2. Point out the wrong statement?
a) The sapply() function behaves similarly to lapply()
b) With multiple factors and many levels, creating an interaction can result in many levels that are empty
c) apply() can be thought of as a combination of split() and sapply() for vectors only
d) You can use tsplit() to evaluate a function single time each with a same argument

Answer: c
Clarification: tapply() can be thought of as a combination of split() and sapply() for vectors only.

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

> x <- 1:4
> lapply(x, runif)

a)

[[1]]
[1] 0.02778712
[[2]]
[1] 0.5273108 0.8803191
[[3]]
[1] 0.37306337 0.04795913 0.13862825
[[4]]
[1] 0.3214921 0.1548316 0.1322282 0.2213059

b)

[[1]]
[1] 1.02778712
[[2]]
[1] 2.5273108 0.8803191
[[3]]
[1] 3.37306337 0.04795913 0.13862825
[[4]]
[1] 0.3214921 0.1548316 0.1322282 0.2213059

c)

[[1]]
[1] 1.02778712
[[2]]
[1] 0.5273108 0.8803191
[[3]]
[1] 0.37306337 0.04795913 0.13862825
[[4]]
[1] 3.3214921 2.1548316 1.1322282 0.2213059

d) Error

Answer: a
Clarification: In the above question, the first argument of runif() is n, and so the elements of the sequence 1:4 all got passed to the n argument of runif().

4. Which of the following R code will print the following result?

[[1]]
[1] 2.263808
[[2]]
[1] 1.314165 9.815635
[[3]]
[1] 3.270137 5.069395 6.814425
[[4]]
[1] 0.9916910 1.1890256 0.5043966 9.2925392

a)

> x <- 1:4
> lapply(x, runif, min = 0, max = 10)

b)

> x <- 1:4
> lapply(x, runif, min = 0, max = 9)

c)

> x <- 1:3
> lapply(x, runif, min = 0, max = 10)

d)

> x <- 1:4
> lapply(x, runif, min = 0, max = 6)

View Answer

  250+ TOP MCQs on History of R and Answers

Answer: a
Clarification: Once the call to lapply() is finished, the function disappears and does not appear in the workspace.

 

5. Point out the correct statement?
a) split() takes elements of the list and passes them as the first argument of the function you are applying
b) You can use tsplit() to evaluate a function single time each with a same argument
c) Sequence of operations is sometimes referred to as “map-reduce”
d) apply() can be thought of as a combination of split() and sapply() for vectors only

Answer: c
Clarification: The results of applying the function over the subsets are then collated and returned as an object.

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

> x <- list(a = matrix(1:4, 2, 2), b = matrix(1:6, 3, 2))
> lapply(x, function(elt) { elt[,1] })

a)

b)

$a
[1] 1 2 3
$b
[1] 1 2 3

c)

d) Error

Answer: a
Clarification: You can put an arbitrarily complicated function definition inside lapply().

7. The _____ function takes a vector or other objects and splits it into groups determined by a factor or list of factors.
a) apply()
b) lsplit()
c) split()
d) mapply()

Answer: c
Clarification: The combination of split() and a function like lapply() or sapply() is a common paradigm in R.

8. Which of the following is valid body of split function?
a) function (x, f)
b) function (x, drop = FALSE, …)
c) function (x, f, drop = FALSE, …)
d) function (drop = FALSE, …)

Answer: c
Clarification: x is a vector (or list) or data frame.

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

> f <- function(elt) {
+ elt[, 1]
+ }
> lapply(x, f)

a)

  250+ TOP MCQs on Subsetting and Answers

b)

$a
[1] 1 2 3
$b
[1] 1 2 3

c)

d) Error

Answer: a
Clarification: Whether you use an anonymous function or you define a function first depends on your context.

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

> x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
> sapply(x, mean)

a)

a b c d
2.500000 -0.251483 1.481246 4.968715

b)

a b c d
2.500000 -3.251483 2.481246 5.968715

c)

a b c d
3.500000 0.251483 1.481246 4.968715

d) Error

Answer: a
Clarification: sapply() collapsed the output into a numeric vector, which is often more useful than a list.

Leave a Comment

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

Scroll to Top