# R Language quiz questions

• 1.

What will be the output of the following code ?

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

1. ```\$a
[1] 2.5
\$b
[1] 1.248845
\$c
[1] 1.9935285
Loop Functions 90
\$d
[1] 5.051388```
2. ```\$a
[1] 2.5
\$b
[1] 0.248845
\$c
[1] 0.9935285
Loop Functions 90
\$d
[1] 5.051388```
3. ```\$a
[1] 3.5
\$b
[1] 0.248845
\$c
[1] 0.9935285
Loop Functions 90
\$d
[1] 5.051388```
4. None of the mentioned

• 2.

What will be the output of following code ?

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

1. ```\$a
[1] 3
\$b
[1] 0.1322028```
2. ```\$a
[1] 4
\$b
[1] 0.1322028```
3. ```\$a
[1] 5
\$b
[1] 0.1322028```
4. Error

• 3.

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) None of the mentioned

1. A

2. B

3. C

4. D

• 4.

lappy functions takes _________ arguments in R language.

1. two

2. three

3. four

4. All of the mentioned

• 5.

_______ is used to apply a function over subsets of a vector.

1. apply()

2. lapply()

3. tapply()

4. mapply()

• 6.

________ applies a function over the margins of an array.

1. apply()

2. lapply()

3. tapply()

4. mapply()

• 7.

Point out the correct statement :

1. lapply() takes elements of the list and passes them as the first argument of the function you are applying

2. You can use lapply() to evaluate a function multiple times each with a different argument

3. Functions that you pass to lapply() may have other arguments

4. None of the mentioned

• 8.

Which of the following is multivariate version of lapply ?

1. apply()

2. lapply()

3. sapply()

4. mapply()

• 9.

__________ function is same as lapply in R

1. apply()

2. lapply()

3. sapply()

4. mapply()

• 10.

Point out the wrong statement :

1. Multi-line expressions with curly braces are just not that easy to sort through when working on the command line

2. lappy() loops over a list, iterating over each element in that list

3. lapply() does not always returns a list

4. All of the mentioned

• 11.

________ loop over a list and evaluate a function on each element

1. apply()

2. lapply()

3. sapply()

4. mapply()

• 12.

What will be the output of following code ?

``````> g <- function(x) {
+               a <- 3
+               x+a+y
+             ## 'y' is a free variable
+ }
> y <- 3
> g(2)``````

1. 9

2. 42

3. 8

4. Error

• 13.

The _________ function is used to plot negative likelihood.

1. plot()

2. graph()

3. graph.plot()

4. None of the mentioned

• 14.

What will be the output of the following code ?

``````> nLL <- make.NegLogLik(normals, c(1, FALSE))
> optimize(nLL, c(1e-6, 10))\$minimum``````

1. 1.217775

2. 1.800596

3. 3.73424

4. empty

• 15.

What will be the output of the following code ?

``````> nLL <- make.NegLogLik(normals, c(FALSE, 2))
> optimize(nLL, c(-1, 3))\$minimum``````

1. 1.217775

2. 2.217775

3. 3

4. empty

• 16.

What will be the output of the following code ?

``````function(p) {
params[!fixed] <- p
mu <- params[1]
sigma <- params[2]
## Calculate the Normal density
a <- -0.5*length(data)*log(2*pi*sigma^2)
b <- -0.5*sum((data-mu)^2) / (sigma^2)
-(a + b)
}
> ls(environment(nLL))``````

1. “data” “fixed” “param”

2. “data” “variable” “params”

3. “data” “fixed” “params”

4. All of the mentioned

• 17.

Point out the correct statement :

1. An environment is a collection of (symbol, value) pairs, i.e. x is a symbol and 3.14 might be its value

2. If the value of a symbol is not found in the environment in which a function was defined, then the search is continued in the child environment

3. After the top-level environment, the search continues down the search list until we hit the parent environment

4. None of the mentioned

• 18.

_________ require you to pass a function whose argument is a vector of parameters

1. optimize()

2. optimise()

3. opt()

4. All of the mentioned

• 19.

Which of the following language supports lexical scoping ?

1. Perl

2. Python

3. Common Lisp

4. All of the mentioned

• 20.

Point out the wrong statement :

1. Dynamic scoping turns out to be particularly useful for simplifying statistical computations

2. Lexical scoping turns out to be particularly useful for simplifying statistical computations

3. The scoping rules of a language determine how values are assigned to free variables

4. All of the mentioned