MCQs on Data Types and Structures | R

In data science, understanding the key data structures like Vectors, Matrices, Lists, and Data Frames, along with handling factors, strings, and missing data, is essential for working with data in R programming.


Chapter: Data Types and Structures – MCQs

1. Vectors, Matrices, Lists, and Data Frames

  1. In R, what is a vector?
    • a) A data structure that can hold only one data type
    • b) A matrix that stores numeric values
    • c) A list that can contain mixed data types
    • d) A data frame with rows and columns
  2. Which of the following functions creates a vector in R?
    • a) list()
    • b) data.frame()
    • c) vector()
    • d) c()
  3. How are matrices created in R?
    • a) By combining vectors of equal length
    • b) By using matrix() function
    • c) By using the list() function
    • d) By adding vectors together
  4. What does the dim() function return when applied to a matrix?
    • a) The data type of the matrix
    • b) The dimensions (rows and columns) of the matrix
    • c) The size of the matrix
    • d) The length of the matrix
  5. What is the primary difference between a list and a vector in R?
    • a) A list can only hold one data type, while a vector can hold multiple types
    • b) A list can hold multiple data types, whereas a vector holds one type
    • c) A list is a type of matrix, whereas a vector is a type of data frame
    • d) A vector is a linear collection, while a list is a non-linear collection
  6. Which function is used to convert a vector into a data frame?
    • a) as.data.frame()
    • b) as.vector()
    • c) convert()
    • d) df.convert()
  7. How do you access an element in a matrix at row 2 and column 3 in R?
    • a) matrix[2,3]
    • b) matrix[2][3]
    • c) matrix(2,3)
    • d) matrix[3,2]
  8. Which of the following is true about a data frame in R?
    • a) A data frame is a list of vectors of equal length
    • b) A data frame can hold only numeric data
    • c) A data frame is similar to a matrix but can contain multiple types of data
    • d) A data frame can only be created with the matrix() function
  9. Which function would you use to extract the first column from a data frame in R?
    • a) df[,1]
    • b) df[1]
    • c) df[[1]]
    • d) df$1
  10. How do you find the number of rows and columns of a data frame in R?
    • a) dim(df)
    • b) nrow(df)
    • c) ncol(df)
    • d) length(df)

2. Factors and Strings

  1. What is a factor in R?
    • a) A numerical value used to categorize data
    • b) A categorical variable that can take a fixed number of levels
    • c) A data structure similar to a vector but with characters only
    • d) A string of characters
  2. Which function is used to create a factor in R?
    • a) factor()
    • b) as.factor()
    • c) as.numeric()
    • d) string()
  3. How does R internally store factors?
    • a) As numeric indices with levels attached
    • b) As strings only
    • c) As logical values
    • d) As arrays
  4. Which of the following is a characteristic of factors in R?
    • a) They can store numeric data only
    • b) They are always unordered
    • c) They have a fixed set of levels
    • d) They can be used only with categorical variables
  5. How do you convert a factor to a character vector in R?
    • a) as.character()
    • b) as.factor()
    • c) convert.to.character()
    • d) toChar()
  6. Which function in R is used to check if a variable is a factor?
    • a) is.factor()
    • b) factor()
    • c) is.character()
    • d) is.list()
  7. How can you find the levels of a factor in R?
    • a) levels(factor)
    • b) levels()
    • c) factor.levels()
    • d) list(factor)
  8. Which of the following statements is true about strings in R?
    • a) Strings are always factors in R
    • b) Strings can be converted into factors if needed
    • c) Strings cannot contain spaces
    • d) Strings are stored as numeric values internally
  9. How do you concatenate two strings in R?
    • a) concatenate()
    • b) string.concat()
    • c) paste()
    • d) concat()
  10. What is the result of substring("Hello World", 1, 5) in R?
    • a) Hello
    • b) World
    • c) Hello World
    • d) Hello

3. Handling Missing Data

  1. What does NA represent in R?
    • a) A value that is undefined or missing
    • b) A null pointer
    • c) A zero value
    • d) A string
  2. Which function is used to check for missing values (NA) in R?
    • a) is.na()
    • b) missing()
    • c) check.na()
    • d) na.exists()
  3. How do you remove rows with missing values from a data frame in R?
    • a) na.omit()
    • b) remove.na()
    • c) drop.na()
    • d) filter.na()
  4. What does na.rm = TRUE do in most R functions?
    • a) Removes any NA values from the computation
    • b) Replaces NA with 0
    • c) Ignores NA values during computation
    • d) Removes missing values from the dataset
  5. How can you replace missing values (NA) in R with a specific value?
    • a) replace()
    • b) fill.na()
    • c) na.replace()
    • d) set.na()
  6. What is the default behavior when you attempt to perform mathematical operations on a vector with NA values?
    • a) The operation will be skipped
    • b) It will return a NA as the result
    • c) The NA values will be treated as zero
    • d) The result will be a vector without NA
  7. Which function would you use to check if any NA values exist in a vector?
    • a) anyNA()
    • b) check.na()
    • c) contains.na()
    • d) is.na.any()
  8. What does the na.pass() function do in R?
    • a) Passes NA values through without any modification
    • b) Replaces NA with a placeholder value
    • c) Filters out rows containing NA values
    • d) Converts NA values into zero
  9. How do you count the number of missing values in a data frame in R?
    • a) sum(is.na(df))
    • b) count.na(df)
    • c) na.count(df)
    • d) na.sum(df)
  10. Which of the following functions fills in missing data using interpolation in R?
    • a) na.approx()
    • b) na.fill()
    • c) fillna()
    • d) na.interpolate()

Answers Table

QnoAnswer
1a) A data structure that can hold only one data type
2d) c()
3b) By using matrix() function
4b) The dimensions (rows and columns) of the matrix
5b) A list can hold multiple data types, whereas a vector holds one type
6a) as.data.frame()
7a) matrix[2,3]
8c) A data frame is similar to a matrix but can contain multiple types of data
9a) df[,1]
10a) dim(df)
11b) A categorical variable that can take a fixed number of levels
12a) factor()
13a) As numeric indices with levels attached
14c) They have a fixed set of levels
15a) as.character()
16a) is.factor()
17a) levels(factor)
18b) Strings can be converted into factors if needed
19c) paste()
20a) Hello
21a) A value that is undefined or missing
22a) is.na()
23a) na.omit()
24a) Removes any NA values from the computation
25a) replace()
26b) It will return a NA as the result
27a) anyNA()
28a) Passes NA values through without any modification
29a) sum(is.na(df))
30a) na.approx()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top