R Programming Language Multiple Choice Questions on “Console Input and Evaluation”.
1. What will be the output of the following R function?
ab <- list(1, 2, 3, "X", "Y", "Z") dim(ab) <- c(3,2) print(ab)
a)
1 “X” 2 “Y” 3 “ Z”
b)
1 “X” 2 “Y” 3 “ Y”
c)
1 “W” 2 “Y” 3 “ Z”
d) Error
Answer: a
Clarification: List is a special type of vector that can contain elements of different classes. Lists is a very important data type in R. Lists can be created using the list() function, which takes an arbitrary number of arguments.
2. What is the meaning of the following R function?
x <- c(4, 5, 1, 2, 3, 3, 4, 4, 5, 6) x <- as.factor(x)
a) x becomes a factor
b) x is a factor
c) x does not exist
d) x is not a vector
Answer: a
Clarification: Factors are used to represent categorical data and can unordered and ordered. One can think of a factor as an integer vector where each integer has a label. Factors are important in statistical modelling and are treated specially by modelling functions like lm() and glm().
3. What is the meaning of the following R function?
a) 1.414314
b) 1.414214
c) Error
d) 14.1414
Answer: b
Clarification: Functions are often used to encapsulate a sequence of expressions that need to be executed numerous times, perhaps under slightly different conditions. Functions are also often written when code must be shared with others.
4. What will be the output of the following R function?
a) Prints todays date
b) Prints some date
c) Prints exact present time and date
d) Error
Answer: c
Clarification: Dates are represented by the Date class and can be coerced from a character string using the as.Date() function. This is a common way to end in R with a Date. There are other alternatives to print date too.
5. What will be the output of the following R function?
paste("Everybody", "is", “a” , "warrior")
a) “Everybody”, “is”, “a” , “warrior”
b) Everybody is a warrior
c) Everybody”, “is”, “a” , “warrior
d) “Everybody is a warrior”
Answer: d
Clarification: Both paste() and cat() print out text to the console by combining multiple character vectors together, it is impossible for those functions to know in advance how many character vectors will be passed to the function by the user.
6. What will be the output of the following R function?
cat("Everybody", "is", "a", “warrior”,sep="*")
a) “Everybody”, “is”, “a” , “warrior”
b) Everybody*is*a*warrior
c) Everybody”, “is”, “a” , “warrior
d) “Everybody*is*a*warrior”
Answer: b
Clarification: Both paste() and cat() print out text to the console by combining multiple character vectors together, it is impossible to those functions to know in advance how many character vectors will be passed to the function by the user.
7. What will be the output of the following R function?
a) no. of characters
b) first 5 characters
c) last 5 characters
d) Does not exist
Answer: a
Clarification: nchar will print the number of characters as output. It does not print the whole characters, it just prints the number. It does not print first 5 and also last 5 characters of the input string.
8. What will be the output of the following R function?
d <- diag(5, nrow=2, ncol=2) d
a)
5 1 0 5
b)
5 0 5 5
c)
5 0 15 5
d)
5 0 0 5
View Answer
Answer: d
Clarification: Generally, the matrix can be printed with a matrix function. But the diagonal matrix can be easily created with diag function. In the question, we have in the same way.
9. What will be the output of the following R function?
a) Tomorrow date
b) Present date
c) Some date
d) Yesterday date
Answer: b
Clarification: Sys.time and also Sys.Date returns the system’s idea of the current date with and without time. Sys.time returns an absolute date-time value which can be converted to various time zones and may return different days. Sys.Date returns the current day in the current time zone.
10. What will be the output of the following R function?
a) Tomorrow date and time
b) Present date and time
c) Some date
d) Yesterday date and time
Answer: b
Clarification: Sys.time returns a present date-time value which can be converted to various time zones and may return different days. Sys.time and also Sys.Date returns the system’s idea of the current date with and without time.