R Programming Questions and Answers – Console Input and Evaluation – 2

This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Console Input and Evaluation – 2”.

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”
advertisement
advertisement

b)

1   “X”
2   “Y”
3   “ Y”

c)

1   “W”
2   “Y”
3   “ Z”
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

d) Error
View Answer

Answer: a
Explanation: 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?

advertisement
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
View Answer

Answer: a
Explanation: 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().
advertisement

3. What is the meaning of the following R function?

print( sqrt(2) )

a) 1.414314
b) 1.414214
c) Error
d) 14.1414
View Answer

Answer: b
Explanation: 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?

d <- date()
d

a) Prints todays date
b) Prints some date
c) Prints exact present time and date
d) Error
View Answer

Answer: c
Explanation: 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”
View Answer

Answer: d
Explanation: 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”
View Answer

Answer: b
Explanation: 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?

nchar()

a) no. of characters
b) first 5 characters
c) last 5 characters
d) Does not exist
View Answer

Answer: a
Explanation: 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
Explanation: 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?

Sys.Date()

a) Tomorrow date
b) Present date
c) Some date
d) Yesterday date
View Answer

Answer: b
Explanation: 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?

Sys.time()

a) Tomorrow date and time
b) Present date and time
c) Some date
d) Yesterday date and time
View Answer

Answer: b
Explanation: 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.

Sanfoundry Global Education & Learning Series – R Programming Language.

Here’s the list of Best Books in R Programming Language .

To practice all areas of R Programming Language, Here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.