This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Control Structures – 1”.
1. Which of the following is valid syntax for if else statement in R?
a)
if(<condition>) { ## do something } else { ## do something else }
b)
if(<condition>) { ## do something } elseif { ## do something else }
c)
if(<condition>) { ## do something } else if { ## do something else }
d)
if(<condition>) { ##& do something } else { ##@ do something else }
Explanation: If you have an action you want to execute when the condition is false, then you need an else clause.
2. Point out the correct statement?
a) Blocks are evaluated until a new line is entered after the closing brace
b) Single statements are evaluated when a new line is typed at the start of the syntactically complete statement
c) The if/else statement conditionally evaluates two statements
d) Break will execute a loop while a condition is true
View Answer
Explanation: The if/else statement returns, as its value, the value of the statement that was selected.
3. Which of the following R syntax is correct for while loop?
a) while ( statement1 ) statement2
b) while ( statement1 ) else statement2
c) while ( statement1 ) do statement2
d) while ( statement2 ) doelse statement2
View Answer
Explanation: The while statement is very similar to the repeat statement.
4. Which of the following R code generate a uniform random number?
a)
x <- runif(1, 0, 10) if(x > 3) { y <- 10 } else { y <- 0 }
b)
x <- run(1, 0, 10) if(x > 3) { y <- 10 } else { y <- 0 }
c)
x <- random(1, 0, 10) if(x > 3) { y <- 10 } else { y <- 0 }
d)
x <- random(1, 0, 10) if(x > 1) { y <- 0 } else { x <- 0 }
Explanation: The value of y is set depending on whether x > 3 or not.
5. Point out the wrong statement?
a) for will execute a loop a fixed number of times
b) break will execute a loop while a condition is true
c) if and else tests a condition and acting on it
d) break is used to break the execution of a loop
View Answer
Explanation: The if-else structure allows you to test a condition and act on it depending on whether it’s true or false.
6. _______ is used to break the execution of a loop.
a) next
b) skip
c) break
d) delete
View Answer
Explanation: Control structures allow you to respond to inputs or to features of the data and execute different R expressions accordingly.
7. Which of the following R code generate a sequence of integers from 1 to 10?
a)
> for(i in 1:9) { + print(i) + } [1]
b)
> for(i in 0:9) { + print(i) + } [1]
c)
> for(i in 1:10) { + print(i) + } [1]
d)
> for(i in 2:50) { + print("i") + } [1]
Explanation: In R, for loops take an iterator variable and assign it successive values from a sequence or vector.
8. Which of the following statement can be used to explicitly control looping?
a) if
b) while
c) break
d) for
View Answer
Explanation: The break statement causes an exit from the innermost loop that is currently being executed.
9. Which of the following should be preferred for evaluation from list of alternatives?
a) subsett
b) eval
c) switch
d) set
View Answer
Explanation: eval(x[[condition]]) is the best way to select one for evaluation.
10. What will be the output of the following R code?
> x <- c("a", "b", "c", "d") > for(i in 1:4) { + ## Print out each element of 'x' + print(x[i]) + }
a)
[1] "a" [1] "b" [1] "c" [1] "d"
b)
[1] "c" [1] "b" [1] "a" [1] "d"
c)
[1] "d" [1] "c" [1] "b" [1] "a"
d) Error
View Answer
Explanation: The above code print out each element of ‘x’.
Sanfoundry Global Education & Learning Series – R Programming Language.
Here’s the list of Best Books in R Programming Language.
- Check R Programming Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Information Technology Books