R Programming Quiz: Creating Vectors
15 questions · exam conditions
0:00
Creating VectorsQuestion 1 of 15

What vector is assigned to result?

result <- c(0, seq(from = -1, by = 2.5, length.out = 4), 7:8)

c(0, -1, 1.5, 4, 7, 8)
c(0, -1, 1.5, 4, 6.5, 7, 8)
c(0, -1, 2.5, 5, 7.5, 7, 8)
c(0, -1, 1.5, 4, 6.5, 8, 7)
← Back to quizzes

R Programming Quiz

R Programming Quiz: Creating Vectors

Practice Creating Vectors in R Programming with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Creating Vectors, giving you a quick way to practice the rules, question types, and explanations that matter most for R Programming.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

What vector is assigned to result?

result <- c(0, seq(from = -1, by = 2.5, length.out = 4), 7:8)

  1. c(0, -1, 1.5, 4, 7, 8)
  2. c(0, -1, 1.5, 4, 6.5, 7, 8) (correct answer)
  3. c(0, -1, 2.5, 5, 7.5, 7, 8)
  4. c(0, -1, 1.5, 4, 6.5, 8, 7)
Explanation: When you see c(...) containing multiple expressions in R, your job is to evaluate each piece separately, then concatenate them all into one flat vector. Here, there are three pieces: the scalar 0, a seq() call, and 7:8. The seq(from = -1, by = 2.5, length.out = 4) generates exactly 4 values starting at -1, each stepping up by 2.5: -1, 1.5, 4, 6.5. The 7:8 shorthand produces the integer sequence 7, 8. Concatenating everything gives c(0, -1, 1.5, 4, 6.5, 7, 8), which is B. Choice A is missing the value 6.5 — a student who truncates seq() to only 3 elements instead of 4 (perhaps misreading length.out = 4 as producing 3 intervals rather than 4 values) would land here. Choice C uses the wrong step logic — instead of adding 2.5 each time to the previous value, it appears to multiply or misapply by, generating -1, 2.5, 5, 7.5. That treats by as an absolute position rather than an increment. Choice D has the correct values but swaps 7 and 8 at the end, reversing 7:8 — remember that 7:8 always goes in ascending order since the start is less than the end. As a study tip, always mentally parse c() from the inside out: evaluate each argument fully before concatenating. Pay special attention to seq()'s three flavors — using length.out controls the count of output values, not the number of gaps between them.

Question 2

A programmer needs to create exactly c(7, 5, 3, 1, -1). Which call does this?

  1. seq(from = 7, to = -1, by = -2) (correct answer)
  2. seq(from = 7, to = -1, by = 2)
  3. seq(from = -1, to = 7, by = -2)
  4. seq(from = -1, to = 7, by = 2)
Explanation: When working with seq() in R, three things fully determine the output: where you start, where you end, and the step size (the by argument). The sign of by must match the direction of travel — negative for descending, positive for ascending — and R will throw an error or produce unexpected results if they conflict. To generate c(7, 5, 3, 1, -1), you need a sequence that starts at 7, moves downward in steps of 2, and stops at -1. That means from = 7, to = -1, and by = -2. Option Aseq(from = 7, to = -1, by = -2) — matches all three requirements exactly, producing 7 → 5 → 3 → 1 → -1. ✓ Option B uses from = 7, to = -1 but sets by = 2 (positive). Since you're traveling downward but stepping upward, R cannot reach -1 from 7 with a positive increment — this produces an error. Option C reverses both the start and the by direction: starting at -1 with by = -2 would move you further away from 7, not toward it, again causing an error. Option D gets the direction right (by = 2, ascending), but starts at -1 and ends at 7, producing c(-1, 1, 3, 5, 7) — the correct values in reverse order. A reliable tip: always check that the sign of by matches your direction of travel. If from > to, you need a negative by; if from < to, you need a positive by. Mismatching them is the most common seq() trap on R exams.

Question 3

Consider the following R code:

base <- c(9:7, seq(from = 0, to = 1, by = 0.5)) idx <- seq(along.with = base)

Which vector is assigned to idx?

  1. c(0, 1, 2, 3, 4, 5)
  2. c(1, 2, 3, 4, 5, 6) (correct answer)
  3. c(9, 8, 7, 0, 0.5, 1)
  4. c(1, 2, 3)
