Quiz: Semester Recap

Week 14 · Advanced Statistical Programming using R

Part 1: Programming Foundations

Q1

Which two commands move into a folder and list its contents?

  1. dir data-raw/list()
  2. cd data-raw/ls
  3. pwd data-raw/open
  4. mv data-raw/read_csv()

Answer: 2) cd changes the working directory, ls lists its contents. pwd prints the current path if you need to check where you are.

Q2

Which name is the formal parameter declared by max_minus_min()?

max_minus_min <- function(x) {
  max(x, na.rm = TRUE) - min(x, na.rm = TRUE)
}
  1. na.rm = TRUE
  2. function(x)
  3. max_minus_min
  4. x

Answer: 4) x is the formal parameter declared in function(x). na.rm = TRUE is an argument passed internally to max() and min(), not a parameter of max_minus_min() itself.

Q3

The lecture extended the classic DRY rule (“Don’t Repeat Yourself”) to DRRY. What does the extra R stand for?

  1. “Really”, write a function whenever code feels important
  2. “Re-read”, re-reading code 3 times means it’s time to write or improve a function
  3. “Rewrite”, rewrite every function at least 3 times before committing
  4. “Reactive”, only write functions when errors occur

Answer: 2) DRY targets repetition in the text of your code. DRRY targets cognitive load: the effort to understand your code later on.

Q4

What is a silent error?

  1. An error that prints a very long message
  2. A function that crashes immediately
  3. A function that returns the wrong output without producing an error
  4. A common warning message from ggplot2

Answer: 3) The code runs and returns an output that looks fine. Silent errors are the hardest to catch, so sense-check every result.

Q5

Your function errors deep inside nested calls. Which command shows the sequence of function calls that led to the error?

  1. browser()
  2. traceback() or rlang::last_trace()
  3. debugonce()
  4. stopifnot()

Answer: 2) traceback() prints the call stack after an error. browser() pauses execution for interactive inspection, debugonce() triggers browser for one call, and stopifnot() validates inputs.

Q6

You edited analysis.R and want to save this change in your Git history. What is the correct order of commands?

  1. git add analysis.Rgit commit -m "update"
  2. git commit -m "update"git add analysis.R
  3. git initgit commit -m "update"
  4. git statusgit log

Answer: 1) git add stages the change, git commit records it as a snapshot in the repository.

Q7

You see this in a file during a merge:

<<<<<<< HEAD
result <- mean(x, na.rm = TRUE)
=======
result <- median(x, na.rm = TRUE)
>>>>>>> feature-branch

What do you do?

  1. Retry with sudo merge
  2. Run git reset --hard to discard both versions
  3. Pick one version, delete the marker lines, then git add + git commit
  4. Blame your colleague

Answer: 3) Choose (or combine) the version you want, remove the <<<<<<<, =======, >>>>>>> markers, then mark as resolved.

Q8

What does quarto publish gh-pages do?

  1. Sends .qmd source files directly to GitHub Pages
  2. Deletes your main branch
  3. Creates a Netlify account
  4. Renders the site locally, then pushes rendered HTML to a gh-pages branch

Answer: 4) Quarto renders locally, then pushes the rendered output to gh-pages. Your .qmd sources stay on main.

Q9

Which file holds your package’s name, version, licence, and dependencies?

  1. NAMESPACE
  2. README.md
  3. DESCRIPTION
  4. .Rbuildignore

Answer: 3) DESCRIPTION stores package metadata: name, version, licence, and dependencies. NAMESPACE controls exports and imports and is normally generated from roxygen2 comments.

Q10

Why should you never write library(ggplot2) inside a package function?

  1. R does not allow library() calls inside functions
  2. library() is slower than require()
  3. Packages cannot use ggplot2
  4. It attaches ggplot2 to the user’s session and changes global state

Answer: 4) Declare ggplot2 under Imports in DESCRIPTION (via usethis::use_package("ggplot2")) and call ggplot2::aes() with namespace qualification.

