Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 12 MCQsR Tool
ECAP792 ·
60 questions. Pick an option to check it.
R is a programming language designed primarily for statistical computing, data analysis, and visualization.
Question 2 of 60 · Introduction to R · Easy
Which symbol is commonly used to assign a value to a variable in R?
Not quite. Try another option.
Answer: <-
The <- operator commonly assigns a value to a variable, as in x <- 10.
Question 3 of 60 · RStudio · Easy
What is RStudio?
Not quite. Try another option.
Answer: An integrated development environment for R
RStudio is an integrated development environment that provides tools for writing and running R code.
Question 4 of 60 · RStudio · Easy
Which RStudio pane normally displays commands and their immediate results?
Not quite. Try another option.
Answer: Console
The Console pane runs R commands and displays their immediate output.
Question 5 of 60 · Important R data structures · Easy
Which R data structure stores data in rows and columns while allowing columns to have different data types?
Not quite. Try another option.
Answer: Dataframe
A dataframe is tabular and can contain columns of different data types.
Question 6 of 60 · Important R data structures · Easy
Which R data structure is one-dimensional and usually contains elements of the same basic type?
Not quite. Try another option.
Answer: Vector
A vector is a one-dimensional structure whose elements normally share the same basic data type.
Question 7 of 60 · Vectors · Easy
Which R function combines values to create a vector?
Not quite. Try another option.
Answer: c()
The c() function combines values, such as c(2, 4, 6), into a vector.
Question 8 of 60 · Vectors · Easy
What is the result of length(c(5, 10, 15, 20))?
Not quite. Try another option.
Answer: 4
The vector contains four elements, so length() returns 4.
Question 9 of 60 · Vectors · Easy
Which expression selects the second element of a vector named scores?
Not quite. Try another option.
Answer: scores[2]
R uses square brackets for indexing, and indexing begins at 1.
Question 10 of 60 · Character strings · Easy
Which value is a valid character string in R?
Not quite. Try another option.
Answer: "Data Science"
Character strings in R are enclosed in quotation marks.
Question 11 of 60 · Character strings · Easy
Which function joins character strings together in R?
Not quite. Try another option.
Answer: paste()
The paste() function combines character strings into a single string.
Question 12 of 60 · Matrices · Easy
What is a matrix in R?
Not quite. Try another option.
Answer: A two-dimensional collection of one basic type
An R matrix has rows and columns, and all its elements share one basic data type.
Question 13 of 60 · Matrices · Easy
Which function is used to create a matrix in R?
Not quite. Try another option.
Answer: matrix()
The matrix() function creates a matrix from supplied values.
Question 14 of 60 · Matrices · Easy
Which expression selects the element in row 2 and column 3 of matrix m?
Not quite. Try another option.
Answer: m[2, 3]
Matrix indexing follows the form [row, column], so m[2, 3] selects row 2, column 3.
Question 15 of 60 · Lists · Easy
What is a key feature of an R list?
Not quite. Try another option.
Answer: It can store elements of different types
A list can contain different kinds of objects, including vectors, strings, matrices, and other lists.
Question 16 of 60 · Lists · Easy
Which function creates a list in R?
Not quite. Try another option.
Answer: list()
The list() function creates a list containing the supplied objects.
Question 17 of 60 · Dataframe · Easy
Which function creates a dataframe in R?
Not quite. Try another option.
Answer: data.frame()
The data.frame() function creates a dataframe from vectors or other suitable objects.
Question 18 of 60 · Dataframe · Easy
How can the column named age be accessed from a dataframe named students?
Not quite. Try another option.
Answer: students$age
The $ operator accesses a named column, so students$age returns the age column.
Question 19 of 60 · R programming structure · Easy
Which symbol begins a comment in R?
Not quite. Try another option.
Answer: #
R treats text following # on the same line as a comment.
Question 20 of 60 · R programming structure · Easy
Which keyword is used to define a function in R?
Not quite. Try another option.
Answer: function
R uses the function keyword, as in square <- function(x) x^2.
Question 21 of 60 · Introduction to R · Medium
What is returned by rep(seq(2, 6, by = 2), times = c(1, 2, 1))?
Not quite. Try another option.
Answer: c(2, 4, 4, 6)
seq() creates c(2, 4, 6), and the vector supplied to times repeats those elements 1, 2, and 1 times, respectively.
Question 22 of 60 · Introduction to R · Medium
Given x <- c(4, 7, 9, 12), what does x[-c(1, 3)] return?
Not quite. Try another option.
Answer: c(7, 12)
Negative indices exclude positions. Removing positions 1 and 3 leaves the values at positions 2 and 4.
Question 23 of 60 · RStudio · Medium
An RStudio project has its working directory set to the project root. What does read.csv("data/sales.csv") do?
Not quite. Try another option.
Answer: Reads the file relative to the project root
Relative paths are resolved from the current working directory, which is normally the project root in an RStudio project.
Question 24 of 60 · RStudio · Medium
While editing an R script in RStudio, which action runs only the selected code in the active R session?
Not quite. Try another option.
Answer: Use Run or press Ctrl+Enter
The Run command, commonly invoked with Ctrl+Enter, sends the selected code or current line to the console.
Question 25 of 60 · Important R data structures · Medium
Consider f <- factor(c("high", "low", "high"), levels = c("low", "high")). What does as.integer(f) return?
Not quite. Try another option.
Answer: c(2, 1, 2)
A factor stores integer codes based on level order. Here, low has code 1 and high has code 2.
Question 26 of 60 · Important R data structures · Medium
For f <- factor(c("A", "B", "A")), which pair correctly describes typeof(f) and class(f)?
Not quite. Try another option.
Answer: typeof(f) is integer; class(f) is factor
Factors are internally stored as integer vectors with a factor class and an associated set of levels.
Question 27 of 60 · Vectors · Medium
What is the result of c(1, 2) + c(10, 20, 30, 40)?
Not quite. Try another option.
Answer: c(11, 22, 31, 42)
R recycles c(1, 2) as c(1, 2, 1, 2) before performing element-wise addition.
Question 28 of 60 · Vectors · Medium
Given x <- c(10, 20, 30, 40, 50), what does x[c(TRUE, FALSE)] return?
Not quite. Try another option.
Answer: c(10, 30, 50)
The logical index is recycled to TRUE, FALSE, TRUE, FALSE, TRUE, selecting positions 1, 3, and 5.
Question 29 of 60 · Vectors · Medium
What is the storage type and value of c(TRUE, 2L, 3.5)?
Not quite. Try another option.
Answer: Double vector c(1, 2, 3.5)
R coerces all elements to the most flexible required atomic type. The double value causes logical and integer values to become doubles.
Question 30 of 60 · Character strings · Medium
What does paste(c("R", "Data", "Tool"), collapse = "-") return?
Not quite. Try another option.
Answer: "R-Data-Tool"
The collapse argument combines every element into one character string using - as the separator.
Question 31 of 60 · Character strings · Medium
What is returned by grepl("^[A-Z][0-9]+$", c("A12", "ab3", "B7"))?
Not quite. Try another option.
Answer: c(TRUE, FALSE, TRUE)
The pattern requires one uppercase letter followed by one or more digits, with no extra characters.
Question 32 of 60 · Matrices · Medium
Given m <- matrix(1:8, nrow = 2), what does m[, 3] return?
Not quite. Try another option.
Answer: c(5, 6)
Matrices are filled column by column by default, so the third column contains 5 and 6.
Question 33 of 60 · Matrices · Medium
Consider m <- matrix(1:6, nrow = 2, byrow = TRUE). What does apply(m, 2, sum) return?
Not quite. Try another option.
Answer: c(5, 7, 9)
With margin 2, apply() processes columns. The column sums are , , and .
Question 34 of 60 · Lists · Medium
Given z <- list(a = 10, b = c(2, 4)), which expression returns the numeric value 4?
Not quite. Try another option.
Answer: z[["b"]][2]
[["b"]] extracts the numeric vector stored in b, and [2] selects its second value.
Question 35 of 60 · Lists · Medium
What is the structure of lapply(c(1, 2, 3), function(v) v + 1)?
Not quite. Try another option.
Answer: A list containing 2, 3, and 4
lapply() applies the function to each element and always returns the results in a list.
Question 36 of 60 · Dataframe · Medium
Given df <- data.frame(name = c("Ana", "Bo", "Cy"), group = c("A", "B", "A"), score = c(80, 90, 65)), what does df[df$group == "A" & df$score >= 70, "name"] return?
Not quite. Try another option.
Answer: "Ana"
Ana belongs to group A and has a score of at least 70. Cy belongs to group A but has a score below 70.
Question 37 of 60 · Dataframe · Medium
Consider df <- data.frame(score = c(8, NA, 6)). After df$score[is.na(df$score)] <- mean(df$score, na.rm = TRUE), what is df$score?
Not quite. Try another option.
Answer: c(8, 7, 6)
The mean of the nonmissing scores is , which replaces the missing value.
Question 38 of 60 · R programming structure · Medium
What are the values of result and global x after running x <- 10; f <- function(y) { x <- 2; x + y }; result <- f(3)?
Not quite. Try another option.
Answer: result is 5 and x is 10
The assignment inside f() creates a local x. The function returns 5, while the global x remains 10.
Question 39 of 60 · R programming structure · Medium
Given grade <- function(x) ifelse(x >= 50, "pass", "fail"), what does grade(c(40, 50, 70)) return?
Not quite. Try another option.
Answer: c("fail", "pass", "pass")
ifelse() evaluates the condition element by element. Scores 50 and 70 satisfy the condition, while 40 does not.
Question 40 of 60 · R programming structure · Medium
What is the final value of total after total <- 0; for (i in 1:5) { if (i %% 2 == 0) next; total <- total + i }?
Not quite. Try another option.
Answer: 9
next skips the even values, so only 1, 3, and 5 are added: .
Question 41 of 60 · Introduction to R · Hard
What is the value of c(identical(1, 1L), typeof(1), typeof(1L)) in R?
Not quite. Try another option.
Answer: c(FALSE, "double", "integer")
1 is stored as a double, whereas 1L is an integer. They compare equal numerically, but identical() also requires matching types.
Question 42 of 60 · Introduction to R · Hard
Which statement correctly describes evaluating stats::filter(x, rep(1, 3)) when the stats package is not attached?
Not quite. Try another option.
Answer: It accesses the exported function without attaching stats.
The :: operator accesses an exported namespace object and loads the namespace if necessary, but it does not attach the package to the search path.
Question 43 of 60 · RStudio · Hard
Which statement about an RStudio Project is accurate?
Not quite. Try another option.
Answer: It normally uses the project root as the working directory.
Opening an RStudio Project establishes project-specific IDE context and normally starts in its root directory. Dependency isolation requires an additional tool such as renv.
Question 44 of 60 · Important R data structures · Hard
Given f <- factor(c("b", "a", "b"), levels = c("b", "a")), what does c(sum(as.integer(f)), paste(as.character(f), collapse = "")) return?
Not quite. Try another option.
Answer: c("4", "bab")
The factor codes are 1, 2, 1, whose sum is 4. Converting with as.character() returns the labels b, a, and b.
Question 45 of 60 · Important R data structures · Hard
What value is returned by the final expression?
e1 <- new.env()
e1$x <- 1
e2 <- e1
e2$x <- 8
e1$x
Not quite. Try another option.
Answer: 8
Environments have reference semantics. Both names refer to the same environment, so changing e2$x also changes the value observed through e1$x.
Question 46 of 60 · Vectors · Hard
For x <- c(NA_real_, NaN, Inf, -Inf, 0), what is c(sum(is.na(x)), sum(is.nan(x)), sum(is.finite(x)))?
Not quite. Try another option.
Answer: c(2, 1, 1)
Both NA and NaN satisfy is.na(), only NaN satisfies is.nan(), and only 0 is finite.
Question 47 of 60 · Vectors · Hard
Given x <- c(a = 10, b = 20, a = 30), what does x[c("a", "a")] return?
Not quite. Try another option.
Answer: c(a = 10, a = 10)
Each character subscript independently matches the first occurrence of the name a; duplicate names do not cause successive matches.
Question 48 of 60 · Vectors · Hard
After x <- 1:3; x[5] <- 9, which description of x is correct?
Not quite. Try another option.
Answer: It is the double vector c(1, 2, 3, NA, 9).
Assigning beyond the current length extends the vector and fills the gap with NA. The double value 9 coerces the original integer vector to double.
Question 49 of 60 · Character strings · Hard
What does sub(".", "-", "a.b.c", fixed = TRUE) return?
Not quite. Try another option.
Answer: "a-b.c"
With fixed = TRUE, the dot is treated literally rather than as a regular-expression wildcard. sub() replaces only its first occurrence.
Question 50 of 60 · Character strings · Hard
What is returned by strsplit("a,b,", ",", fixed = TRUE)[[1]]?
Not quite. Try another option.
Answer: c("a", "b")
strsplit() does not include an empty field produced solely by a match at the end of the input string.
Question 51 of 60 · Matrices · Hard
Given m <- matrix(1:6, nrow = 2), what is m[cbind(c(2, 1, 2), c(1, 3, 2))]?
Not quite. Try another option.
Answer: c(2, 5, 4)
A two-column index matrix selects row-column pairs. The selected cells are m[2,1], m[1,3], and m[2,2].
Question 52 of 60 · Matrices · Hard
For m <- matrix(1:6, nrow = 2), which pair describes m[1, , drop = FALSE]?
Not quite. Try another option.
Answer: A matrix with dimensions c(1, 3)
drop = FALSE preserves the two-dimensional matrix structure even though only one row is selected.
Question 53 of 60 · Lists · Hard
Given z <- list(a = list(b = 1:3), b = 4), what does z[[c("a", "b")]] return?
Not quite. Try another option.
Answer: 1:3
A vector supplied to [[ performs recursive indexing, equivalent here to z[["a"]][["b"]].
Question 54 of 60 · Lists · Hard
With default R options and x <- list(alpha = 7, beta = 9), which expression evaluates to TRUE?
Not quite. Try another option.
Answer: is.null(x[["al"]]) && identical(x$al, 7)
[[ uses exact name matching by default, whereas $ permits an unambiguous partial match under default options.
Question 55 of 60 · Dataframe · Hard
Given d <- data.frame(x = 1:3, y = 4:6), what is the result of c(is.data.frame(d["x"]), is.data.frame(d[, "x"]), is.data.frame(d[, "x", drop = FALSE]))?
Not quite. Try another option.
Answer: c(TRUE, FALSE, TRUE)
Single-bracket column selection returns a data frame. Two-dimensional selection simplifies one column to a vector unless drop = FALSE is specified.
Question 56 of 60 · Dataframe · Hard
Under the default check.names = TRUE, what is names(data.frame(x = 1:2, x = 3:4))?
Not quite. Try another option.
Answer: c("x", "x.1")
data.frame() makes duplicate column names syntactically valid and unique, retaining the first x and renaming the second to x.1.
Question 57 of 60 · R programming structure · Hard
What does the following code return?
x <- 10
f <- function(y = x) {
x <- 20
y
}
f()
Not quite. Try another option.
Answer: 20
A default argument expression is evaluated lazily in the function's evaluation environment. By the time y is forced, the local x is 20.
Question 58 of 60 · R programming structure · Hard
What is returned by the final expression?
make_counter <- function() {
x <- 0
function() { x <<- x + 1; x }
}
a <- make_counter()
b <- make_counter()
c(a(), a(), b())
Not quite. Try another option.
Answer: c(1, 2, 1)
Each call to make_counter() creates a separate enclosing environment. Calls to a share one counter, while b has an independent counter.
Question 59 of 60 · R programming structure · Hard
What happens when this code is evaluated?
f <- function(x) 1
f(stop("boom"))
Not quite. Try another option.
Answer: It returns 1 without an error.
Function arguments are lazy promises. Because f never evaluates x, the call to stop() is never executed.
Question 60 of 60 · R programming structure · Hard
What is returned by this code?
make_funs <- function() {
fs <- vector("list", 3)
for (i in 1:3) fs[[i]] <- function() i
fs
}
sapply(make_funs(), function(f) f())
Not quite. Try another option.
Answer: c(3, 3, 3)
All closures share the same make_funs() environment and look up the same loop variable i, whose final value is 3.
That's unit 12
Answer a few questions and your score shows up here.
Nearly through. Did unit 12 of ECAP792 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 12 of ECAP792 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.