Q. What will be the output of the following R code?
> f <- function(num) {
+ hello <- "Hello, world!\n"
+ for(i in seq_len(num)) {
+ cat(hello)
+ }
+ chars <- nchar(hello) * num
+ chars
+ }
> meaningoflife <- f(3)
> print(meaningoflife)

  • (A) 32
  • (B) 42
  • (C) 52
  • (D) 46
πŸ’¬ Discuss
βœ… Correct Answer: (A) 32

Q. . . . . . . . . function is same as lapply in R.

  • (A) apply()
  • (B) lapply()
  • (C) sapply()
  • (D) mapply()
πŸ’¬ Discuss
βœ… Correct Answer: (C) sapply()

Q. What would be the value of following R expression?
log(-1)

  • (A) Warning in log(-1): NaNs produced
  • (B) 1
  • (C) Null
  • (D) 0
πŸ’¬ Discuss
βœ… Correct Answer: (A) Warning in log(-1): NaNs produced

Q. The . . . . . . . . for R are the main feature that make it different from the original S language.

  • (A) scoping rules
  • (B) closure rules
  • (C) environment rules
  • (D) closure & environment rules
πŸ’¬ Discuss
βœ… Correct Answer: (A) scoping rules

Q. What will be the output of the following R code?
> f <- function(elt) {
+ elt[, 1]
+ }
> lapply(x, f)

  • (A) $a [1] 1 2 $b [1] 1 2 3
  • (B) $a [1] 1 2 3 $b [1] 1 2 3
  • (C) $a [1] 1 2 3 $b [1] 1 2
  • (D) Error
πŸ’¬ Discuss
βœ… Correct Answer: (A) $a [1] 1 2 $b [1] 1 2 3

Q. 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) }
πŸ’¬ Discuss
βœ… Correct Answer: (A) primes <- numeric(5); num <- 2; while (length(primes) < 5) { if (all(num %% 2:(num-1) != 0)) { primes <- c(primes, num) } num <- num + 1 }

Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (B) To check a single condition and execute different code based on the result

Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (A) for loop is used for fixed iterations, while loop for variable iterations

Q. 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
πŸ’¬ Discuss
βœ… Correct Answer: (A) Pauses the execution of R for 2 seconds

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