Bird
Raised Fist0
R Programmingprogramming~20 mins

For loop in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple for loop with sequence
What is the output of this R code?
result <- c()
for (i in 1:4) {
  result <- c(result, i * 2)
}
print(result)
R Programming
result <- c()
for (i in 1:4) {
  result <- c(result, i * 2)
}
print(result)
A[1] 1 2 3 4
B[1] 2 3 4 5
C[1] 4 8 12 16
D[1] 2 4 6 8
Attempts:
2 left
💡 Hint
Remember the loop multiplies each number by 2 before adding to the result.
❓ Predict Output
intermediate
2:00remaining
Output of nested for loops with sum
What is the output of this R code?
total <- 0
for (i in 1:2) {
  for (j in 1:3) {
    total <- total + i + j
  }
}
print(total)
R Programming
total <- 0
for (i in 1:2) {
  for (j in 1:3) {
    total <- total + i + j
  }
}
print(total)
A[1] 18
B[1] 21
C[1] 12
D[1] 15
Attempts:
2 left
💡 Hint
Add all pairs of i and j from the loops.
🔧 Debug
advanced
2:00remaining
Identify the error in this for loop
What error does this R code produce?
for i in 1:5 {
  print(i)
}
R Programming
for i in 1:5 {
  print(i)
}
ASyntaxError: unexpected symbol 'i'
BError: object 'i' not found
CError: unexpected '{' in "for i in 1:5 {"
DNo error, prints numbers 1 to 5
Attempts:
2 left
💡 Hint
Check the syntax of the for loop header in R.
❓ Predict Output
advanced
2:00remaining
Output of for loop with conditional break
What is the output of this R code?
result <- c()
for (i in 1:10) {
  if (i > 4) {
    break
  }
  result <- c(result, i)
}
print(result)
R Programming
result <- c()
for (i in 1:10) {
  if (i > 4) {
    break
  }
  result <- c(result, i)
}
print(result)
A[1] 1 2 3 4
B[1] 1 2 3 4 5
C[1] 1 2 3
D[1] 5 6 7 8 9 10
Attempts:
2 left
💡 Hint
The loop stops when i is greater than 4.
🧠 Conceptual
expert
2:00remaining
Number of iterations in a for loop with sequence and step
How many times will this R for loop run?
count <- 0
for (i in seq(2, 10, by=3)) {
  count <- count + 1
}
print(count)
R Programming
count <- 0
for (i in seq(2, 10, by=3)) {
  count <- count + 1
}
print(count)
A3
B2
C5
D4
Attempts:
2 left
💡 Hint
Check the sequence generated by seq(2, 10, by=3).

Practice

(1/5)
1. What does a for loop do in R programming?
easy
A. Stops the program immediately
B. Creates a new variable
C. Defines a new function
D. Repeats a block of code for each item in a sequence

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop is used to repeat actions multiple times, once for each item in a sequence.
  2. Step 2: Match the description to the options

    Repeats a block of code for each item in a sequence correctly describes this behavior, while others describe unrelated actions.
  3. Final Answer:

    Repeats a block of code for each item in a sequence -> Option D
  4. Quick Check:

    For loop = repeat for each item [OK]
Hint: For loops repeat code for each item in a list or sequence [OK]
Common Mistakes:
  • Confusing for loop with function definition
  • Thinking for loop stops the program
  • Believing for loop creates variables only
2. Which of the following is the correct syntax for a for loop in R?
easy
A. for (i; i < 5; i++) { print(i) }
B. for (i in 1:5) { print(i) }
C. for i = 1 to 5 { print(i) }
D. loop (i in 1 to 5) { print(i) }

Solution

  1. Step 1: Recall R for loop syntax

    In R, the for loop uses the format: for (variable in sequence) { code }
  2. Step 2: Compare options to correct syntax

    for (i in 1:5) { print(i) } matches the correct syntax. Options A, B, and D use syntax from other languages or incorrect forms.
  3. Final Answer:

    for (i in 1:5) { print(i) } -> Option B
  4. Quick Check:

    R for loop = for (var in sequence) [OK]
Hint: Use 'for (var in sequence)' syntax in R [OK]
Common Mistakes:
  • Using 'for i = 1 to 5' like other languages
  • Using C-style for loop syntax
  • Writing 'loop' instead of 'for'
3. What will be the output of this R code?
for (i in 1:3) {
  print(i * 2)
}
medium
A. [1] 2 [1] 4 [1] 6
B. [1] 1 [1] 2 [1] 3
C. [1] 3 [1] 6 [1] 9
D. Error: object 'i' not found

Solution

  1. Step 1: Understand the loop iterations

    The loop runs with i = 1, 2, 3.
  2. Step 2: Calculate print output for each i

    For i=1: print(2), i=2: print(4), i=3: print(6).
  3. Final Answer:

    [1] 2 [1] 4 [1] 6 -> Option A
  4. Quick Check:

    Multiply i by 2 each loop [OK]
Hint: Multiply loop variable by 2 inside print [OK]
Common Mistakes:
  • Printing i instead of i*2
  • Confusing output format
  • Expecting a single combined print
4. Identify the error in this R for loop:
for i in 1:4 {
  print(i)
}
medium
A. Loop variable should be a string
B. Using curly braces instead of parentheses
C. Missing parentheses around the loop variable and sequence
D. Incorrect print function usage

Solution

  1. Step 1: Check for correct for loop syntax in R

    R requires parentheses around the loop variable and sequence: for (i in 1:4) { ... }
  2. Step 2: Identify the error in given code

    The code misses parentheses around 'i in 1:4', causing a syntax error.
  3. Final Answer:

    Missing parentheses around the loop variable and sequence -> Option C
  4. Quick Check:

    For loop needs parentheses ( ) [OK]
Hint: Always put parentheses after 'for' in R [OK]
Common Mistakes:
  • Omitting parentheses around 'i in 1:4'
  • Confusing braces and parentheses
  • Thinking print syntax is wrong
5. You want to create a vector of squares of numbers 1 to 5 using a for loop in R. Which code correctly does this?
hard
A. squares <- c() for (i in 1:5) { squares[i] <- i^2 } squares
B. for (i in 1:5) { squares <- i^2 } squares
C. squares <- 1:5 for (i in squares) { squares[i] = i * 2 } squares
D. squares <- c() for i in 1:5 { squares[i] <- i^2 } squares

Solution

  1. Step 1: Initialize an empty vector to store results

    squares <- c() for (i in 1:5) { squares[i] <- i^2 } squares initializes 'squares' as an empty vector to store squares.
  2. Step 2: Use a for loop to assign squares to each position

    Inside the loop, squares[i] <- i^2 correctly stores the square of i at position i.
  3. Step 3: Check other options for errors

    for (i in 1:5) { squares <- i^2 } squares overwrites 'squares' each time, losing previous values. squares <- 1:5 for (i in squares) { squares[i] = i * 2 } squares multiplies by 2, not square. squares <- c() for i in 1:5 { squares[i] <- i^2 } squares misses parentheses in for loop syntax.
  4. Final Answer:

    squares <- c() for (i in 1:5) { squares[i] <- i^2 } squares -> Option A
  5. Quick Check:

    Initialize vector, assign squares in loop [OK]
Hint: Initialize vector before loop, assign squares by index [OK]
Common Mistakes:
  • Not initializing vector before loop
  • Overwriting vector inside loop
  • Wrong loop syntax without parentheses
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard