21
How do you create a vector containing the first 5 prime numbers in R using a while loop?
A)
primes <- numeric(5); num <- 2; while (length(primes) < 5) { if (all(num %% 2:(num-1) != 0)) { primes <- c(primes, num) } num <- num + 1 }
B)
primes <- while (length(primes) < 5) { if (all(num %% 2:(num-1) != 0)) { c(primes, num) } num <- num + 1 }
C)
primes <- c(2, 3, 5, 7, 11); while (length(primes) < 5) { primes <- c(primes, primes[length(primes)] + 2) }
D)
primes <- numeric(5); for (i in seq(2, 11, by = 2)) { primes <- c(primes, i) }
Answer & Solution
Answer: Option
A
22
In R, what is the purpose of the ifelse() function?
A)
To create a loop that iterates a specified number of times
B)
To check a single condition and execute different code based on the result
C)
To switch between different conditions and values
D)
To generate random numbers based on a condition
Answer & Solution
Answer: Option
B
23
How is the for loop different from the while loop in R?
A)
for loop is used for fixed iterations, while loop for variable iterations
B)
while loop is used for fixed iterations, for loop for variable iterations
C)
Both for and while loops are used for fixed iterations
D)
Both for and while loops are used for variable iterations
Answer & Solution
Answer: Option
A
24
What does the Sys.sleep(2) function do in R?
A)
Pauses the execution of R for 2 seconds
B)
Sets the system clock to 2 seconds
C)
Sleeps the R process for 2 milliseconds
D)
Delays the execution of a loop by 2 seconds
Answer & Solution
Answer: Option
A
25
In R, how do you create an infinite loop?
A)
repeat { code_block }
B)
while (TRUE) { code_block }
C)
for (i in 1:Inf) { code_block }
D)
forever { code_block }
Answer & Solution
Answer: Option
B
26
What is the output of the following R code: x <- 5; while (x > 0) { print(x); x <- x - 2 }
A)
5, 3, 1
B)
5, 4, 3, 2, 1
C)
5, 2, 1
D)
5, 3, 2, 1
Answer & Solution
Answer: Option
B
27
In R, how do you create a switch statement with a default case?
A)
switch(case_expression, list("case1" = expr1, "case2" = expr2, "default" = default_expr))
B)
switch(case_expression, "case1" = expr1, "case2" = expr2, default = default_expr)
C)
switch(case_expression, case1 = expr1, case2 = expr2, default = default_expr)
D)
switch(case_expression, "case1", "case2", default = default_expr)
Answer & Solution
Answer: Option
C
28
How do you create a loop that prints the squares of numbers from 1 to 10 in R?
A)
for (i in 1:10) { print(i^2) }
B)
for (i = 1; i <= 10; i++) { print(i^2) }
C)
while (i <= 10) { print(i^2); i <- i + 1 }
D)
repeat { print(i^2); i <- i + 1; if (i > 10) break }
Answer & Solution
Answer: Option
A
29
In R, what is the purpose of the next statement in a loop?
A)
Skips the next condition in an if-else statement
B)
Exits the loop immediately
C)
Moves to the next iteration of the loop
D)
Restarts the loop from the beginning
Answer & Solution
Answer: Option
C
30
What is the output of the following R code: for (i in 1:5) { if (i == 3) next; print(i) }
A)
1, 2, 4, 5
B)
1, 2, 3, 4, 5
C)
2, 4
D)
0, 1, 2, 3, 4
Answer & Solution
Answer: Option
A