MCQs on Introduction to Programming Concepts | R

In this quiz, we cover essential programming concepts in R, including writing modular code and functions, performing debugging and error handling, and crafting simple scripts. These topics form the foundation for developing efficient and error-free R code.


Writing Modular Code and Functions

  1. Which of the following is the correct way to define a function in R?
    a) function name() {}
    b) def function() {}
    c) func name() {}
    d) function(name) {}
  2. What keyword is used to return a value from a function in R?
    a) return()
    b) output()
    c) result()
    d) yield()
  3. What does the args() function in R do?
    a) Returns the arguments passed to a function
    b) Defines a function’s arguments
    c) Displays a function’s return value
    d) Specifies variable names in a function
  4. How do you pass a default argument to a function in R?
    a) function(x = 10)
    b) function(x : 10)
    c) function(x) <- 10
    d) function(x = "default")
  5. Which of the following defines a function that adds two numbers in R?
    a) add <- function(a, b) { return(a + b) }
    b) add <- function(a, b) return(a + b)
    c) add(a, b) => a + b
    d) function add(a, b) { a + b }
  6. What is the purpose of a return statement in a function?
    a) It returns the function name
    b) It ends the function and provides an output
    c) It displays the arguments
    d) It declares the function’s result
  7. How would you define a function in R that returns the square of a number?
    a) square <- function(x) x^2
    b) square(x) -> x^2
    c) function square(x) { x^2 }
    d) square(x) = x^2
  8. Which keyword can be used to access a function’s argument list in R?
    a) args()
    b) values()
    c) parameters()
    d) inputs()
  9. What is the scope of a variable defined inside a function in R?
    a) Global
    b) Local
    c) Static
    d) Global or local depending on the use
  10. Which of the following will correctly call a function add(a, b) in R?
    a) add(a = 3, b = 4)
    b) add(3, 4)
    c) Both a and b
    d) None of the above

Debugging and Error Handling

  1. What function is used in R to test for errors in code execution?
    a) debug()
    b) error()
    c) tryCatch()
    d) check()
  2. What is the purpose of the tryCatch() function in R?
    a) To skip over errors
    b) To execute code and handle potential errors
    c) To display errors
    d) To terminate the program
  3. What is the default behavior of the stop() function in R?
    a) It prints an error message and continues the program
    b) It terminates the function and displays an error message
    c) It prints a warning but does not terminate execution
    d) It ignores the error and continues
  4. How would you handle an error using tryCatch() in R?
    a) tryCatch(expression, error = function(e) {print("error occurred")})
    b) tryCatch(expression, catch = function() {error("handle error")})
    c) tryCatch(expression, handle = "error")
    d) tryCatch(expression, error = error())
  5. Which of the following is used to display an error message in R?
    a) print()
    b) message()
    c) cat()
    d) stop()
  6. What is the primary purpose of the debug() function in R?
    a) To prevent errors from stopping the execution
    b) To enable step-by-step debugging
    c) To log errors
    d) To automatically fix bugs
  7. Which of the following functions can be used to continue execution after an error is caught?
    a) stop()
    b) next()
    c) recover()
    d) try()
  8. In R, how can you catch and handle warnings without stopping the execution?
    a) Using tryCatch() with the warning argument
    b) Using try()
    c) Using catch()
    d) Using stop()
  9. What is the result of using try() in R?
    a) It executes a block of code and catches errors
    b) It ignores errors and continues execution
    c) It displays error details without stopping the program
    d) It terminates the program immediately
  10. Which function in R is used to retrieve detailed information about errors?
    a) traceback()
    b) error()
    c) warning()
    d) details()

Writing Simple Scripts

  1. What is the extension for an R script file?
    a) .rscript
    b) .Rdata
    c) .R
    d) .r
  2. How can you execute an R script from the console?
    a) source("script.R")
    b) run("script.R")
    c) execute("script.R")
    d) runScript("script.R")
  3. What command is used to print output to the R console?
    a) print()
    b) display()
    c) cat()
    d) show()
  4. Which command is used to assign a value to a variable in R?
    a) :=
    b) ->
    c) =
    d) Both b and c
  5. How do you comment a line in an R script?
    a) // This is a comment
    b) # This is a comment
    c) /* This is a comment */
    d) -- This is a comment
  6. What does the following R code do?
    if(x < 5) { print("x is less than 5") }
    a) It checks if x is greater than 5
    b) It prints a message if x is less than 5
    c) It throws an error if x is less than 5
    d) It assigns a value to x
  7. How can you check the structure of an R object?
    a) class()
    b) structure()
    c) str()
    d) info()
  8. What is the first step in writing a simple R script?
    a) Define functions
    b) Load data
    c) Write comments
    d) Assign values to variables
  9. Which of the following R functions can be used to load external data into R?
    a) load()
    b) read()
    c) read.table()
    d) import()
  10. How do you run a script automatically when R starts?
    a) Add the script to the .Rprofile file
    b) Use the run() function
    c) Place the script in the R library folder
    d) Use autoRun()

Answer Key

QnoAnswer
1a) function name() {}
2a) return()
3a) Returns the arguments passed to a function
4a) function(x = 10)
5a) add <- function(a, b) { return(a + b) }
6b) It ends the function and provides an output
7a) square <- function(x) x^2
8a) args()
9b) Local
10c) Both a and b
11c) tryCatch()
12b) To execute code and handle potential errors
13b) It terminates the function and displays an error message
14a) tryCatch(expression, error = function(e) {print("error occurred")})
15d) stop()
16b) To enable step-by-step debugging
17c) recover()
18a) Using tryCatch() with the warning argument
19a) It executes a block of code and catches errors
20a) traceback()
21c) .R
22a) source("script.R")
23a) print()
24d) Both b and c
25b) # This is a comment
26b) It prints a message if x is less than 5
27c) str()
28b) Load data
29c) read.table()
30a) Add the script to the .Rprofile file

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top