Bird
Raised Fist0
R Programmingprogramming~20 mins

Running R code in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
R Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple R loop
What is the output of the following R code?
R Programming
result <- c()
for(i in 1:4) {
  result <- c(result, i^2)
}
print(result)
A[1] 1 2 3 4
B[1] 1 8 27 64
C[1] 2 4 6 8
D[1] 1 4 9 16
Attempts:
2 left
💡 Hint
Think about what i^2 means inside the loop.
🧠 Conceptual
intermediate
2:00remaining
Understanding R function return values
What will be the output when running this R code?
R Programming
my_func <- function(x) {
  y <- x + 2
  z <- y * 3
  z
  y
}
print(my_func(4))
A[1] 6
B[1] 18
C[1] 12
D[1] 0
Attempts:
2 left
💡 Hint
Remember that R returns the last evaluated expression in a function.
🔧 Debug
advanced
2:00remaining
Identify the error in R code
What error does this R code produce when run?
R Programming
x <- c(1, 2, 3)
print(x[4])
AError: subscript out of bounds
BNA (no error, prints NA)
CNULL
DSyntaxError
Attempts:
2 left
💡 Hint
What happens when you access an index outside the vector length in R?
❓ Predict Output
advanced
2:00remaining
Output of nested if-else in R
What is the output of this R code?
R Programming
x <- 5
if (x > 3) {
  if (x < 10) {
    print("Between 4 and 9")
  } else {
    print("10 or more")
  }
} else {
  print("3 or less")
}
A"Between 4 and 9"
B"10 or more"
CError: unexpected symbol
D"3 or less"
Attempts:
2 left
💡 Hint
Check the conditions carefully and their order.
🚀 Application
expert
2:00remaining
Calculate the sum of even numbers using R code
Which option correctly calculates the sum of even numbers from 1 to 10 in R?
Asum(c(2,4,6,8,10))
Bsum(1:10 %% 2 == 0)
Csum(seq(2, 10, by=2))
Dsum(1:10[1:10 %% 2 == 0])
Attempts:
2 left
💡 Hint
Use seq() to generate even numbers or filter with modulo operator.

Practice

(1/5)
1. What does running R code mean?
easy
A. Installing R software
B. Only writing comments in the script
C. Saving files without executing them
D. Typing commands or running script files to perform tasks

Solution

  1. Step 1: Understand the meaning of running code

    Running code means executing commands or scripts to perform actions in R.
  2. Step 2: Identify the correct description

    Typing commands or running script files matches the definition of running R code.
  3. Final Answer:

    Typing commands or running script files to perform tasks -> Option D
  4. Quick Check:

    Running code = executing commands/scripts [OK]
Hint: Running code means executing commands or scripts [OK]
Common Mistakes:
  • Confusing running code with just writing comments
  • Thinking saving files runs code
  • Mixing running code with installing software
2. Which of the following is the correct way to run an R script named script.R from the console?
easy
A. source('script.R')
B. run('script.R')
C. execute('script.R')
D. load('script.R')

Solution

  1. Step 1: Recall the function to run scripts in R

    The function source() is used to run R scripts from the console.
  2. Step 2: Match the correct syntax

    source('script.R') correctly runs the script named script.R.
  3. Final Answer:

    source('script.R') -> Option A
  4. Quick Check:

    Run script = source() [OK]
Hint: Use source() to run R scripts from console [OK]
Common Mistakes:
  • Using run() which is not an R function
  • Using execute() which does not exist in R
  • Using load() which loads data, not scripts
3. What will be the output of the following R code?
print(2 + 3 * 4)
medium
A. 14
B. 20
C. 24
D. Error

Solution

  1. Step 1: Understand operator precedence in R

    Multiplication (*) has higher precedence than addition (+), so 3 * 4 is calculated first.
  2. Step 2: Calculate the expression

    3 * 4 = 12, then 2 + 12 = 14.
  3. Final Answer:

    14 -> Option A
  4. Quick Check:

    2 + (3 * 4) = 14 [OK]
Hint: Multiply before adding in R expressions [OK]
Common Mistakes:
  • Adding before multiplying ignoring precedence
  • Expecting error due to syntax
  • Confusing multiplication with addition
4. Identify the error in the following R code snippet:
print('Hello World)
medium
A. Missing closing parenthesis for print
B. Missing closing quote for the string
C. print() is not a valid function in R
D. No error, code runs fine

Solution

  1. Step 1: Check string syntax

    The string starts with a single quote but does not have a matching closing quote.
  2. Step 2: Confirm the error type

    This causes a syntax error because the string is not properly closed.
  3. Final Answer:

    Missing closing quote for the string -> Option B
  4. Quick Check:

    Unclosed string = syntax error [OK]
Hint: Always close quotes around strings [OK]
Common Mistakes:
  • Thinking print() is invalid in R
  • Ignoring missing quote and blaming parentheses
  • Assuming code runs without errors
5. You want to run multiple R commands stored in a file named analysis.R. Which is the best way to run all commands at once and see their output in the console?
hard
A. Open the file and run each line manually
B. Copy and paste the file content into the console line by line
C. Use source('analysis.R') to run the whole script
D. Restart R and open the file without running it

Solution

  1. Step 1: Understand how to run multiple commands in R

    Running a whole script at once is done using the source() function.
  2. Step 2: Identify the best method for running all commands and seeing output

    source('analysis.R') runs all commands in the file and shows output in the console.
  3. Final Answer:

    Use source('analysis.R') to run the whole script -> Option C
  4. Quick Check:

    Run script = source() [OK]
Hint: Use source() to run entire R scripts at once [OK]
Common Mistakes:
  • Running lines manually wastes time
  • Copy-pasting line by line is inefficient
  • Restarting R does not run code automatically
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