11
How is a do-while loop implemented in R?
A)
do { code_block } while (condition)
B)
while (condition) { code_block }
C)
do { code_block } until (condition)
D)
while (condition) do { code_block }
Answer & Solution
Answer: Option
A
12
In R, what does the if (condition) { code_block } else { code_block } statement do?
A)
Executes code_block if the condition is true, otherwise executes the else block
B)
Executes code_block if the condition is false, otherwise executes the else block
C)
Executes code_block if the condition is true, otherwise does nothing
D)
Executes code_block if the condition is false, otherwise does nothing
Answer & Solution
Answer: Option
A
13
How is the repeat loop terminated in R?
A)
Using the break statement
B)
Using the stop statement
C)
Using the terminate statement
D)
Using the end statement
Answer & Solution
Answer: Option
A
14
What is the purpose of the which() function in R?
A)
To determine the length of an object
B)
To identify the positions of elements satisfying a condition
C)
To create a loop based on certain conditions
D)
To concatenate two vectors
Answer & Solution
Answer: Option
B
15
How do you create a vector containing the first 5 positive even numbers in R using a loop?
A)
even_numbers <- numeric(5); for (i in 1:5) { even_numbers[i] <- 2 * i }
B)
even_numbers <- for (i in 1:5) { 2 * i }
C)
even_numbers <- seq(2, 10, by = 2)
D)
even_numbers <- c(2, 4, 6, 8, 10)
Answer & Solution
Answer: Option
A
16
What is the output of the following R code: for (i in 1:5) { if (i %% 2 == 0) { print(i) } }
A)
2
B)
4
C)
2, 4
D)
1, 3, 5
Answer & Solution
Answer: Option
C
17
In R, what is the purpose of the repeat loop combined with break?
A)
To create an infinite loop
B)
To repeat a block of code until a certain condition is met
C)
To skip the current iteration of the loop
D)
To exit the loop immediately
Answer & Solution
Answer: Option
D
18
How is the switch() function different from if-else in R?
A)
switch() is used for multiple conditions, if-else for two conditions
B)
switch() can only handle numeric values, if-else can handle any type
C)
if-else can only handle numeric values, switch() can handle any type
D)
Both switch() and if-else are used interchangeably
Answer & Solution
Answer: Option
A
19
What is the role of the next statement in a loop in R?
A)
To skip the next condition in an if-else statement
B)
To exit the loop immediately
C)
To move to the next iteration of the loop
D)
To restart the loop from the beginning
Answer & Solution
Answer: Option
C
20
In R, what does the repeat loop combined with next do?
A)
Repeats a block of code indefinitely and skips to the next iteration when a condition is met
B)
Repeats a block of code a specified number of times and exits the loop immediately
C)
Repeats a block of code a specified number of times and skips to the next iteration
D)
Repeats a block of code indefinitely and exits the loop immediately
Answer & Solution
Answer: Option
C