R Online Test - MCQ Questions

Welcome to R Programming Online Test!. Here, we will present 35 MCQs (Multiple-Choice Questions) containing code snippets to test your coding and logical skills.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

1. What is the primary function of the apply() function in R?

a) To execute a function over the margins of an array
b) To manipulate each element of a vector
c) To generate a sequence of numbers
d) To plot graphs

2. What does the following R code output?

a) 10
b) 20
c) 30
d) 40

3. How do you check for missing values in an R vector?

a) is.na(vec)
b) is.null(vec)
c) na.omit(vec)
d) null(vec)

4. Which function in R is used to import data from CSV files?

a) read.csv()
b) read.table()
c) load.csv()
d) open.csv()

5. What is the purpose of the set.seed() function in R?

a) To define the middle value of a dataset
b) To ensure the reproducibility of random number generation
c) To set the default directory
d) To establish a connection to a database

6. What will the following R code output?

a) 0 1 0 1
b) TRUE FALSE TRUE FALSE
c) 1 3
d) 2 4

7. How can you generate a sequence from 1 to 10 in R?

a) seq(1, 10)
b) sequence(1, 10)
c) 1:10
d) Both a) and c)

8. Which package in R is commonly used for creating plots?

a) dplyr
b) ggplot2
c) tidyr
d) shiny

9. What is the default method to handle strings as factors in data.frame() in R version 3.x and earlier?

a) As characters
b) As factors
c) As numeric
d) As logical

10. What does the subset() function do in R?

a) Subtracts one number from another
b) Divides the dataset into train and test sets
c) Selects variables and observations based on conditions
d) Combines multiple data frames

11. What will the following R code snippet return?

a) NA
b) 12
c) 15
d) 0

12. How do you create a factorial function using recursion in R?

factorial <- function(n) {
    if (n <= 1)
    return(1)
    else
    return(n * factorial(n-1))
}
factorial(5)
a) 120
b) 24
c) 5
d) None of the above

13. What is the class of the following object in R?

a) integer
b) double
c) character
d) logical

14. What does the lapply() function return?

a) A vector
b) A list
c) An array
d) A data frame

15. What is the purpose of the rnorm() function in R?

a) To generate a sequence of numbers
b) To perform linear regression
c) To generate random numbers from a normal distribution
d) To normalize data

16. What is the output of the following R code?

x <- c(1, 2, 3, 4, 5)
print(sum(x))
a) 15
b) 5
c) 10
d) 20

17. What will the following R code print?

x <- c(TRUE, FALSE, TRUE, FALSE)
print(length(x))
a) 2
b) 4
c) TRUE
d) FALSE

18. What does the following R code output?

x <- 5
y <- 3
print(x %% y)
a) 2
b) 1.67
c) 5
d) 3

19. What is the output of the following R code?

x <- seq(1, 5)
print(x[3])
a) 1
b) 2
c) 3
d) 4

20. What does the following R code print?

vec <- c("apple", "banana", "cherry")
print(tolower(vec))
a) "apple" "banana" "cherry"
b) "APPLE" "BANANA" "CHERRY"
c) "apple" "banana" "cherry"
d) None of the above

21. What will be the output of the following R code?

x <- c("a", "b", "c", "d")
print(paste(x, collapse = " "))
a) "a b c d"
b) "abcd"
c) "a, b, c, d"
d) "a+b+c+d"

22. What does this R code output?

x <- c(10, 20, 30)
x <- append(x, c(40, 50))
print(x[5])
a) 10
b) 20
c) 40
d) 50

23. What will the following R code snippet output?

x <- 5
if (x > 3) {
    x <- 10
}
print(x)
a) 5
b) 10
c) TRUE
d) FALSE

24. What does the following R code print?

x <- factor(c("high", "low", "medium", "high", "low"))
print(nlevels(x))
a) 3
b) 5
c) 1
d) 2

25. What is the result of executing the following R code?

x <- c(1, 2, 3)
y <- c(4, 5, 6)
print(x * y)
a) 4 10 18
b) 5 7 9
c) 1 2 3
d) 4 5 6

26. What will this R function output?

calculate <- function(a, b) {
    return(a + b)
}
print(calculate(5, 3))
a) 8
b) 15
c) 5
d) None of these

27. What is the output of the following R code?

x <- matrix(1:9, nrow=3)
print(x[2, 3])
a) 2
b) 4
c) 6
d) 8

28. What is the output of this R program?

x <- c(TRUE, FALSE, TRUE, FALSE)
print(all(x))
a) TRUE
b) FALSE
c) NULL
d) Error

29. What will the following R code snippet output?

x <- "Hello, R!"
print(substring(x, 8, 9))
a) R!
b) R
c) o
d) Hello

30. What does this R code print?

x <- 1:10
print(x[x > 5])
a) 1 2 3 4 5
b) 6 7 8 9 10
c) 5 6 7 8 9 10
d) 1 2 3 4

31. What is the outcome of the following R code?

x <- c("apple", "banana", "cherry")
print(length(x))
a) 3
b) 6
c) 1
d) None of these

32. What does the following R code return?

x <- c(NA, 1, 2, NA, 3)
print(sum(x, na.rm = TRUE))
a) NA
b) 6
c) 0
d) None of these

33. What is the result of executing the following R code?

x <- c("one", "two", "three")
y <- c("four", "five", "six")
print(c(x, y))
a) "one" "two" "three" "four" "five" "six"
b) "one four" "two five" "three six"
c) "one" "four" "two" "five" "three" "six"
d) Error

34. What will the following R code snippet output?

x <- 10
if (x %% 2 == 0) {
    print("Even")
    } else {
        print("Odd")
    }
a) Even
b) Odd
c) Error
d) None

35. What is the output of the following R code?

print(sqrt(16))
a) 4
b) 16
c) 256
d) Error

36. What will be the output of the following R program?

x <- "100"
print(as.numeric(x) * 2)
a) "200"
b) 200
c) Error
d) NULL

Comments