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
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
Which of the following functions creates a vector in R?
a) list()
b) data.frame()
c) vector()
d) c()
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
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
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
Which function is used to convert a vector into a data frame?
a) as.data.frame()
b) as.vector()
c) convert()
d) df.convert()
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]
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
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
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
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
Which function is used to create a factor in R?
a) factor()
b) as.factor()
c) as.numeric()
d) string()
How does R internally store factors?
a) As numeric indices with levels attached
b) As strings only
c) As logical values
d) As arrays
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
How do you convert a factor to a character vector in R?
a) as.character()
b) as.factor()
c) convert.to.character()
d) toChar()
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()
How can you find the levels of a factor in R?
a) levels(factor)
b) levels()
c) factor.levels()
d) list(factor)
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
How do you concatenate two strings in R?
a) concatenate()
b) string.concat()
c) paste()
d) concat()
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
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
Which function is used to check for missing values (NA) in R?
a) is.na()
b) missing()
c) check.na()
d) na.exists()
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()
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
How can you replace missing values (NA) in R with a specific value?
a) replace()
b) fill.na()
c) na.replace()
d) set.na()
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
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()
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
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)
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
Qno
Answer
1
a) A data structure that can hold only one data type
2
d) c()
3
b) By using matrix() function
4
b) The dimensions (rows and columns) of the matrix
5
b) A list can hold multiple data types, whereas a vector holds one type
6
a) as.data.frame()
7
a) matrix[2,3]
8
c) A data frame is similar to a matrix but can contain multiple types of data
9
a) df[,1]
10
a) dim(df)
11
b) A categorical variable that can take a fixed number of levels
12
a) factor()
13
a) As numeric indices with levels attached
14
c) They have a fixed set of levels
15
a) as.character()
16
a) is.factor()
17
a) levels(factor)
18
b) Strings can be converted into factors if needed
19
c) paste()
20
a) Hello
21
a) A value that is undefined or missing
22
a) is.na()
23
a) na.omit()
24
a) Removes any NA values from the computation
25
a) replace()
26
b) It will return a NA as the result
27
a) anyNA()
28
a) Passes NA values through without any modification