R Quiz - MCQ - Multiple Choice Questions

Welcome to the R programming Quiz. Here, you can find 50+ multiple-choice questions (MCQs) covering various aspects of the R programming language. These questions span a wide range of topics within R, including fundamental concepts, syntax, functions, data types, and specific programming practices. Each question is formulated to test understanding and proficiency in R programming, making this collection suitable for learners at different levels, from beginners to intermediate users. The questions also come with answers and brief explanations to reinforce learning and provide insight into the reasoning behind the correct choices.

This set encompasses questions on basic operations, vector manipulation, data frames, functions, control structures, and statistical operations, among others. It's designed to serve as a comprehensive resource for self-assessment, educational purposes, exam preparation, or even as a teaching aid. Whether you're looking to test your understanding of R, prepare for an exam, or construct an educational activity, these MCQs offer a broad overview of the language's capabilities and common programming patterns encountered in data analysis and statistical computing with R.

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

a) To create a conditional statement
b) To combine values into a vector or list
c) To clear the console
d) To compute the cosine of a number

2. How do you check the structure of an object in R?

a) obj()
b) structure()
c) str()
d) typeof()

3. Which of the following is used to install packages in R?

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

4. What does the lapply() function do?

a) Applies a function over a list and returns a list
b) Applies a function to each row of a dataframe
c) Linear approximation
d) Lazy evaluation of an expression

5. How do you create a dataframe in R?

a) dataframe()
b) as.dataframe()
c) data.frame()
d) create.df()

6. Which function is used to read a CSV file into R?

a) read.csv()
b) read_csv()
c) load.csv()
d) import.csv()

7. What symbol is used to assign a value to a variable in R?

a) =
b) :=
c) <-
d) <<

8. Which function can be used for generating sequence of numbers in R?

a) seq()
b) series()
c) sequence()
d) range()

9. What does the ggplot2 package in R primarily provide?

a) Advanced mathematical functions
b) Data manipulation capabilities
c) Tools for creating data visualizations
d) Database connection utilities

10. Which of the following functions is used to apply a function to each column of a dataframe or matrix?

a) lapply()
b) sapply()
c) apply()
d) colApply()

11. How do you subset a dataframe in R based on condition?

a) subset(df, condition)
b) df[condition]
c) df$column[condition]
d) filter(df, condition)

12. What is the output type of the summary() function when applied to a dataframe?

a) A detailed report in the console
b) Another dataframe
c) A list containing summary statistics for each column
d) A numeric vector

13. Which package in R is most commonly used for data manipulation?

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

14. How can you create a scatter plot in R?

a) plot(x, y, type = "s")
b) plot(x, y, type = "b")
c) plot(x, y, type = "p")
d) plot(x, y, type = "l")

15. What does the t() function perform on a matrix in R?

a) Transforms its values
b) Truncates its dimensions
c) Transposes it
d) Tests its type

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

a) .varName
b) _varName
c) var.Name
d) varName1

17. Which data type does NOT exist in R?

a) Character
b) Integer
c) Short
d) Complex

18. How do you concatenate two strings in R?

a) concat(str1, str2)
b) paste(str1, str2)
c) str1 + str2
d) join(str1, str2)

19. How can you define a function in R that calculates the square of a number?

a) square <- function(x) { return x^2 }
b) function square(x) = x^2
c) def square(x): return x**2
d) square(x): x^2

20. Which loop structure is NOT present in R?

a) for
b) while
c) do-while
d) repeat

21. Which function is used to create a list containing elements of different types in R?

a) vector()
b) c()
c) list()
d) matrix()

22. How do you define an array with 2 rows and 3 columns in R?

a) array(1:6, dim = c(2, 3))
b) matrix(1:6, nrow = 2, ncol = 3)
c) dataframe(1:6, rows = 2, cols = 3)
d) vector(1:6, length = 6)

23. What function do you use to add a column to a dataframe in R?

a) append()
b) attach()
c) cbind()
d) merge()

24. How do you access the third element of a vector in R?

a) vector[3]
b) vector(3)
c) get(vector, 3)
d) vector.third

25. Which of the following is true about R lists?

a) Lists in R can only hold elements of the same type.
b) Lists in R are a type of vector.
c) Lists in R cannot contain other lists.
d) Lists in R can hold elements of different types.

26. What is the result of combining a vector of characters and a vector of numbers with the c() function?

a) A vector of characters
b) A vector of numbers
c) An error
d) A list

27. In R, what is the purpose of the NA value?

a) It represents a missing or undefined value.
b) It is a placeholder for future assignment.
c) It denotes a null pointer or reference.
d) It is used to terminate loops prematurely.

28. Which function allows you to apply a function conditionally across elements of a list in R?

