R Programming interview questions

25 questions from the R Programming course, each with the correct answer and an explanation. Try to answer before you open the answer.

Getting Started with R

1. For which kind of work is R especially well known?

  1. Statistical analysis and data visualisation
  2. Operating system kernels
  3. Mobile game engines
  4. Device drivers
Show answer

Answer: Statistical analysis and data visualisation

R was designed for statistics and graphics and excels at them.

Lesson: What R Is For

2. What is the difference between install.packages() and library()?

  1. There is none
  2. library downloads; install.packages loads
  3. Both must be run before every line
  4. install.packages downloads a package once; library loads it into the current session
Show answer

Answer: install.packages downloads a package once; library loads it into the current session

Installation happens once per machine; loading happens in each R session.

Lesson: Installing R, IDEs, Packages and Projects

Data Structures

4. In R, what does x[-2] return for x <- c(10, 20, 30)?

  1. 20
  2. 30
  3. c(10, 30)
  4. An error
Show answer

Answer: c(10, 30)

Negative indices exclude elements, so the second element is dropped.

Lesson: Vectors, Recycling and Indexing

5. For a list x, what is the difference between x["a"] and x[["a"]]?

  1. There is none
  2. x["a"] returns a sub-list; x[["a"]] returns the element itself
  3. x[["a"]] returns a sub-list
  4. Both throw errors
Show answer

Answer: x["a"] returns a sub-list; x[["a"]] returns the element itself

Single brackets keep the list container; double brackets extract the content.

Lesson: Lists, Factors and Matrices

6. What is a data frame in R?

  1. A list of equal-length column vectors forming a table
  2. A matrix of characters
  3. A plot type
  4. A package
Show answer

Answer: A list of equal-length column vectors forming a table

Each column is a vector, and all columns have the same length.

Lesson: Data Frames and Tibbles

Programming in R

7. Why is seq_along(x) preferred over 1:length(x) in loops?

  1. It correctly produces an empty sequence when x is empty
  2. It is faster for long vectors
  3. It sorts x
  4. It works only with lists
Show answer

Answer: It correctly produces an empty sequence when x is empty

1:length(x) gives c(1, 0) for an empty x, causing a loop to run when it should not.

Lesson: Control Flow and Vectorisation

8. What does the native pipe `x |> f(y)` mean?

  1. f(y, x)
  2. x(f, y)
  3. It assigns f to x
  4. f(x, y)
Show answer

Answer: f(x, y)

The pipe inserts the left-hand value as the first argument of the right-hand call.

Lesson: Functions, Defaults, Dots and Pipes

9. Which purrr function returns a numeric (double) vector, failing if any result is not a single number?

  1. map
  2. keep
  3. map_dbl
  4. reduce
Show answer

Answer: map_dbl

map_dbl guarantees a double vector output.

Lesson: The apply Family and purrr

Data Wrangling with the Tidyverse

10. Which function standardises messy column names such as "Order ID" into snake_case?

  1. read_csv
  2. glimpse
  3. janitor::clean_names
  4. pivot_longer
Show answer

Answer: janitor::clean_names

clean_names converts column names to consistent snake_case.

Lesson: Importing and Cleaning Data

11. What does group_by(city) |> summarise(revenue = sum(total)) produce?

  1. One row with total revenue
  2. One row per city with that city's total revenue
  3. The original rows sorted by city
  4. A plot
Show answer

Answer: One row per city with that city's total revenue

Grouping makes summarise produce one row per group.

Lesson: Transforming Data with dplyr

12. Which join returns rows from the left table that have no match in the right table?

  1. anti_join
  2. left_join
  3. inner_join
  4. full_join
Show answer

Answer: anti_join

anti_join is ideal for finding unmatched records during data checks.

Lesson: Tidy Data, Reshaping and Joins

Visualisation and Exploration

13. In ggplot2, what does aes() define?

  1. Mappings from data variables to visual properties such as x, y and colour
  2. The file format of the plot
  3. The theme only
  4. The number of bins
Show answer

Answer: Mappings from data variables to visual properties such as x, y and colour

Aesthetic mappings connect data columns to visual channels.

Lesson: The Grammar of Graphics with ggplot2