Explanation: When working with seq() in R, it helps to first decode what each piece of code actually produces before jumping to conclusions about the result. Start by unpacking base. The expression c(9:7, seq(from = 0, to = 1, by = 0.5)) combines two things: 9:7 produces the descending sequence c(9, 8, 7), and seq(from = 0, to = 1, by = 0.5) produces c(0, 0.5, 1). Concatenated together, base becomes a vector of 6 elements: c(9, 8, 7, 0, 0.5, 1). Now the key: seq(along.with = base). The along.with argument tells seq() to generate an integer sequence of positions — essentially 1:length(base). Since base has 6 elements, idx becomes c(1, 2, 3, 4, 5, 6), making B the correct answer. As for the wrong choices: A (c(0, 1, 2, 3, 4, 5)) is a classic off-by-one error — R indexes starting at 1, not 0. C (c(9, 8, 7, 0, 0.5, 1)) confuses idx with base itself — seq(along.with = ...) returns index positions, not the values of the reference vector. D (c(1, 2, 3)) likely results from only counting elements in 9:7 and ignoring the seq() portion of base, underestimating the total length. A useful tip: whenever you see seq(along.with = x), mentally replace it with 1:length(x) — it always returns a 1-based integer index sequence matching the length of the reference vector.

Question 4

What is returned by the following R expression?

seq(from = -0.2, to = 0.9, by = 0.4)

  1. c(0.0, 0.4, 0.8)
  2. c(-0.2, 0.2, 0.6, 0.9)
  3. c(-0.2, 0.2, 0.6, 1.0)
  4. c(-0.2, 0.2, 0.6) (correct answer)
Explanation: When working with seq() in R, the key is understanding exactly how the function generates its sequence: it starts at from, repeatedly adds by, and stops before exceeding to. It will never include a value that goes past the endpoint. Starting at -0.2 and adding 0.4 repeatedly gives you: -0.2, 0.2, 0.6, 1.0. But that last value, 1.0, exceeds to = 0.9, so R drops it. The result is c(-0.2, 0.2, 0.6) — making D the correct answer. Option A, c(0.0, 0.4, 0.8), is wrong because the sequence doesn't start at 0.0 — the from argument explicitly sets the start to -0.2, not zero. Option B, c(-0.2, 0.2, 0.6, 0.9), is a tempting trap: it correctly starts at -0.2 but incorrectly includes 0.9 as a final value. Since 0.9 is not reachable by adding exact multiples of 0.4 to -0.2, R won't append it — seq() only steps in the increments you specify. Option C, c(-0.2, 0.2, 0.6, 1.0), makes the opposite mistake: it includes 1.0, which would be the next step but exceeds to, so it gets excluded. A reliable mental model: treat seq() like a for-loop that increments by by and breaks the moment the next value would surpass to. When in doubt, sketch out the arithmetic by hand — it only takes a few seconds and eliminates all four common traps here.

Question 5

Which expression returns 1 2 3 4 5 6?

  1. 1:(5 + 1) (correct answer)
  2. 1:5 + 1
  3. c(1:5, 1)
  4. c(1:3, 5:6)
Explanation: The colon builds the sequence first: 1:(5 + 1) is 1:6, which gives 1 2 3 4 5 6. The trap is 1:5 + 1: R treats that as (1:5) + 1, so you get 2 3 4 5 6, not 1 through 6.

Question 6

What is typeof(c(1:3, 4.5))?

  1. "integer"
  2. "numeric"
  3. "double" (correct answer)
  4. "logical"
Explanation: The expression 1:3 creates an integer vector, but 4.5 is a double. When c() combines them, R coerces the integers to double, so typeof returns "double". The tempting wrong answer is "numeric" because it is a general category in R, but typeof distinguishes integer and double and never returns "numeric".

Question 7

What does seq(0, 1, length.out = 4) return?

  1. 0.00 0.25 0.50 0.75 1.00
  2. 0.25 0.50 0.75 1.00
  3. 0.00 0.25 0.50 0.75
  4. 0.00 0.33 0.67 1.00 (correct answer)
Explanation: length.out = 4 means 4 evenly spaced values from 0 to 1 inclusive, so there are 3 gaps. Each gap is 1/3, giving 0, 0.33, 0.67, 1. The tempting wrong answer is 0.00 0.25 0.50 0.75, which treats length.out as if it were 4 steps instead of 4 values and drops the endpoint.

Question 8

What does c(1:2, seq(5, 9, by = 2)) return?

  1. 1 2 5 7
  2. 1 2 5 7 9 (correct answer)
  3. 1 2 5 7 9 11
  4. 1 2 5 6 7 8 9
Explanation: The first part, 1:2, gives 1 and 2. The second part, seq(5, 9, by = 2), gives 5, 7, and 9 because it steps by 2 from 5 through 9. Combining them produces 1 2 5 7 9. A tempting wrong result is 1 2 5 7, which drops the 9 even though the sequence includes the endpoint.

Question 9

What does seq(4, 1, by = -1.5) return?

  1. 4.0 2.5
  2. 4.0 2.5 1.0 (correct answer)
  3. 4.0 2.5 1.0 -0.5
  4. 4.0 3.0 2.0 1.0
