R Programming Questions and Answers – Control Structures – 1

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)

advertisement
advertisement
if(<condition>) {
          ## do something
}
else if {
          ## do something else
}

d)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
if(<condition>) {
          ##& do something
}
else {
          ##@ do something else
}
View Answer
Answer: a
Explanation: If you have an action you want to execute when the condition is false, then you need an else clause.
 
 

advertisement

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

Answer: c
Explanation: The if/else statement returns, as its value, the value of the statement that was selected.

advertisement

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

Answer: a
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
}
View Answer
Answer: a
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

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

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

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

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

Answer: a
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.

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

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.