J Exam

Last updated: 2022-02-21 17:14:28

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 gives 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

What will be the output of the following code section?

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

() [1] NA NA NA

() [1] 3 2 4

() [1] NA -2 8

() [1] NA -1 2

J.3.2 Question 2

What will be the output of the following code section?

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)

() [1] 1 2

() [1] 2 1

() [1] 1 1

() [1] 2

J.3.3 Question 3

What will be the output of the following code section?

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

() [1] 1 2 1 2

() [1] 1 2 3 4

() [1] 1 2

() [1] 1 4 1 4

J.3.4 Question 4

What will be the output of the following code section?

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

() [1] FALSE

() [1] TRUE TRUE TRUE

() [1] TRUE FALSE

() [1] TRUE

J.3.5 Question 5

What will be the output of the following code section?

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

() [1] FALSE FALSE FALSE FALSE

() [1] TRUE FALSE FALSE FALSE

() [1] TRUE TRUE FALSE FALSE

() [1] TRUE TRUE TRUE FALSE

J.3.6 Data for questions 6-8

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

library(sf)
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 refsys point   values x/y
## x        1  3      0     1     NA FALSE     NULL [x]
## y        1  3      3    -1     NA FALSE     NULL [y]
## layer    1  2     NA    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

What will be the output of the following code section?

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

() [1] 0 3

() [1] 1 3

() [1] 0 2

() [1] 1 2

J.3.8 Question 7

What will be the output of the following code section?

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

() [1] 1.222222

() [1] 1.444444

() [1] 1.888888

() [1] 1

J.3.9 Question 8

What will be the output of the following code section?

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

() [1] 1

() [1] 56

() [1] 8

() [1] 101

J.3.10 Question 9

What will be the output of the following code section?

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

() [1] NA

() [1] 1

() [1] 2

() [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

What will be the output of the following code section?

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

() [1] 0

() [1] 1

() [1] 2

() [1] 3

J.3.13 Question 11

What will be the output of the following code section?

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

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

() [1] "sparrow sparrow"

() [1] "sparrow"

() [1] "sparrow cat"

J.3.14 Question 12

What will be the output of the following code section?

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

() [1] 1

() [1] 2

() [1] 5

() [1] 6

J.3.15 Question 13

What will be the output of the following code section?

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

() [1] FALSE

() [1] TRUE

() [1] TRUE TRUE TRUE

() [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

What will be the output of the following code section?

x = NULL
for(i in c(4, 6, 8)) {
  x = c(x, which.min(cars[cars$cyl == i, "hp"]))
}
x

() [1] 2 4 1

() [1] 8 6 5

() [1] 2 4 6

() [1] 62 105 175

J.3.18 Question 15

What will be the output of the following code section?

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

() [1] 2 4 8

() [1] 8

() [1] 4

() [1] 4 8

J.3.19 Data for questions 16-17

For questions 14-15 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

What will be the output of the following code section?

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

() [1] 6 6 6

() [1] 1 1 1

() [1] 1 2 3

() [1] 2 2 2

J.3.21 Question 17

What will be the output of the following code section?

t(m)[1,2]

() [1] 31

() [1] 29

() [1] 28

() [1] 33