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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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) }
?
Answer:
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?
Answer:
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?
Answer:
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
}
Answer:
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?
Answer:
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
Post a Comment
Leave Comment