R Programming Language Certification R Programming Language: Computer Science 3 — Questions and Answers
Question 1: Which R function applies a function to each element of a list and returns a list?
- sapply()
- lapply() (Correct answer)
- tapply()
- vapply()
Correct answer: lapply()
lapply() always returns a list, unlike sapply() which attempts simplification.
Question 2: What does the `<<-` operator do in R?
- Creates a local variable
- Assigns a value in the global or enclosing environment (Correct answer)
- Compares two values
- Appends to an existing object
Correct answer: Assigns a value in the global or enclosing environment
<<- performs assignment in the parent (enclosing) environment, bypassing the current local scope.
Question 3: In R, `Sys.time()` returns an object of which class?
- Date
- POSIXct POSIXt (Correct answer)
- character
- numeric
Correct answer: POSIXct POSIXt
Sys.time() returns a POSIXct object representing the current date-time as seconds since the Unix epoch.
Question 4: Which function converts a character vector to lowercase in R?
- str_lower()
- lower()
- tolower() (Correct answer)
- casefold(upper=FALSE)
Correct answer: tolower()
tolower() is the base R function for converting strings to lowercase.
Question 5: What is the default `stringsAsFactors` behavior in R 4.0+ when using `data.frame()`?
- TRUE — strings become factors by default
- FALSE — strings remain character vectors (Correct answer)
- Depends on global options only
- Strings are converted to integers
Correct answer: FALSE — strings remain character vectors
Since R 4.0, stringsAsFactors defaults to FALSE, keeping character columns as character vectors.
Question 6: Which R construct allows you to iterate over named elements of a list while preserving names?
- for (x in lst)
- lapply(lst, function(x) x) (Correct answer)
- mapply()
- Map(f, lst)
Correct answer: lapply(lst, function(x) x)
lapply() preserves the names of its input list in the returned list.
Question 7: What does `tryCatch()` return when no error occurs?
- NULL
- The value of the finally expression
- The value of the expr argument (Correct answer)
- TRUE
Correct answer: The value of the expr argument
tryCatch() returns the value of expr when it completes without triggering a handler.
Which R function applies a function to each element of a list and returns a list?