Explanation: seq(4, 1, by = -1.5) steps downward by 1.5: 4, then 2.5, then 1.0. The next value would be -0.5, which is less than the stopping value 1, so R stops at 1.0. The tempting wrong answer adds -0.5, but a sequence with by does not go past the endpoint.

Question 10

Consider the following R code:

v <- c(1:3, seq(from = 3, to = 5, by = 1), 5:4)

Which vector is assigned to v?

  1. c(1, 2, 3, 3, 4, 5, 5, 4) (correct answer)
  2. c(1, 2, 3, 4, 5, 4)
  3. c(1, 2, 3, 3, 4, 4, 5, 5)
  4. c(1, 2, 3, 4, 5, 5, 4, 3)
Explanation: When you see R code that combines multiple vector-generating expressions inside c(), your job is to evaluate each piece independently and then concatenate them in order — no deduplication, no sorting, just sequential joining. Here, v <- c(1:3, seq(from = 3, to = 5, by = 1), 5:4) has three parts. First, 1:3 produces 1, 2, 3. Second, seq(from = 3, to = 5, by = 1) steps from 3 to 5 in increments of 1, giving 3, 4, 5. Third, 5:4 counts down from 5 to 4, giving 5, 4. Concatenating all three in order yields 1, 2, 3, 3, 4, 5, 5, 4 — which is answer A. Answer B, c(1, 2, 3, 4, 5, 4), is what you'd get if you mistakenly removed the duplicate 3 and the extra 5, as if R automatically merged overlapping values — it doesn't. Answer C, c(1, 2, 3, 3, 4, 4, 5, 5), reflects a misconception that 5:4 counts up and produces 4, 5 instead of down as 5, 4. Answer D, c(1, 2, 3, 4, 5, 5, 4, 3), suggests seq(from = 3, to = 5, by = 1) starts at 4 rather than 3, losing that crucial repeated 3. A good study habit here: always watch the direction of sequences. In R, a:b counts down when a > b, and seq() respects its from value exactly — it never skips the starting point.

Question 11

Which vector is created by the expression 5.5:1.2?

  1. c(5.5, 4.5, 3.5, 2.5)
  2. c(5.5, 4.5, 3.5, 2.5, 1.5, 1.2)
  3. c(5, 4, 3, 2)
  4. c(5.5, 4.5, 3.5, 2.5, 1.5) (correct answer)
Explanation: When working with R's colon operator (:), the key rule to remember is that it generates a sequence starting at the first value, stepping by 1 (or -1 for descending), and stops at or before the endpoint — it never overshoots it. For 5.5:1.2, R starts at 5.5 and counts down by 1 each step: 5.5 → 4.5 → 3.5 → 2.5 → 1.5. The next step would be 0.5, which overshoots the endpoint of 1.2, so R stops at 1.5. That gives you c(5.5, 4.5, 3.5, 2.5, 1.5) — confirming D is correct. Choice A stops at 2.5, omitting 1.5, which is still within the valid range above 1.2. Choice B appends 1.2 itself to the sequence, but the : operator only includes the endpoint if it's reached exactly by a whole-number step — 1.2 is never landed on, so it's excluded. Choice C suggests R rounds the values to integers, which it does not; the : operator preserves the starting value's decimal and steps by exactly 1. A useful strategy: to find the sequence generated by a:b, ask yourself how many whole-number steps fit between a and b. For 5.5 down to 1.2, the distance is 4.3, so you get floor(4.3) + 1 = 5 elements. Watch for traps involving non-integer endpoints — R never rounds or includes a value it can't reach with integer steps.

Question 12

Consider this assignment:

v <- c(FALSE, 2L, 3.5, "4")

Which result best describes v?

  1. c(FALSE, 2, 3.5, 4)
  2. c("0", "2", "3.5", "4")
  3. c("FALSE", "2", "3.5", "4") (correct answer)
  4. list(FALSE, 2L, 3.5, "4")
Explanation: Whenever you see a vector created with mixed data types in R, your first instinct should be type coercion — R cannot store multiple types in a single atomic vector, so it automatically converts everything to the most flexible type present. R follows a strict coercion hierarchy: logicalintegerdoublecharacter. Because a character ("4") appears in c(FALSE, 2L, 3.5, "4"), all elements get converted to character strings. FALSE becomes "FALSE", 2L becomes "2", and 3.5 becomes "3.5", giving you c("FALSE", "2", "3.5", "4") — which is C, the correct answer. A is wrong because it reflects numeric coercion, as if the character "4" didn't exist. This would be the result if you had c(FALSE, 2L, 3.5, 4) — logicals and integers promote to double, but no character forces the full jump up the hierarchy. B is a subtle trap: it converts FALSE to "0" instead of "FALSE". That numeric-then-character logic is wrong — R coerces the logical directly to character, preserving "FALSE" rather than first converting to 0. D is wrong because using c() with mixed types does not produce a list; you'd need list() explicitly. A list is the R structure that can hold multiple types without coercion. As a study tip, memorize the coercion chain: logical → integer → double → character. The "winning" type is always the one highest on that chain, and it governs the entire vector.

