R Programming Questions and Answers – Vectorized Operations – 1

This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Vectorized Operations – 1”.

1. Which of the following is example of vectorized operation as far as subtraction is concerned?

> x <- 1:4
> y <- 6:9

a) x+y
b) x-y
c) x/y
d) x–y
View Answer

Answer: b
Explanation: Subtraction, multiplication and division are also vectorized.

2. Point out the wrong statement?
a) Very less operations in R are vectorized
b) Vectorization allows you to write code that is efficient, concise, and easier to read than in non-vectorized languages
c) vectorized means that operations occur in parallel in certain R objects
d) Matrix operations are also vectorized
View Answer

Answer: a
Explanation: Many operations in R are vectorized.

advertisement
advertisement

3. What will be the output of the following R code?

> x <- 1:4
> y <- 6:9
> z <- x + y
> z

a) 7 9 11 13
b) 7 9 11 13 14
c) 9 7 11 13
d) NULL
View Answer

Answer: a
Explanation: This is simplest example of adding two vectors together.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. What will be the output of the following R code?

advertisement
> x <- 1:4
> x > 2

a) 1 2 3 4
b) FALSE FALSE TRUE TRUE
c) 1 2 3 4 5
d) 5 4 3 1 2 1
View Answer

Answer: b
Explanation: Another operation you can do in a vectorized manner is logical comparisons.

advertisement

5. Point out the wrong statement?
a) Dates are represented by the Date class
b) Times are represented by the POSIXct or the POSIXlt class
c) Dates are represented by the DateTime class
d) Times can be coerced from a character string
View Answer

Answer: c
Explanation: Dates are stored internally as the number of days since 1970-01-01 while times are stored internally as the number of seconds since 1970-01-01.

6. What will be the output of the following R code?

> x <- 1:4
> y <- 6:9
> x/y

a) 0.1666667 0.2857143 0.4444444
b) 0.1666667 0.2857143 0.3750000 0.4444444
c) 0.2857143 0.3750000 0.4444444
d) Error
View Answer

Answer: b
Explanation: Logical operations return a logical vector of TRUE and FALSE.

7. What will be the output of the following R code?

> x <- matrix(1:4, 2, 2)
> y <- matrix(rep(10, 4), 2, 2)
> x * y

a)

[,1] [,2]
[1,] 10 30
[2,] 20 40

b)

[,1] [,2]
[1,] 10 30
[2,] 30 40

c)

[,1] [,2]
[1,] 20 30
[2,] 20 40

d) Error
View Answer

Answer: a
Explanation: Matrix operations are also vectorized, making for nicely compact notation.

8. Which of the following code represents internal representation of a Date object?
a) class(as.Date(“1970-01-02”))
b) unclass(as.Date(“1970-01-02”))
c) unclassint(as.Date(“1970-01-02”))
d) classint(as.Date(“1970-02-02”))
View Answer

Answer: b
Explanation: You can see the internal representation of a Date object by using the unclass() function.

9. What will be the output of the following R code?

> x <- as.Date("1970-01-01")
> x

a) “1970-01-01”
b) “1970-01-02”
c) “1970-02-01”
d) “1970-02-02”
View Answer

Answer: a
Explanation: Dates are represented by the Date class and can be coerced from a character string using the as.Date() function.

10. What will be the output of the following R code?

> x <- Sys.time()
> class(x)

a) “POSIXct” “POSIXt”
b) “POSIXXt” “POSIXt”
c) “POSIXct” “POSIct”
d) “POSIXt” “POSIXt”
View Answer

Answer: a
Explanation: Times can be coerced from a character string using the as.POSIXlt or as.POSIXct function.

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.