R Programming MCQ Questions and Answers

Welcome to our R Programming Quiz! This set of 25 multiple-choice questions is designed to evaluate your knowledge of R, a powerful language used mainly for statistical computing and graphics. Ready to challenge yourself and see how well you know R? Let's get started!

1. What is the use of the c() function in R?

a) It combines values into a vector or list.
b) It creates a new class.
c) It checks conditions.
d) It concatenates strings.

2. How can you generate a sequence of numbers from 1 to 10 in R?

a) 1:10
b) seq(1, 10)
c) 1..10
d) Both a and b are correct

3. What will the following R code output?

x <- c(1, 2, 3)
y <- x * 2
print(y)
a) 2 4 6
b) 1 2 3
c) Error
d) None of the above

4. How do you access the second element of a vector in R?

a) vector[2]
b) vector(2)
c) vector[[2]]
d) vector{2}

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

a) To perform iteration
b) To execute a function
c) To make condition-based decisions
d) To apply a condition to a vector and return a vector with results

6. What will the following R code output?

v <- c(1, 2, "three")
print(class(v))
a) numeric
b) character
c) integer
d) vector

7. How do you check for missing values in a vector in R?

a) na.omit()
b) is.na()
c) na.check()
d) check.na()

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

a) It partitions data into subsets.
b) It removes elements that do not meet a condition.
c) It creates a subset of a data frame or matrix based on specified conditions.
d) It calculates the subset sum of a set.

9. What will the following R code output?

matrix(1:9, nrow=3, ncol=3)
a) A 3x3 matrix with numbers from 1 to 9
b) A 3x3 matrix filled with the number 1
c) A vector from 1 to 9
d) Error

10. How do you apply a function to each element of a list in R?

lapply(list(1, 2, 3), function(x) x * 2)
a) It multiplies each element by 2
b) It sums each element with 2
c) It reduces each element by 2
d) It divides each element by 2

11. What is R mainly used for?

a) Web development
b) Statistical analysis and graphics
c) Mobile applications
d) 3D gaming

12. How can you install a package in R?

a) package.install()
b) install.packages()
c) download.packages()
d) get.packages()

13. What will the following R code output?

df <- data.frame(numbers = 1:3, letters = c("a", "b", "c"))
print(nrow(df))
a) 3
b) 2
c) 1
d) Error

14. What is a data frame in R?

a) A 3D array structure
b) A storage area for numerical data
c) A tabular data structure
d) A type of list that holds constants

15. What will the following R code output?

x <- factor(c("yes", "no", "yes", "yes", "no"))
table(x)
a) yes: 3, no: 2
b) yes: 1, no: 1
c) 1: 3, 2: 2
d) None of the above

16. How do you create a simple linear model in R?

lm(data = mydata, formula = y ~ x)
a) By using the lm() function with a formula and data frame
b) By using the model() function with parameters
c) By manually calculating regression coefficients
d) By using the linear() function

17. What is the use of the summary() function in R?

a) It provides a concise summary of a data frame.
b) It summarizes the contents of a matrix.
c) It gives a detailed report of a model's performance.
d) All of the above

18. What will the following R code output?

set.seed(123)
runif(1)
a) A random number between 0 and 1
b) The same random number each time it is run
c) A random number between 1 and 10
d) Error

19. What does the merge() function do in R?

a) Merges two or more plots into one
b) Combines several strings into one
c) Merges two data frames by common columns or row names
d) Concatenates elements in a vector

20. How can you save a plot as a PDF in R?

pdf("myplot.pdf")
plot(1:10)
dev.off()
a) By opening a PDF device, plotting, and then closing the device
b) By printing a plot directly to a printer
c) By using the save() function
d) By using the export() function

21. What will the following R code output?

vec <- c(TRUE, FALSE, TRUE, FALSE)
sum(vec)
a) 2
b) TRUE
c) FALSE
d) Error

22. How do you select a specific column from a data frame in R?

a) Use the $ operator followed by the column name.
b) Use the @ operator followed by the column name.
c) Use the # operator followed by the column name.
d) Use the % operator followed by the column name.

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

a) To apply a function over the margins of an array or matrix
b) To apply a function to each element of a list
c) To compute the sum of array dimensions
d) To bind two arrays together

24. How do you create a histogram in R?

hist(c(1,2,2,3,4,5,5,5,6))
a) By using the hist() function
b) By using the plot() function
c) By using the barplot() function
d) By using the scatter() function

25. What is the RStudio?

a) A package in R for statistical computing
b) An integrated development environment for R
c) A data frame in R
d) A plotting tool in R

Comments