R Programming Language Certification R Programming Language: Computer Science 5 — Questions and Answers
Question 1: In R's S3 object system, how does `UseMethod()` determine which method to call?
- It checks the object's typeof()
- It dispatches based on the first element of class() (Correct answer)
- It uses the object's mode()
- It calls the most recently defined method
Correct answer: It dispatches based on the first element of class()
UseMethod() dispatches to a function named generic.classname using the first element of the object's class vector.
Question 2: Which R function serializes an R object to a binary format for saving to disk?
- write.csv()
- saveRDS() (Correct answer)
- dump()
- dput()
Correct answer: saveRDS()
saveRDS() saves a single R object to a binary .rds file that can be restored with readRDS().
Question 3: What does `Reduce('+', list(1,2,3,4))` return in R?
- list(1,3,6,10)
- 10 (Correct answer)
- c(1,2,3,4)
- Error
Correct answer: 10
Reduce applies + cumulatively: ((1+2)+3)+4 = 10, returning the final scalar result.
Question 4: In R, what is a closure?
- A function that takes no arguments
- A function that captures its enclosing environment (Correct answer)
- A sealed environment that cannot be modified
- A class with private fields
Correct answer: A function that captures its enclosing environment
A closure is a function bundled with a reference to its enclosing lexical environment at creation time.
Question 5: Which R function checks for duplicate rows in a data frame?
- unique()
- distinct()
- duplicated() (Correct answer)
- anyDuplicated()
Correct answer: duplicated()
duplicated() returns a logical vector marking each row that is a duplicate of an earlier row.
Question 6: What is the purpose of `options(warn=2)` in R?
- Suppresses all warnings
- Converts warnings into errors (Correct answer)
- Prints warnings immediately
- Increases the warning buffer size
Correct answer: Converts warnings into errors
Setting warn=2 causes R to treat warnings as errors, halting execution when a warning is triggered.
Question 7: Which operator in R is used to access a function from a package without loading the package?
- @
- $
- :: (Correct answer)
- ->
Correct answer: ::
The :: operator lets you call a function as package::function() without attaching the package to the search path.
In R's S3 object system, how does `UseMethod()` determine which method to call?