R Programming Language Certification R Programming Language: Computer Science 2 — Questions and Answers
Question 1: Which R function returns the number of elements in a vector?
- nrow()
- length() (Correct answer)
- dim()
- size()
Correct answer: length()
length() returns the total number of elements in a vector or list.
Question 2: What does the '%%' operator do in R?
- Integer division
- Modulo (remainder) (Correct answer)
- Matrix multiplication
- Exponentiation
Correct answer: Modulo (remainder)
The %% operator returns the remainder after division, e.g., 7 %% 3 returns 1.
Question 3: In R, which class does the result of `1L` belong to?
- numeric
- double
- integer (Correct answer)
- complex
Correct answer: integer
The L suffix forces R to store the value as an integer rather than a double.
Question 4: What is the output of `which(c(FALSE, TRUE, FALSE, TRUE))`?
- c(FALSE, TRUE)
- c(1, 3)
- c(2, 4) (Correct answer)
- c(TRUE, TRUE)
Correct answer: c(2, 4)
which() returns the indices of TRUE values; positions 2 and 4 are TRUE.
Question 5: Which statement correctly creates a named list in R?
- list[name='Alice', age=30]
- list(name='Alice', age=30) (Correct answer)
- c(name='Alice', age=30)
- dict(name='Alice', age=30)
Correct answer: list(name='Alice', age=30)
list() with named arguments creates a named list; c() creates a named vector but coerces types.
Question 6: What does `NA_real_` represent in R?
- A missing value of type double (Correct answer)
- A missing value of type integer
- An undefined logical value
- A missing character string
Correct answer: A missing value of type double
NA_real_ is a typed NA constant specifically for double (real) vectors, avoiding type coercion.
Question 7: In R, what is the result of `class(matrix(1:4, nrow=2))`?
- array
- data.frame
- matrix (Correct answer)
- matrix array
Correct answer: matrix
matrix() creates an object with class 'matrix', which also inherits 'array' but class() returns 'matrix'.
Which R function returns the number of elements in a vector?