R Programming Language Certification R Programming Language: Computer Science 4 ā Questions and Answers
Question 1: In R, what is the difference between `=` and `<-` for assignment inside a function call?
- They are completely interchangeable
- = assigns locally in the call; <- assigns in the current environment (Correct answer)
- = assigns globally; <- assigns locally
- There is no difference in R 4.0+
Correct answer: = assigns locally in the call; <- assigns in the current environment
Inside a function call, = sets a named argument while <- creates a variable in the calling environment.
Question 2: Which R function tests whether an object inherits from a given class?
- typeof()
- is()
- inherits() (Correct answer)
- class()
Correct answer: inherits()
inherits(x, 'classname') returns TRUE if x has the specified class anywhere in its class hierarchy.
Question 3: What is the result of `nchar(NA)` in R?
- 0
- 2
- NA (Correct answer)
- Error
Correct answer: NA
nchar() propagates NA by default, returning NA when given an NA input.
Question 4: In R, which environment is searched last in the search path?
- .GlobalEnv
- package:base (Correct answer)
- package:stats
- Autoloads
Correct answer: package:base
package:base is always the last environment on the search path, ensuring base functions are always found.
Question 5: What does `match.arg()` do inside an R function?
- Matches a numeric argument to an index
- Validates and partially matches a character argument against predefined choices (Correct answer)
- Converts arguments to a list
- Checks argument types at runtime
Correct answer: Validates and partially matches a character argument against predefined choices
match.arg() validates a character argument against allowed values and supports partial matching.
Question 6: Which of the following creates a factor with a specific level order in R?
- factor(x, ordered=TRUE)
- factor(x, levels=c('low','mid','high')) (Correct answer)
- as.factor(x)
- ordered(x)
Correct answer: factor(x, levels=c('low','mid','high'))
Passing levels= to factor() sets the explicit order of levels, independent of whether the factor is ordered.
Question 7: What is the time complexity of looking up an element by name in a large R named list?
- O(1) ā hash table lookup
- O(n) ā linear scan (Correct answer)
- O(log n) ā binary search
- O(n²) ā nested scan
Correct answer: O(n) ā linear scan
R named lists use sequential name matching internally, making named lookups O(n) in the list length.
In R, what is the difference between `=` and `<-` for assignment inside a function call?