R Programming Questions and Answers – Control Structures – 2

This set of R Programming Quiz focuses on “Control Structures – 2”.

1. Which of the following code skips the first 20 iterations?
a)

for(i in 1:100) {
             if(i <= 20) {
                next
      }
}

b)

for(i in 1:100) {
           if(i <= 19) {
           next
       }
}

c)

advertisement
advertisement
for(i in 1:100) {
         if(i <= 21) {
         next
      }
}

d)

for(i in 1:10) {
         if(i <= 24) {
         next
      }
}
View Answer
Answer: a
Explanation: next is used to skip an iteration of a loop.
 
 

advertisement

2. Point out the correct statement?
a) The only way to exit a repeat loop is to call break
b) Infinite loops should generally be avoided
c) Control structures like if, while, and for allow you to control the flow of an R program
d) All of the mentioned
View Answer

Answer: d
Explanation: Control structures are primarily useful for writing programs; for command line interactive work.

advertisement

3. _________ initiates an infinite loop right from the start.
a) never
b) repeat
c) break
d) set
View Answer

Answer: b
Explanation: These are not commonly used in statistical or data analysis applications but they do have their uses.

4. Which of the following R code snippet stops loop after 20 iterations?
a)

for(i in 1:100) {
      print(i)
              if(i > 20) {
               break 
       } 
}

b)

for(i in 1:100) {
      print(i)
              if(i > 19) {
               break 
       } 
}

c)

for(i in 1:100) {
      print(i)
              if(i < 20) {
               break 
       } 
}

d)

for(i in 2:100) {
      print("i")
              if(i > 150) {
               break 
       } 
}
View Answer
Answer: a
Explanation: break is used to exit a loop immediately, regardless of what iteration the loop may be on.
 
 

5. Point out the wrong statement?
a) Statements cannot be grouped together using braces ‘{’ and ‘}’
b) Computation in R consists of sequentially evaluating statements
c) Single statements are evaluated when a new line is typed at the end of the syntactically complete statement
d) next is used to skip an iteration of a loop
View Answer

Answer: a
Explanation: Blocks are not evaluated until a new line is entered after the closing brace.

6. _______ is used to skip an iteration of a loop.
a) next
b) skip
c) group
d) cancel
View Answer

Answer: a
Explanation: No statement below next in the current loop is evaluated.

7. Which of the following R code can be used to avoid numeric problems such as taking the logarithm of a negative number?
a)

 if( any(x < 0) ) y <- log(1+x) else y <- log(x)

b)

 if( any(x <= 0) ) y <- log(1+x) else y <- log(x)

c)

 if( any(x >= 0) ) y <- log(1+x) else y <- log(x)

d)

 if( any(x >> 0) ) x >- log(1+x) else x <- log(x)
View Answer
Answer: b
Explanation: The else clause is optional. The statement if(any(x <= 0)) x <- x[x <= 0] is valid.
 
 

8. R has ________ statements that provide explicit looping.
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: b
Explanation: They are for, while and repeat.

9. The syntax of the repeat loop is _________
a) rep statement
b) repeat statement
c) repeat else
d) repeat while
View Answer

Answer: b
Explanation: The repeat statement causes repeated evaluation of the body until a break is specifically requested.

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

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

a) 2.2903605 2.3271663 -0.7060073 1.3622045 -0.2892720
b) 5.5
c) NULL
d) Error
View Answer

Answer: a
Explanation: Conditions are always evaluated from left to right.

Sanfoundry Global Education & Learning Series – R Programming Language.

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

To practice all areas of R Programming for quizzes, Here is complete set of 1000+ Multiple Choice Questions and Answers.

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.