R Programming - Loops MCQ Questions and Answers

Loops in R allow you to execute a block of code multiple times. They are especially useful for repetitive tasks where the same operation needs to be performed on a sequence of values or data structures. R supports various loop structures such as for, while, and repeat loops, which make it easier to manage repeated tasks.

This quiz will test your understanding of how loops work in R. It covers the basics of loops, including syntax, usage, and common operations. Each question includes an explanation to clarify the concept.

Let’s get started with these multiple-choice questions (MCQs) to improve your knowledge of loops in R.

1. Which loop in R is used when the number of iterations is known beforehand?

a) while loop
b) repeat loop
c) for loop
d) do-while loop

2. What is the correct syntax for a for loop in R?

a) for(i = 1; i <= 5; i++)
b) for(i in 1:5)
c) for i in 1:5
d) for(i, 1:5)

3. Which loop should be used when the number of iterations is not known beforehand?

a) while loop
b) for loop
c) repeat loop
d) do-while loop

4. What is the purpose of the break statement in loops?

a) Skips the current iteration
b) Exits the loop immediately
c) Restarts the loop
d) Pauses the loop

5. Which of the following loops will run indefinitely unless manually stopped?

a) while(TRUE)
b) for(i in 1:5)
c) while(i == 0)
d) for(i in 1:100)

6. What is the purpose of the next statement in R loops?

a) Terminates the loop
b) Skips the current iteration and moves to the next
c) Pauses the loop
d) Repeats the current iteration

7. What is the default increment step in a for loop in R?

a) 1
b) 2
c) 5
d) 0

8. What is the correct syntax for a while loop in R?

a) while condition do
b) while(condition)
c) while condition
d) while{condition}

9. Which loop runs at least once, regardless of the condition?

a) for loop
b) while loop
c) repeat loop
d) None of the above

10. How do you exit a repeat loop in R?

a) continue
b) break
c) stop
d) exit

11. What will be the output of the following code: for(i in 1:3) { print(i) }?

a) 1 2 3
b) 3 2 1
c) Error
d) None of the above

12. Which of the following loops is considered infinite?

a) for(i in 1:5)
b) repeat { print(1) }
c) while(i <= 5)
d) None of the above

13. Which of the following loops does not have a conditional check at the start?

a) for loop
b) while loop
c) repeat loop
d) do-while loop

14. What will be the output of the following code?

 i <- 5; 
while(i > 0) { 
	print(i); 
	i <- i - 1 
}
a) 5 4 3 2 1
b) 1 2 3 4 5
c) 1 2 3 4 5 0
d) Error

15. Which of the following can be used to loop through a vector in R?

a) for loop
b) while loop
c) repeat loop
d) All of the above

These questions help you understand the different types of loops in R and how to use them effectively for repetitive tasks. Mastering loops will allow you to automate and simplify many programming tasks in R. Keep practicing to strengthen your skills.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare