R Programming Quiz focuses on “Control Structures”.
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)
for(i in 1:100) { if(i <= 21) { next } }
d)
for(i in 1:10) { if(i <= 24) { next } }
View Answer
Answer: a
Clarification: next is used to skip an iteration of a loop.
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
Answer: d
Clarification: Control structures are primarily useful for writing programs; for command line interactive work.
3. _________ initiates an infinite loop right from the start.
a) never
b) repeat
c) break
d) set
Answer: b
Clarification: 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
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: Conditions are always evaluated from left to right.