Unit 12: R Tool - Practice Quiz
1 What is R primarily used for?
2 Which symbol is commonly used to assign a value to a variable in R?
!=
==
<-
>=
3 What is RStudio?
4 Which RStudio pane normally displays commands and their immediate results?
5 Which R data structure stores data in rows and columns while allowing columns to have different data types?
6 Which R data structure is one-dimensional and usually contains elements of the same basic type?
7 Which R function combines values to create a vector?
print()
dim()
sum()
c()
8
What is the result of length(c(5, 10, 15, 20))?
4
3
20
50
9
Which expression selects the second element of a vector named scores?
scores{2}
scores<2>
scores(2)
scores[2]
10 Which value is a valid character string in R?
Data Science
{Data Science}
"Data Science"
<Data Science>
11 Which function joins character strings together in R?
paste()
matrix()
length()
factor()
12 What is a matrix in R?
13 Which function is used to create a matrix in R?
data.frame()
list()
vector()
matrix()
14
Which expression selects the element in row 2 and column 3 of matrix m?
m(2, 3)
m[[2, 3]]
m[3, 2]
m[2, 3]
15 What is a key feature of an R list?
16 Which function creates a list in R?
table()
array()
levels()
list()
17 Which function creates a dataframe in R?
as.vector()
data.frame()
character()
is.matrix()
18
How can the column named age be accessed from a dataframe named students?
students$age
students@age
students#age
students%age
19 Which symbol begins a comment in R?
**
<!--
//
#
20 Which keyword is used to define a function in R?
define
function
method
procedure
21
What is returned by rep(seq(2, 6, by = 2), times = c(1, 2, 1))?
c(2, 4, 4, 6)
c(2, 2, 4, 6)
c(2, 4, 6, 6)
c(2, 2, 4, 4, 6, 6)
22
Given x <- c(4, 7, 9, 12), what does x[-c(1, 3)] return?
c(4, 9)
c(7, 12)
c(7, 9, 12)
c(4, 7, 9)
23
An RStudio project has its working directory set to the project root. What does read.csv("data/sales.csv") do?
24 While editing an R script in RStudio, which action runs only the selected code in the active R session?
25
Consider f <- factor(c("high", "low", "high"), levels = c("low", "high")). What does as.integer(f) return?
c(1, 1, 2)
c(1, 2, 1)
c(2, 1, 2)
c(2, 2, 1)
26
For f <- factor(c("A", "B", "A")), which pair correctly describes typeof(f) and class(f)?
typeof(f) is list; class(f) is character
typeof(f) is factor; class(f) is integer
typeof(f) is character; class(f) is factor
typeof(f) is integer; class(f) is factor
27
What is the result of c(1, 2) + c(10, 20, 30, 40)?
c(11, 22, 31, 42)
c(11, 22, 30, 40)
c(10, 40, 30, 80)
c(11, 21, 32, 42)
28
Given x <- c(10, 20, 30, 40, 50), what does x[c(TRUE, FALSE)] return?
c(20, 40)
c(10, 30, 50)
c(10, 20)
c(30, 40, 50)
29
What is the storage type and value of c(TRUE, 2L, 3.5)?
c(TRUE, TRUE, TRUE)
c(1, 2, 3.5)
c(1L, 2L, 3L)
c("TRUE", "2", "3.5")
30
What does paste(c("R", "Data", "Tool"), collapse = "-") return?
"R-Data-Tool"
c("R", "Data", "Tool")
c("R-", "Data-", "Tool")
"R Data Tool"
31
What is returned by grepl("^[A-Z][0-9]+$", c("A12", "ab3", "B7"))?
c(TRUE, FALSE, TRUE)
c(TRUE, TRUE, FALSE)
c(FALSE, TRUE, TRUE)
c(FALSE, FALSE, TRUE)
32
Given m <- matrix(1:8, nrow = 2), what does m[, 3] return?
c(3, 4)
c(5, 6)
c(3, 7)
c(2, 6)
33
Consider m <- matrix(1:6, nrow = 2, byrow = TRUE). What does apply(m, 2, sum) return?
c(6, 15)
c(5, 7, 9)
c(4, 6, 8)
c(3, 7, 11)
34
Given z <- list(a = 10, b = c(2, 4)), which expression returns the numeric value 4?
z[["b"]][2]
z["b"][2]
z$b[1]
z[[2]][1]
35
What is the structure of lapply(c(1, 2, 3), function(v) v + 1)?
36
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?
"Bo"
c("Ana", "Cy")
"Ana"
"Cy"
37
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?
c(8, 6, 6)
c(8, NA, 6)
c(8, 7, 6)
c(8, 8, 6)
38
What are the values of result and global x after running x <- 10; f <- function(y) { x <- 2; x + y }; result <- f(3)?
result is 13 and x is 2
result is 5 and x is 2
result is 13 and x is 10
result is 5 and x is 10
39
Given grade <- function(x) ifelse(x >= 50, "pass", "fail"), what does grade(c(40, 50, 70)) return?
c("pass", "pass", "pass")
c("fail", "fail", "pass")
c("fail", "pass", "pass")
c("pass", "fail", "fail")
40
What is the final value of total after total <- 0; for (i in 1:5) { if (i %% 2 == 0) next; total <- total + i }?
6
12
9
15
41
What is the value of c(identical(1, 1L), typeof(1), typeof(1L)) in R?
c(FALSE, "numeric", "integer")
c(FALSE, "double", "integer")
c(TRUE, "double", "integer")
c(TRUE, "numeric", "numeric")
42
Which statement correctly describes evaluating stats::filter(x, rep(1, 3)) when the stats package is not attached?
library(stats) must run first.
stats temporarily for this expression.
stats into the global environment before resolving the requested function.
stats.
43 Which statement about an RStudio Project is accurate?
44
Given f <- factor(c("b", "a", "b"), levels = c("b", "a")), what does c(sum(as.integer(f)), paste(as.character(f), collapse = "")) return?
c("3", "bab")
c("5", "aba")
c("4", "bab")
c("4", "121")
45
What value is returned by the final expression?
e1 <- new.env()
e1$x <- 1
e2 <- e1
e2$x <- 8
e1$x
NULL
8
numeric(0)
1
46
For x <- c(NA_real_, NaN, Inf, -Inf, 0), what is c(sum(is.na(x)), sum(is.nan(x)), sum(is.finite(x)))?
c(1, 1, 3)
c(1, 1, 1)
c(2, 2, 1)
c(2, 1, 1)
47
Given x <- c(a = 10, b = 20, a = 30), what does x[c("a", "a")] return?
c(a = 10, a = 10)
c(a = 10, a = 30)
c(a = 10, b = 20)
c(a = 30, a = 30)
48
After x <- 1:3; x[5] <- 9, which description of x is correct?
4 has not previously been allocated.
c(1, 2, 3, 0, 9).
c(1, 2, 3, NA, 9).
c(1L, 2L, 3L, NA, 9L).
49
What does sub(".", "-", "a.b.c", fixed = TRUE) return?
"a-b.c"
"a.b-c"
"-a.b.c"
"a-b-c"
50
What is returned by strsplit("a,b,", ",", fixed = TRUE)[[1]]?
c("a", "b")
NA_character_ to represent the trailing separator.
c("a", "b", "")
c("a,b", "")
51
Given m <- matrix(1:6, nrow = 2), what is m[cbind(c(2, 1, 2), c(1, 3, 2))]?
c(1, 6, 3)
c(4, 5, 2)
c(2, 3, 6)
c(2, 5, 4)
52
For m <- matrix(1:6, nrow = 2), which pair describes m[1, , drop = FALSE]?
c(1, 3)
c(3, 1)
3
c(1, 3, 1)
53
Given z <- list(a = list(b = 1:3), b = 4), what does z[[c("a", "b")]] return?
list(a = list(b = 1:3))
1:3
list(b = 1:3)
4
54
With default R options and x <- list(alpha = 7, beta = 9), which expression evaluates to TRUE?
identical(x[["al"]], 7) && is.null(x$al)
identical(x[["al"]], 7) && identical(x$al, 7)
is.null(x[["al"]]) && is.null(x$al)
is.null(x[["al"]]) && identical(x$al, 7)
55
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]))?
c(TRUE, FALSE, TRUE)
c(FALSE, FALSE, TRUE)
c(TRUE, FALSE, FALSE)
c(TRUE, TRUE, TRUE)
56
Under the default check.names = TRUE, what is names(data.frame(x = 1:2, x = 3:4))?
c("x", "x_1")
c("x.1", "x.2")
c("x", "x.1")
c("x", "x")
57
What does the following code return?
x <- 10
f <- function(y = x) {
x <- 20
y
}
f()
NULL
20
10
58
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())
c(2, 2, 1)
c(1, 1, 1)
c(1, 2, 3)
c(1, 2, 1)
59
What happens when this code is evaluated?
f <- function(x) 1
f(stop("boom"))
1 without an error.
boom immediately.
1.
60
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())
c(1, 2, 3)
c(NA, NA, NA)
c(3, 3, 3)
c(1, 1, 1)
Did this save you a night before the exam?
LPU Notes is free, and it stays free. 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.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →