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
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) {}
What keyword is used to return a value from a function in R? a) return() b) output() c) result() d) yield()
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
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")
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 }
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
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
Which keyword can be used to access a function’s argument list in R? a) args() b) values() c) parameters() d) inputs()
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
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
What function is used in R to test for errors in code execution? a) debug() b) error() c) tryCatch() d) check()
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
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
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())
Which of the following is used to display an error message in R? a) print() b) message() c) cat() d) stop()
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
Which of the following functions can be used to continue execution after an error is caught? a) stop() b) next() c) recover() d) try()
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()
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
Which function in R is used to retrieve detailed information about errors? a) traceback() b) error() c) warning() d) details()
Writing Simple Scripts
What is the extension for an R script file? a) .rscript b) .Rdata c) .R d) .r
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")
What command is used to print output to the R console? a) print() b) display() c) cat() d) show()
Which command is used to assign a value to a variable in R? a) := b) -> c) = d) Both b and c
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
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
How can you check the structure of an R object? a) class() b) structure() c) str() d) info()
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
Which of the following R functions can be used to load external data into R? a) load() b) read() c) read.table() d) import()
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
Qno
Answer
1
a) function name() {}
2
a) return()
3
a) Returns the arguments passed to a function
4
a) function(x = 10)
5
a) add <- function(a, b) { return(a + b) }
6
b) It ends the function and provides an output
7
a) square <- function(x) x^2
8
a) args()
9
b) Local
10
c) Both a and b
11
c) tryCatch()
12
b) To execute code and handle potential errors
13
b) It terminates the function and displays an error message
14
a) tryCatch(expression, error = function(e) {print("error occurred")})