Part 2: Working with Real World Data

Q11

What does Initial Data Analysis (IDA) refer to?

  1. The first time you open a dataset in R
  2. Systematic checks of data quality before formal analysis
  3. Running a regression model on a fresh dataset
  4. Creating publication-ready plots

Answer: 2) IDA is the preparation phase. It checks types, ranges, and missing values so the data matches your assumptions before any inference.

Q12

A monthly dataset contains rows for January, February, and April, but no row for March. What kind of missingness is this?

  1. Explicit missingness
  2. Missing completely at random (MCAR)
  3. Implicit missingness
  4. Censored missingness

Answer: 3) The expected March row is absent entirely, so the missingness is implicit. Use tidyr::complete() to add the missing row and make the gap explicit as NA.

Q13

You clone an renv-managed repo. What is the correct setup sequence?

  1. clone → renv::init()renv::snapshot()
  2. clone → renv::update()renv::commit()
  3. fork → renv::install()renv::push()
  4. clone → open project → renv::restore()

Answer: 4) renv::restore() installs every package at the version recorded in renv.lock. Opening the project triggers .Rprofile to activate the local library.

Q14

What does FAIR stand for in the context of open data?

  1. Fast, Accurate, Inclusive, Reliable
  2. Findable, Accessible, Interoperable, Reusable
  3. Free, Anonymous, Indexed, Reviewed
  4. Filtered, Annotated, Inspected, Reproducible

Answer: 2) Findable (identifiers), Accessible (open protocols, metadata stays even if data doesn’t), Interoperable (standard formats), Reusable (clear licensing). Introduced by Wilkinson et al. (2016).

Q15

What are the three goals of analysis introduced in W10?

  1. Reading, Writing, Plotting
  2. Frequentist, Bayesian, Causal
  3. Descriptive, Explanatory, Predictive
  4. Sampling, Modelling, Testing

Answer: 3) Descriptive asks what is the state of things in the population, Explanatory is there a real relationship, Predictive how well can we predict new cases.

Q16

A p-value is the probability of…

  1. …seeing a result at least this extreme, assuming H₀ is true
  2. …the null hypothesis being true
  3. …the alternative hypothesis being true
  4. …the effect being large

Answer: 1) The p-value lives in the world where H₀ is true. It does not tell you the probability of H₀ itself, or the size of any effect.

Q17

Should you use a table or a plot to communicate a trend?

  1. Always a table, since tables hold more information
  2. A plot, since patterns and trends are easier to see visually
  3. Whichever is shorter
  4. Both are equivalent

Answer: 2) Tables are best for exact values or small comparisons. Plots win for patterns, trends, and messages.

Q18

Which tools can restore a reproducible R environment? (multiple correct answers)

  1. renv for R package versions
  2. Docker for the full computational environment
  3. sessionInfo() in a README
  4. Git for code history

Answer: 1 & 2)
Correct: 1. renv::restore() rebuilds the R library from renv.lock.
Correct: 2. Docker rebuilds the OS, libraries, and runtimes from a Dockerfile.
False: 3. sessionInfo() documents the environment but cannot restore it.
False: 4. Git tracks code history but does not manage the R environment.

Bonus: Advanced Topics (Not Examined)

Q19

What is the fastest way to make an existing ggplot2 figure explorable in the browser?

  1. Export it as PNG and add hover in Photoshop
  2. Wrap it in ggplotly()
  3. Rewrite it in JavaScript
  4. Convert it into a gt table

Answer: 2) ggplotly(p) turns a static ggplot2 object into an interactive plotly chart. Hover, zoom, and legend toggles come along for free.

Q20

What does the {reticulate} package do?

  1. Wraps R into a Python package for Jupyter
  2. Compiles R code to Python bytecode
  3. Runs an embedded Python session inside R with automatic object conversion
  4. Provides a REST API between an R and a Python server

Answer: 3) reticulate embeds Python in your R session. R and Python objects are translated on the fly, so a data frame becomes a pandas DataFrame and back.