Question 13

What is assigned to x by the following code?

x <- c(1, NULL, c(2, 3:4), NULL)

  1. c(1, 0, 2, 3, 4, 0)
  2. c(1, NA, 2, 3, 4, NA)
  3. c(1, 2, 3, 4) (correct answer)
  4. c(1, 2, 3, 4, NULL)
Explanation: When working with vectors in R, it's essential to understand how c() handles special values — particularly NULL. Unlike NA, which represents a missing value and occupies a position in a vector, NULL represents the absence of an object entirely. When you pass NULL inside c(), R simply ignores it, as if it were never there. Walking through the code: c(1, NULL, c(2, 3:4), NULL) first evaluates the inner c(2, 3:4), which produces c(2, 3, 4). Then the outer c() combines 1, nothing (first NULL), 2, 3, 4, and nothing (second NULL). The result is cleanly c(1, 2, 3, 4), making C the correct answer. The wrong answers each reflect a specific misconception. A treats NULL like a zero, as if it contributes a 0 to the vector — this is not how R works. B treats NULL like NA, as if it inserts a missing-value placeholder — but NA and NULL are fundamentally different things in R. D imagines that NULL might be appended as its own element at the end, but you cannot store a NULL as an element inside an atomic vector; it disappears entirely. A useful study tip: always remember the mantra "NA is a placeholder, NULL is nothing." When you see NULL inside c(), mentally cross it out. This distinction between NULL and NA is a classic R exam trap, so expect it to appear in questions about vector construction and function return values.

Question 14

Which vector is returned by this call?

seq(from = -3, to = 9, length.out = 5)

  1. c(-3, 0, 3, 6, 9) (correct answer)
  2. c(-3, 1, 5, 9, 13)
  3. c(-3, 0, 3, 6)
  4. c(-3, -0.6, 1.8, 4.2, 6.6)
Explanation: When working with seq() in R, the length.out argument tells R how many evenly spaced values to generate between from and to — it controls the count, not the step size. Your job is to find the step size that divides the interval into exactly that many points. Here, the interval runs from −3 to 9, a total span of 12. With length.out = 5, R needs 5 values, which means 4 equal gaps between them. The step size is: 9(3)51=124=3\frac{9 - (-3)}{5 - 1} = \frac{12}{4} = 3 Starting at −3 and adding 3 each time gives: −3, 0, 3, 6, 9 — exactly answer A. Now let's see where the other choices go wrong. B uses a step of 4, which would be correct if length.out = 4 (three gaps over 12), but here we need four gaps, not three. C only contains four values instead of five, suggesting someone calculated the step correctly but forgot that length.out specifies the number of elements, not the number of gaps — an easy slip. D appears to misapply a formula entirely, producing decimal values that don't correspond to any standard interpretation of these arguments. A reliable strategy: when you see length.out, use the formula step=tofromlength.out1\text{step} = \frac{\text{to} - \text{from}}{\text{length.out} - 1} and count your output values to confirm you have exactly length.out of them. The "minus one" in the denominator is the detail most students miss.

Question 15

What vector is produced by the following R expression?

2 + 1:4 * 2

  1. c(4, 6, 8, 10) (correct answer)
  2. c(6, 8)
  3. c(3, 4, 5, 6, 7, 8, 9, 10)
  4. c(6, 8, 10, 12)
Explanation: When you see an R expression mixing arithmetic operators with the : sequence operator, operator precedence is everything. R evaluates operators in a specific order: : binds most tightly, then *, then +. Think of it like PEMDAS, but for R's operator hierarchy. So in 2 + 1:4 * 2, R first evaluates 1:4, producing the vector c(1, 2, 3, 4). Next, it applies * 2 to each element, giving c(2, 4, 6, 8). Finally, it adds 2 to each element via vectorization, yielding c(4, 6, 8, 10) — confirming that A is correct. The wrong answers each represent a different precedence mistake. D, c(6, 8, 10, 12), comes from evaluating left-to-right as (2 + 1):4 * 2, making the sequence 3:4 = c(3, 4) and then doubling — but that would actually give c(6, 8), not D. B, c(6, 8), is exactly that left-to-right misreading: (2 + 1):4 creates c(3, 4), then multiplying by 2 gives c(6, 8) — a tempting trap if you ignore that : outranks +. C, c(3, 4, 5, 6, 7, 8, 9, 10), suggests someone evaluated (2 + 1):(4 * 2) = 3:8, misapplying precedence entirely. A quick tip: in R, when in doubt, use ?Syntax in the console to pull up the full operator precedence table. On exams, always mentally resolve : first, then * and /, then + and -.