J Exam

Last updated: 2024-03-20 11:51:57

J.1 Course grade

  • Exercises (50%)
  • Exam (50%)
    • The exam is composed of 20 multiple-choice questions. There is exactly one right answer to each question.
    • Exam duration is 3 hours.
    • Permitted exam equipment: a calculator.
    • The exam takes place in a regular class, with pen and paper (not in front of a computer).
    • Each question contains a short code section. The code section is not identical to the ones that appeared in the course materials, but they combine functions and methods which were learned. You need to choose the right code output out of 4 options.

J.2 Material for exam

The material for the exam is Chapters 110, except for:

  • Dates (Date)
  • Chapter 9

Note that two functions named st_mosaic and st_warp appear in the data preparation steps in Chapter 10, however these functions are covered in Chapter 9 and therefore will not appear in the exam.

J.3 Examples of exam questions

J.3.1 Question 1

x = c(3, 2, 4)
x * c(NA, diff(x))

(A) [1] NA NA NA

(B) [1] 3 2 4

(C) [1] NA -2 8

(D) [1] NA -1 2

J.3.2 Question 2

f = function(x = 0, y = 1) {
  if(x != y) return(c(x, y)) else return(x)
}
x = 1
y = 2
f(x, x) * f(y)

(A) [1] 1 2

(B) [1] 2 1

(C) [1] 1 1

(D) [1] 2

J.3.3 Question 3

x = rep(c(1, 2), 2)
x[x ^ 2]

(A) [1] 1 2 1 2

(B) [1] 1 2 3 4

(C) [1] 1 2

(D) [1] 1 4 1 4

J.3.4 Question 4

x = c(3, 8, 5)
y = c(1, 4, 9)
any(x < 2) | (all(x != y) & TRUE)

(A) [1] FALSE

(B) [1] TRUE TRUE TRUE

(C) [1] TRUE FALSE

(D) [1] TRUE

J.3.5 Question 5

m = matrix(12:1, ncol = 3)
f = function(x) sum(x) > 20
apply(m, 1, f)

(A) [1] FALSE FALSE FALSE FALSE

(B) [1] TRUE FALSE FALSE FALSE

(C) [1] TRUE TRUE FALSE FALSE

(D) [1] TRUE TRUE TRUE FALSE

J.3.6 Data for questions 6-9

For questions 6-9 we define a stars raster named r, and an sf point layer named pnt:

library(stars)
v = c(10,7,4,1,8,5,9,3,9,8,4,2,5,7,1,9,6,3)
a = array(v, dim = c(3, 3, 2))
r = st_as_stars(a)
r = st_set_dimensions(r, names = c('x', 'y', 'layer'))
r = st_set_dimensions(r, 'y', delta = -1, offset = 3)
r = st_set_dimensions(r, 'y', point = FALSE)
r = st_set_dimensions(r, 'layer', values = c('one', 'two'))
names(r) = 'var'
r
## stars object with 3 dimensions and 1 attribute
## attribute(s):
##      Min. 1st Qu. Median     Mean 3rd Qu. Max.
## var     1    3.25    5.5 5.611111       8   10
## dimension(s):
##       from to offset delta point   values x/y
## x        1  3      0     1 FALSE     NULL [x]
## y        1  3      3    -1 FALSE     NULL [y]
## layer    1  2     NA    NA FALSE one, two
pnt = data.frame(x = c(1.6, 0.2), y = c(1.7, 2.6), z = c(80, 90))
pnt = st_as_sf(pnt, coords = c('x', 'y'))

You can also learn about the structure of r from the following plot.

plot(r, text_values = TRUE, axes = TRUE, col = grey.colors(9)[4:10], key.pos = 4)

J.3.7 Question 6

dim(st_join(pnt[pnt$z == 80, ], st_as_sf(r[,,,2,drop=TRUE])))

(A) [1] 0 3

(B) [1] 1 3

(C) [1] 0 2

(D) [1] 1 2

J.3.8 Question 7

mean(st_apply(r, 1:2, function(x) which.max(x))[[1]])

(A) [1] 1.222222

(B) [1] 1.444444

(C) [1] 1.888888

(D) [1] 1

J.3.9 Question 8

b = st_buffer(pnt[1, ], 10)
aggregate(r[,,,1,drop=TRUE], b, sum)[[1]]

(A) [1] 1

(B) [1] 56

(C) [1] 8

(D) [1] 101

J.3.10 Question 9

length(dim(st_apply(r, 3, mean)))

(A) [1] NA

(B) [1] 1

(C) [1] 2

(D) [1] 3

J.3.11 Data for questions 10-11

For questions 10-11 we will define two data.frame objects named animals and types:

animals = data.frame(
  name = c('cat', 'cat', 'sparrow')
)
types = data.frame(
  name = c('cat', 'sparrow', 'turtle'),
  type = c('mammal', 'bird', 'reptile')
)

J.3.12 Question 10

animals2 = merge(animals, types, by = 'name', all.x = TRUE)
sum(animals2$type == 'mammal')

(A) [1] 0

(B) [1] 1

(C) [1] 2

(D) [1] 3

J.3.13 Question 11

paste(types$name, types$name[1])[2]

(A) [1] "cat cat" "sparrow cat" "turtle cat"

(B) [1] "sparrow sparrow"

(C) [1] "sparrow"

(D) [1] "sparrow cat"

J.3.14 Question 12

sum(c(min(1:5), max(1:5)))-1

(A) [1] 1

(B) [1] 2

(C) [1] 5

(D) [1] 6

J.3.15 Question 13

any(seq(-8, 12, by = 4) == 4)

(A) [1] FALSE

(B) [1] TRUE

(C) [1] TRUE TRUE TRUE

(D) [1] FALSE FALSE FALSE

J.3.16 Data for questions 14-15

For questions 14-15 we will define a data.frame object named cars. (All columns are numeric.)

cars = mtcars[1:10, 1:4]
cars
##                    mpg cyl  disp  hp
## Mazda RX4         21.0   6 160.0 110
## Mazda RX4 Wag     21.0   6 160.0 110
## Datsun 710        22.8   4 108.0  93
## Hornet 4 Drive    21.4   6 258.0 110
## Hornet Sportabout 18.7   8 360.0 175
## Valiant           18.1   6 225.0 105
## Duster 360        14.3   8 360.0 245
## Merc 240D         24.4   4 146.7  62
## Merc 230          22.8   4 140.8  95
## Merc 280          19.2   6 167.6 123

J.3.17 Question 14

x = rep(NA, 3)
cyl = c(4, 6, 8)
for(i in 1:length(cyl)) {
  x[i] = which.min(cars[cars$cyl == cyl[i], "hp"])
}
x

(A) [1] 2 4 1

(B) [1] 8 6 5

(C) [1] 2 4 6

(D) [1] 62 105 175

J.3.18 Question 15

range(cars$cyl[cars$mpg < 20 | cars$mpg > 23])

(A) [1] 2 4 8

(B) [1] 8

(C) [1] 4

(D) [1] 4 8

J.3.19 Data for questions 16-17

For questions 16-17 we will define a matrix named m:

m = matrix(28:36, ncol = 3)
m
##      [,1] [,2] [,3]
## [1,]   28   31   34
## [2,]   29   32   35
## [3,]   30   33   36

J.3.20 Question 16

f = function(x) diff(range(x))
apply(m, 2, f)

(A) [1] 6 6 6

(B) [1] 1 1 1

(C) [1] 1 2 3

(D) [1] 2 2 2

J.3.21 Question 17

t(m)[1,2]

(A) [1] 31

(B) [1] 29

(C) [1] 28

(D) [1] 33