Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (B) 5, 4, 3, 2, 1

Q. 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)
πŸ’¬ Discuss
βœ… Correct Answer: (C) switch(case_expression, case1 = expr1, case2 = expr2, default = default_expr)

Q. 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 }
πŸ’¬ Discuss
βœ… Correct Answer: (A) for (i in 1:10) { print(i^2) }

Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (C) Moves to the next iteration of the loop

Q. 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) 1, 2, 3, 4, 5
πŸ’¬ Discuss
βœ… Correct Answer: (A) 1, 2, 4, 5

Q. 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 }
πŸ’¬ Discuss
βœ… Correct Answer: (A) do { code_block } while (condition)

Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (A) Executes code_block if the condition is true, otherwise executes the else block

Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (A) Using the break statement

Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (B) To identify the positions of elements satisfying a condition

Q. 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)
πŸ’¬ Discuss
βœ… Correct Answer: (A) even_numbers <- numeric(5); for (i in 1:5) { even_numbers[i] <- 2 * i }