R Programming Questions and Answers – Control Structures – 3

This set of R Programming MCQs focuses on “Control Structures – 3”.

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

```> x <- 3
> switch(2, 2+2, mean(1:10), rnorm(5))```

a) 5
b) 5.5
c) NULL
d) 58

Explanation: If value is a number between 1 and the length of list then the corresponding element of list is evaluated and the result returned.

2. Point out the correct statement?
a) Statements, such as x<-1:10 or mean(y), can be separated by either a semicolon or a new line
b) Whenever the evaluator is presented with a syntactically complete statement that statement is evaluated and the value returned
c) Computation in R consists of sequentially evaluating statements
d) All of the mentioned

Explanation: Both semicolons and new lines can be used to separate statements.

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

```> x <- 3
> switch(6, 2+2, mean(1:10), rnorm(5))```

a) 10
b) 1
c) NULL
d) 5

Explanation: If value is too large or too small NULL is returned.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

```> y <- "fruit"
> switch(y, fruit = "banana", vegetable = "broccoli", "Neither")```

a) “banana”
b) “Neither”
c) “broccoli”
d) Error

Explanation: If value is a character vector then the element of ‘…’ with a name that exactly matches value is evaluated.

5. Point out the correct statement?
a) The next statement causes an exit from the innermost loop that is currently being executed
b) There are two statements that can be used to explicitly control looping
c) The break statement immediately causes control to return to the start of the loop
d) Computation in R consists of sequentially evaluating statements

Explanation: They are break and next.

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

```> centre <- function(x, type) {
+ switch(type,
+        mean = mean(x),
+        median = median(x),
+        trimmed = mean(x, trim = .1))
+ }
> x <- rcauchy(10)
> centre(x, "mean")```

a) 0.8760325
b) 0.5360891
c) 0.6086504
d) gives mean with values depending on rcauchy values

Explanation: A common use of switch is to branch according to the character value of one of the arguments to a function. Generates a random point pattern, a simulated realisation of the Neyman-Scott process with Cauchy cluster kernel.

7. Which of the following R code will print “Neither”?
a)

```> y <- "meat"
> switch(y, fruit = "banana", vegetable = "broccoli", "Neither")```

b)

```> y <- "brocolli"
> switch(y, fruit = "banana", vegetable = "broccoli", "Neither")```

c)

```> y <- "banana"
> switch(y, fruit = "banana", vegetable = "broccoli", "Neither")```

d)

```> y >- "banana"
> y(y, fruit = "banana", vegetable = "broccoli", "Neither")```
Explanation: If there is no match a single unnamed argument will be used as a default.

8. R has ________ basic indexing operators.
a) two
b) three
c) four
d) five

Explanation: R contains several constructs which allow access to individual elements or subsets through indexing operations.

9. The syntax of the for loop is?
a)

```for ( \$name in vector )
statement1```

b)

```for loop( name in vector )
statement1```

c)

```for ( name in vector )
statement1```

d)

```forif loop( name in vector )
statement1```
Explanation: For each element in vector the variable name is set to the value of that element and statement1 is evaluated.

10. Which of the following R code syntax is syntactically valid?
a)

```if ( statement1 ) {
statement2
} else if ( statement3 ) {
statement4
} else if ( statement5 ) {
statement6
} else
statement8```

b)

```if ( statement1 ) {
statement2
} elseif ( statement3 ) {
statement4
} elseif ( statement5 ) {
statement6
} else
statement8```

c)

```if ( statement1 ) {
statement2
} elseif ( statement3 ) {
statement4
} else if ( statement5 ) {
statement6
} else
statement8```

d)

```if ( statement1 ) {
statement2
} else if ( statement5 ) {
statement6
} else
statement8```
Explanation: If the optional else clause is omitted and all the odd numbered statements evaluate to FALSE no statement will be evaluated and NULL is returned.

Sanfoundry Global Education & Learning Series – R Programming Language.

Here’s the list of Best Books in R Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]