Bird
Raised Fist0
R Programmingprogramming~20 mins

R Console and script files in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
R Console and Script Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of variable assignment in R Console
What will be the output in the R console after running this code?
R Programming
x <- 5
x + 3
AError: object 'x' not found
B[1] 53
C[1] 15
D[1] 8
Attempts:
2 left
💡 Hint
Remember that '<-' assigns a value to a variable and '+' adds numbers.
❓ Predict Output
intermediate
2:00remaining
Output of sourcing an R script file
Given an R script file named 'script.R' containing: my_var <- 10 print(my_var * 2) What will be printed in the R console after running source('script.R')?
R Programming
my_var <- 10
print(my_var * 2)
AError: object 'my_var' not found
B[1] 20
C[1] 10
DNULL
Attempts:
2 left
💡 Hint
The print function outputs the value to the console.
❓ Predict Output
advanced
2:00remaining
Output of a function defined and called in R Console
What is the output of this R code run in the console? add <- function(a, b) { return(a + b) } add(4, 7)
R Programming
add <- function(a, b) {
  return(a + b)
}
add(4, 7)
A[1] 11
BNULL
C[1] 47
DError: object 'add' not found
Attempts:
2 left
💡 Hint
Functions return the sum of their inputs here.
❓ Predict Output
advanced
2:00remaining
Result of running incomplete R script in console
What error message will appear if you run this incomplete R script in the console? x <- c(1, 2, 3 mean(x)
R Programming
x <- c(1, 2, 3
mean(x)
AError: unexpected end of input
B[1] 2
CError: object 'x' not found
DNULL
Attempts:
2 left
💡 Hint
Check if all parentheses are closed properly.
🧠 Conceptual
expert
2:00remaining
Difference between running code in R Console vs script file
Which statement correctly describes a key difference between running code directly in the R console and running code from an R script file?
AScript files automatically print all variable values, but console does not.
BCode in the console cannot create variables, but script files can.
CCode in the console runs line-by-line immediately, while code in a script file runs only when sourced or executed.
DConsole code runs slower than script files because it is interpreted.
Attempts:
2 left
💡 Hint
Think about when code executes in each environment.

Practice

(1/5)
1. What is the main purpose of the R Console?
easy
A. To type and run commands one at a time
B. To save many commands in a file
C. To create graphical plots only
D. To install R packages automatically

Solution

  1. Step 1: Understand the R Console function

    The R Console allows users to type commands and see results immediately, one command at a time.
  2. Step 2: Compare with script files

    Script files are used to save many commands to run later, not the console's main role.
  3. Final Answer:

    To type and run commands one at a time -> Option A
  4. Quick Check:

    R Console = run commands one by one [OK]
Hint: Console runs commands instantly, scripts save commands [OK]
Common Mistakes:
  • Confusing console with script file
  • Thinking console saves commands permanently
  • Assuming console only creates plots
2. Which of the following is the correct way to run a script file named analysis.R in R?
easy
A. execute("analysis.R")
B. run("analysis.R")
C. source("analysis.R")
D. load("analysis.R")

Solution

  1. Step 1: Recall R function to run scripts

    In R, the function source() is used to run commands saved in a script file.
  2. Step 2: Check other options

    Functions like run(), execute(), and load() do not run script files; load() loads saved data, not scripts.
  3. Final Answer:

    source("analysis.R") -> Option C
  4. Quick Check:

    Run script = source() [OK]
Hint: Use source() to run saved R scripts [OK]
Common Mistakes:
  • Using load() to run scripts
  • Trying run() or execute() which don't exist
  • Forgetting to use quotes around filename
3. What will be the output when you run the following commands in the R Console?
x <- 5
y <- 3
z <- x + y
print(z)
medium
A. NULL
B. 53
C. Error: object 'z' not found
D. 8

Solution

  1. Step 1: Calculate the sum of x and y

    x is assigned 5, y is assigned 3, so z = 5 + 3 = 8.
  2. Step 2: Print the value of z

    The print(z) command outputs the value stored in z, which is 8.
  3. Final Answer:

    8 -> Option D
  4. Quick Check:

    5 + 3 = 8 [OK]
Hint: Add variables then print result [OK]
Common Mistakes:
  • Concatenating numbers as strings (53)
  • Forgetting to assign z before print
  • Expecting NULL output
4. You wrote this script file calc.R:
result <- a + b
print(result)

When you run source("calc.R") in the console, you get an error. What is the problem?
medium
A. Variables a and b are not defined before use
B. The print() function is misspelled
C. source() cannot run script files
D. The assignment operator <- is incorrect

Solution

  1. Step 1: Check variable definitions

    The script uses variables a and b without assigning values first, causing an error.
  2. Step 2: Verify other parts

    print() is correct, source() runs scripts, and <- is valid assignment in R.
  3. Final Answer:

    Variables a and b are not defined before use -> Option A
  4. Quick Check:

    Undefined variables cause errors [OK]
Hint: Define variables before using them in scripts [OK]
Common Mistakes:
  • Assuming print() is wrong
  • Thinking source() can't run scripts
  • Confusing assignment operator syntax
5. You want to organize your R work by saving commands in a script file and running them all at once. Which approach is best?
hard
A. Use load() to run commands saved in a script
B. Write commands in a script file and use source() to run them
C. Save commands in a text file and open it in a text editor only
D. Type all commands directly in the console one by one

Solution

  1. Step 1: Understand script file usage

    Script files save many commands so you can run them together later.
  2. Step 2: Use source() to run scripts

    The source() function runs all commands in the script file at once.
  3. Step 3: Evaluate other options

    Typing in console is manual and slow; text files alone don't run commands; load() loads data, not scripts.
  4. Final Answer:

    Write commands in a script file and use source() to run them -> Option B
  5. Quick Check:

    Scripts + source() = organized runs [OK]
Hint: Save commands in script, run with source() [OK]
Common Mistakes:
  • Running scripts by typing manually
  • Using load() instead of source()
  • Not saving commands in R script format
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