a) lapply()
b) sapply()
c) apply()
d) ifelse()

29. How do you remove NA values from a vector in R?

a) na.omit(vector)
b) vector[!is.na(vector)]
c) remove.na(vector)
d) vector[-na]

30. What is the default mode of a vector created by the vector() function without specifying the mode?

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

31. Which R operator is used for element-wise multiplication of two vectors?

a) *
b) %*%
c) ^
d) %/%

32. What is the primary way to handle exceptions in R?

a) try-catch blocks
b) if-else statements
c) stop() function
d) try() function

33. How do you split a string into individual characters in R?

a) split(str, "")
b) strsplit(str, "")
c) separate(str, "")
d) chars(str)

34. Which R function is used to check if a value is NA?

a) is.na(value)
b) is.null(value)
c) is.missing(value)
d) is.undefined(value)

35. How can you repeat a vector c(1,2,3) three times in R?

a) rep(c(1,2,3), times=3)
b) repeat(c(1,2,3), 3)
c) cycle(c(1,2,3), 3)
d) loop(c(1,2,3), 3)

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

a) It initializes the random number generator to a specific state.
b) It creates a unique identifier for each session.
c) It seeds the environment with variables.
d) It determines the precision of floating-point operations.

37. Which statement creates a factor in R with three levels?

a) factor(c("low", "medium", "high"))
b) c("low", "medium", "high")
c) list("low", "medium", "high")
d) array(c("low", "medium", "high"))

38. How do you convert a factor to a numeric vector in R?

a) as.numeric(as.character(factor))
b) numeric(factor)
c) to.numeric(factor)
d) convert(factor, "numeric")

39. In R, which function can be used to merge two data frames by a common column?

a) merge(df1, df2, by="commonColumn")
b) concatenate(df1, df2, on="commonColumn")
c) join(df1, df2, key="commonColumn")
d) bind(df1, df2, together="commonColumn")

40. How is a comment indicated in R code?

a) #
b) //
c) /*
d) --

41. Which of the following is a valid way to declare a matrix with 2 rows and 3 columns filled with zeros?

a) matrix(0, nrow=2, ncol=3)
b) zeros(2,3)
c) matrix([0,0,0,0,0,0], nrow=2)
d) array(0, dim=c(2,3))

42. What is the result of the expression is.vector(list(1,2,3)) in R?

a) TRUE
b) FALSE
c) Error
d) NULL

43. How can you save the workspace at the end of an R session?

a) save.workspace()
b) save.session()
c) save.image()
d) save.env()

44. Which of the following functions can be used to generate a sequence that progresses from 1 to 10 in steps of 2 in R?

a) seq(1, 10, by=2)
b) sequence(1, 10, step=2)
c) generate(1, 10, 2)
d) range(1, 10, increment=2)

45. How do you create a matrix in R where the numbers 1 through 9 are arranged in 3 rows and 3 columns?

a) matrix(1:9, nrow=3, ncol=3)
b) array(1:9, dim=c(3,3))
c) build.matrix(1:9, rows=3, columns=3)
d) cbind(1:3, 4:6, 7:9)

46. What does the dim() function return when applied to a dataframe in R?

a) The number of elements in the dataframe
b) The number of columns in the dataframe
c) A vector containing the number of rows and columns
d) The length of the dataframe

47. Which of the following syntaxes removes a variable from the R environment?

a) del(var)
b) unset(var)
c) remove(var)
d) rm(var)

48. Which function is used to round numbers to the nearest integer in R?

a) round()
b) int()
c) floor()
d) ceil()

49. How do you calculate the mean of a numeric vector in R?

a) mean(vector)
b) average(vector)
c) sum(vector) / length(vector)
d) median(vector)

50. Which statement is used to import the ggplot2 library in an R script?

a) import ggplot2
b) include("ggplot2")
c) library(ggplot2)
d) use ggplot2

51. What is the result of executing is.logical(c(TRUE, FALSE)) in R?

a) TRUE
b) FALSE
c) Error
d) NULL

52. How do you subset a dataframe df to only include rows where the value of the column age is greater than 30?

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

53. In R, how can you generate a random sample of 3 numbers from the vector c(1,2,3,4,5)?

a) sample(c(1,2,3,4,5), 3)
b) random(c(1,2,3,4,5), 3)
c) choose(c(1,2,3,4,5), 3)
d) pick(c(1,2,3,4,5), 3)

54. What does the rnorm() function generate in R?

a) A sequence of rational numbers
b) A random permutation of numbers
c) A vector of random numbers following a normal distribution
d) A repeat sequence of numbers

55. Which keyword is used to define a global variable inside a function in R?

a) global
b) <<-
c) ::
d) globalVar

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare