R Programming MCQ

R is a versatile language primarily used for statistical analysis and data visualization. With its extensive library support and flexibility, R has cemented its place as an essential tool for data scientists and researchers. Dive into this set of 25 multiple-choice questions to test and expand your R programming knowledge!

1. What is R primarily used for?

a) Web Development
b) Mobile App Development
c) Statistical Analysis and Data Visualization
d) System Programming

Answer:

c) Statistical Analysis and Data Visualization

Explanation:

While R can have other applications, its primary use is for statistics and data visualization.

2. Which function is used to install packages in R?

a) install.package()
b) install.packages()
c) library()
d) require()

Answer:

b) install.packages()

Explanation:

install.packages() is used to install new packages in R. The other functions are used to load or check packages.

3. How do you load a package in R?

a) load(packageName)
b) source(packageName)
c) require(packageName)
d) use(packageName)

Answer:

c) require(packageName)

Explanation:

require() is used to load a package in R. If the package isn't installed, it returns FALSE.

4. What is the correct way to load the ggplot2 package?

a) require("ggplot2")
b) library(ggplot2)
c) load.ggplot2()
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

Both require("ggplot2") and library(ggplot2) are correct methods to load the ggplot2 package in R.

5. Which data structure in R can hold elements of different types?

a) Vector
b) Matrix
c) Array
d) List

Answer:

d) List

Explanation:

Lists in R can hold elements of different types, while vectors, matrices, and arrays hold elements of the same type.

6. How do you create a vector c(1,2,3,4) in R?

a) vector(1:4)
b) 1:4
c) v = c(1,2,3,4)
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

Both 1:4 and v = c(1,2,3,4) create the desired vector.

7. In R, what is the output of the expression c(1,2,3) + c(1,1,1)?

a) c(1,1,1)
b) c(2,2,2)
c) c(2,3,4)
d) Error

Answer:

c) c(2,3,4)

Explanation:

Vector addition in R is element-wise, so 1+1=2, 2+1=3, and 3+1=4.

8. Which function is used to check if an R package is installed?

a) is.installed()
b) check_installed()
c) installed()
d) require()

Answer:

d) require()

Explanation:

While require() primarily loads a package, it returns FALSE if the package isn't installed, effectively checking its presence.

9. Which of the following is NOT a valid variable name in R?

a) .data
b) _data
c) data1
d) 1data

Answer:

d) 1data

Explanation:

Variable names in R cannot start with a number.

10. Which base R function provides the structure of an R object?

a) str()
b) desc()
c) structure()
d) describe()

Answer:

a) str()

Explanation:

str() gives the structure of an R object, detailing the type, length, and content.

11. What is the default data visualization package in R?

a) lattice
b) plotly
c) shiny
d) base graphics

Answer:

d) base graphics

Explanation:

R's base graphics system is the default plotting system.

12. What does the apply() function do in R?

a) Applies a function across elements of a matrix
b) Applies a function across elements of a list
c) Applies a function across elements of a vector
d) None of the above

Answer:

a) Applies a function across elements of a matrix

Explanation:

apply() is used to apply a function across rows or columns of a matrix.

13. Which function is used to combine multiple vectors into a data frame?

a) c()
b) merge()
c) combine()
d) data.frame()

Answer:

d) data.frame()

Explanation:

data.frame() is used to combine multiple vectors into a single data frame.

14. What is the output of class(NA) in R?

a) "numeric"
b) "logical"
c) "NA"
d) "NULL"

Answer:

b) "logical"

Explanation:

By default, NA (representing missing data) is of the logical type.

15. Which R function is used for statistical hypothesis tests?

a) stat.test()
b) hypothesis()
c) t.test()
d) stats()

Answer:

c) t.test()

Explanation:

t.test() is a commonly used function for t-tests, which are types of statistical hypothesis tests.

16. Which function in R is used to seed the random number generator to ensure reproducibility?

a) set.seed()
b) seed.set()
c) random.seed()
d) rand.set()

Answer:

a) set.seed()

Explanation:

set.seed() initializes the random number generator, ensuring consistent and reproducible results.

17. What does the rnorm() function do in R?

a) Generate random numbers from a normal distribution
b) Generate random numbers from a uniform distribution
c) Test if numbers are from a normal distribution
d) Normalize vectors

Answer:

a) Generate random numbers from a normal distribution

Explanation:

rnorm() generates random numbers following a normal distribution.

18. How do you access the second element of a list named my_list?

a) my_list[2]
b) my_list[[2]]
c) my_list$2
d) my_list.2

Answer:

b) my_list[[2]]

Explanation:

Lists in R are accessed using double square brackets.

19. Which of the following is true about R's data frames?

a) Rows represent variables, and columns represent observations.
b) Rows represent observations, and columns represent variables.
c) Data frames can only have numeric data.
d) Data frames cannot have missing values.

Answer:

b) Rows represent observations, and columns represent variables.

Explanation:

In R's data frames, each row is an observation and each column is a variable or feature.

20. Which R function is used to handle exceptions?

a) handle()
b) exception()
c) tryCatch()
d) onError()

Answer:

c) tryCatch()

Explanation:

tryCatch() provides exception handling by capturing errors and allowing for recovery actions.

21. Which of the following R functions is used to get the class of an object?

a) class()
b) type()
c) typeof()
d) Both a) and c)

Answer:

d) Both a) and c)

Explanation:

In R, both class() and typeof() provide information about the type or class of an object, but they can sometimes return different results based on the underlying data structure.

22. Which base R function returns a logical vector indicating if each element is missing?

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

Answer:

a) is.na()

Explanation:

is.na() checks each element of an object and returns a logical vector indicating if the elements are NA (missing).

23. What does the lapply() function return?

a) Vector
b) List
c) Matrix
d) Data Frame

Answer:

b) List

Explanation:

lapply() returns a list of the same length as the input. It applies a function to each element of a list or vector.

24. Which R function is used to export data frames to .csv files?

a) write.csv()
b) export.csv()
c) save.csv()
d) to.csv()

Answer:

a) write.csv()

Explanation:

write.csv() is the function used to write a data frame to a .csv file in R.

25. How do you subset rows of a data frame df where the column 'age' is greater than 30?

a) df[df$age > 30, ]
b) subset(df, age > 30)
c) df[age > 30]
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

Both df[df$age > 30, ] and subset(df, age > 30) are valid methods to subset rows based on a condition in R.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top