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

Answer:

c) for loop

Explanation:

The for loop is used in R when the number of iterations is known before the loop starts.

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)

Answer:

b) for(i in 1:5)

Explanation:

The correct syntax for a for loop in R is for(i in 1:5), where i takes values from 1 to 5 in each iteration.

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

Answer:

a) while loop

Explanation:

The while loop is used when the number of iterations is not known beforehand. It continues until the condition becomes false.

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

Answer:

b) Exits the loop immediately

Explanation:

The break statement is used to exit the loop immediately, regardless of the loop's condition.

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)

Answer:

a) while(TRUE)

Explanation:

The while(TRUE) loop runs indefinitely since the condition always evaluates to true. It needs a break statement to stop it.

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

Answer:

b) Skips the current iteration and moves to the next

Explanation:

The next statement skips the current iteration of the loop and proceeds with the next one.

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

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

Answer:

a) 1

Explanation:

In R, the for loop increments by 1 by default when iterating over a sequence like 1:5.

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}

Answer:

b) while(condition)

Explanation:

The correct syntax for a while loop in R is while(condition), where the condition is checked before each iteration.

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

Answer:

c) repeat loop

Explanation:

The repeat loop runs indefinitely until a break statement is encountered, so it executes at least once.

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

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

Answer:

b) break

Explanation:

The break statement is used to exit a repeat loop in R.

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

Answer:

a) 1 2 3

Explanation:

The for loop iterates through the sequence 1:3 and prints each value (1, 2, and 3).

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

Answer:

b) repeat { print(1) }

Explanation:

The repeat loop will continue indefinitely unless a break statement is encountered, making it an infinite loop.

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

Answer:

c) repeat loop

Explanation:

The repeat loop does not check any condition at the start; it runs indefinitely until a break statement is used.

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

Answer:

a) 5 4 3 2 1

Explanation:

The while loop starts with i = 5 and decrements i by 1 in each iteration, printing the values 5, 4, 3, 2, and 1.

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

Answer:

d) All of the above

Explanation:

All types of loops (for, while, and repeat) can be used to iterate through a vector in R, depending on the use case.

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