R Programming Language Multiple Choice Questions on “Basics”.
1. What is output of getOption(“defaultPackages”) in R studio?
a) Installs a new package
b) Shows default packages in R
c) Error
d) Nothing will print
Answer: b
Clarification: There are base packages (which come with R automatically), and contributed packages. The base packages are maintained by a select group of volunteers, called R Core. In addition to the base packages, there are over ten thousand additional contributed packages written by individuals all over the world.
2. What will be the output of the following R code?
x <- c(3, 7, NA, 4, 7) y <- c(5, NA, 1, 2, 2) x + y
a) Symbol
b) Missing Data
c) 5
d) 15.5
Answer: b
Clarification: Missing data are a persistent and prevalent problem in many statistical analyses, especially those associated with the social sciences. R reserves the special symbol NA to represent missing data. Ordinary arithmetic with NA value gives NA’s (addition, subtraction, etc.) and applying a function to a vector that has a NA in it will usually give a NA.
3. R language is a dialect of which of the following languages?
a) S
b) C
c) MATLAB
d) SAS
Answer: a
Clarification: The R language is a dialect of S which was designed in the 1980s. Since the early 90’s the life of the S language has gone down a rather winding path. The scoping rules for R are the main feature that makes it different from the original S language.
4. R language has superficial similarity with _________
a) C
b) Python
c) MATLAB
d) SAS
Answer: a
Clarification: The language syntax has a superficial similarity with C, but the semantics are of the FPL (functional programming language) variety with stronger affinities with Lisp and APL. There are many syntaxes in C, which are closely resembled with R.
5. What is the mode of ‘a’ in the following R code?
a <- c(1,” a”, FALSE)
a) Numeric
b) Character
c) Integer
d) Logical
Answer: b
Clarification: All three elements can be expressed as a character. Both paste() and cat() will printout text to the console by combining multiple character vectors together. The original data are formatted as character strings so we convert them to R’s Date format for easier manipulation.
6. What is the length of b?
a) 4
b) 5
c) 6
d) 0
Answer: c
Clarification: Length of b [1] 2 3 4 5 6 7 is 6. We can also create an empty list of a prespecified length with the vector() function. Data frames are represented as a special type of list where every element of the list has to have the same length.
7. What is the mode of b in the following R code?
a) Numeric
b) Character
c) Integer
d) Logical
Answer: a
Clarification: All the elements in ‘b’ can be expressed in numeric. Both paste() and cat() will printout text to the console by combining multiple character vectors together. The original data are formatted as character strings so we convert them to R’s Data format for easier manipulation.
8. What are the typeof(x) and mode(x) in the following R syntax?
a) Numeric, Integer
b) Integer, Numeric
c) Integer, Integer
d) Numeric, Numeric
Answer: b
Clarification: Here typeof() tells about the data type. They are an important type of object in R and are used in a variety of statistical modelling applications. You can determine an object’s type with the typeof function.
9. How many atomic vector types does R have?
a) 5
b) 6
c) 8
d) 10
Answer: b
Clarification: R language has 6 atomic data types. They are logical, integer, real, complex, string (or character) and raw. There is also a class for “raw” objects, but they are not commonly used directly in data analysis.
10. What is the function to set row names for a data frame?
a) row.names()
b) colnames()
c) col.names()
d) column name cannot be set for a data frame
Answer: a
Clarification: row.names() is the function to set row names for a data frame. Data frames have a special attribute called row.names, which indicate information about each row of the data frame.