14. What does facet_wrap(~ city) do?

  1. Changes the colour palette
  2. Sorts the data
  3. Saves the plot
  4. Creates small multiples: one panel per city
Show answer

Answer: Creates small multiples: one panel per city

Faceting splits the data into panels by a variable.

Lesson: Facets, Scales, Labels and Themes

15. What is the main goal of exploratory data analysis?

  1. To produce the final published report immediately
  2. To train deep learning models
  3. To understand structure, quality, distributions and relationships in the data before modelling
  4. To delete missing data
Show answer

Answer: To understand structure, quality, distributions and relationships in the data before modelling

EDA builds understanding and uncovers data problems early.

Lesson: Exploratory Data Analysis

Statistics and Modelling

16. Which R function gives the cumulative probability P(X <= x) for a normal distribution?

  1. dnorm
  2. qnorm
  3. pnorm
  4. rnorm
Show answer

Answer: pnorm

p-functions return cumulative probabilities; d is density, q is quantile, r is random draws.

Lesson: Descriptive Statistics and Distributions

17. What does a p-value of 0.03 mean?

  1. There is a 3% chance the null hypothesis is true
  2. If the null hypothesis were true, results at least this extreme would occur about 3% of the time
  3. The effect is large
  4. The result is certainly important
Show answer

Answer: If the null hypothesis were true, results at least this extreme would occur about 3% of the time

p-values are computed assuming the null is true; they are not the probability of the null.

Lesson: Hypothesis Tests and Confidence Intervals

18. Which call fits a logistic regression for a binary outcome in R?

  1. glm(y ~ x, family = binomial)
  2. lm(y ~ x)
  3. t.test(y, x)
  4. cor(y, x)
Show answer

Answer: glm(y ~ x, family = binomial)

glm with the binomial family fits logistic regression.

Lesson: Linear and Logistic Regression

Strings, Dates, Objects and Performance

19. Which lubridate function parses "03/10/2026" written as day/month/year?

  1. dmy
  2. ymd
  3. mdy
  4. ydm
Show answer

Answer: dmy

The function name follows the order of components in the input.

Lesson: Strings with stringr and Dates with lubridate

20. How does S3 method dispatch decide which function to call for print(x)?

  1. It always calls print.default
  2. It uses the first argument's length
  3. S3 does not support dispatch
  4. It looks for print.<class of x>, falling back to print.default
Show answer

Answer: It looks for print.<class of x>, falling back to print.default

S3 generics dispatch on the class attribute to methods named generic.class.

Lesson: Functions as Objects, Environments and S3/R6 Classes

21. Which approach lets dplyr code query a folder of Parquet files larger than memory?

  1. read.csv on each file
  2. Converting everything to a matrix
  3. arrow::open_dataset with dplyr verbs and collect() at the end
  4. Using for loops
Show answer

Answer: arrow::open_dataset with dplyr verbs and collect() at the end

arrow datasets push filtering and aggregation down to efficient readers before collecting results.

Lesson: Performance and Big Data in R

Reports, Apps, Workflow and Revision

22. What is the main benefit of a Quarto or R Markdown report?

  1. It makes R run faster
  2. It replaces databases
  3. Rerunning it regenerates every result from the data, avoiding copy-paste errors
  4. It only produces PDFs
Show answer

Answer: Rerunning it regenerates every result from the data, avoiding copy-paste errors

Reproducible documents tie narrative and results directly to code and data.

Lesson: Reproducible Reports with Quarto and R Markdown

23. What does a reactive() expression provide in Shiny?

  1. A static value computed once at start-up
  2. A cached result that recomputes only when the inputs it reads change, and can be shared by several outputs
  3. A way to write JavaScript
  4. A database connection
Show answer

Answer: A cached result that recomputes only when the inputs it reads change, and can be shared by several outputs

reactive() expressions cache derived data and update when their dependencies change.

Lesson: Interactive Apps with Shiny

25. Which dplyr verb adds or modifies columns?

  1. filter
  2. arrange
  3. summarise
  4. mutate
Show answer

Answer: mutate

mutate creates or changes columns; filter keeps rows; summarise reduces groups.

Lesson: Revision and Interview Questions

← Back to the course