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
View Answer

Answer: b
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
View Answer

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

advertisement
advertisement

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
View Answer

Answer: c
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?

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

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

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

advertisement

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
View Answer

Answer: b
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
View Answer

Answer: d
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")
View Answer
Answer: a
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
View Answer

Answer: b
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
View Answer
Answer: c
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
View Answer
Answer: a
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.