250+ TOP MCQs on Vectorized Operations and Answers

R Programming Language Multiple Choice Questions on “Vectorized Operations ”.

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

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

Answer: b
Clarification: 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

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

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

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

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

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

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

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

Answer: c
Clarification: 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

Answer: b
Clarification: 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

Answer: a
Clarification: 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”))

Answer: b
Clarification: 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”

Answer: a
Clarification: 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”

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

250+ TOP MCQs on Scoping Rules and Answers

R Programming Language Multiple Choice Questions on “Scoping Rules ”.

1. What will be the output of the following R code snippet?

> lm <- function(x) { x * x }
> lm

a) function(x) { x * x }
b) func(x) { x * x }
c) function(x) { x / x }
d) function { x $ x }

Answer: a
Clarification: When R tries to bind a value to a symbol, it searches through a series of environments to find the appropriate value.

2. Point out the correct statement?
a) The search list can be found by using the searchlist() function
b) The search list can be found by using the search() function
c) The global environment or the user’s workspace is always the second element of the search list
d) R has separate namespaces for functions and non-functions

Answer: b
Clarification: Base package is always the last element.

3. A function, together with an environment, makes up what is called a ______ closure.
a) formal
b) function
c) reflective
d) symmetry

Answer: b
Clarification: The function closure model can be used to create functions that “carry around” data with them.

4. Which of the variable in the following R code is variable?

> f <- function(x, y) {
+            x^2 + y / z
+ }

a) x
b) y
c) z
d) yy

Answer: c
Clarification: This function has 2 formal arguments x and y. In the body of the function there is another symbol z. In this case z is called a free variable.

5. Point out the wrong statement?
a) The order of the packages on the search list does not matter
b) R has separate namespaces for functions and non-functions
c) Users can configure which packages get loaded on startup so if you are writing a function
d) The search list can be found by using the search() function

Answer: a
Clarification: The order of the packages on the search list matters, particularly if there are multiple objects with the same name in different packages.

6. R uses _________ scoping6 0 or static scoping.
a) reflective
b) transitive
c) lexical
d) closure

Answer: c
Clarification: Lexical scoping in R means that the values of free variables are searched for in the environment in which the function was defined.

7. The only environment without a parent is the ________ environment.
a) full
b) half
c) null
d) empty

Answer: d
Clarification: Every environment has a parent environment and it is possible for an environment to have multiple “children”.

8. The ________ for R are the main feature that make it different from the original S language.
a) scoping rules
b) closure rules
c) environment rules
d) closure & environment rules

Answer: a
Clarification: This function never actually uses the argument b, so calling f(2) will not produce an error because the 2 gets positionally matched to a.

9. The _________ function is a kind of “constructor function” that can be used to construct other functions.
a) make.pow()
b) make.power()
c) keep.power()
d) keep.pow()

Answer: b
Clarification: Typically, a function is defined in the global environment, so that the values of free variables are just found in the user’s workspace.

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

> g <- function(x) {
+             a <- 3
+             x+a+y
+          ## 'y' is a free variable
+ }
> g(2)

a) 9
b) 42
c) 8
d) Error

Answer: d
Clarification: Object ‘y’ not found error is displayed.

250+ TOP MCQs on Exploratory Data Analysis and Answers

Advanced R Programming Questions & Answers focuses on “Exploratory Data Analysis”.

1. __________ produces box-and-whisker plots.
a) xyplot
b) dotplot
c) barchart
d) bwplot

Answer: d
Clarification: Bwplot plots a series of vertical box-and-whisker plots where the individual boxplots represent the data subdivided by the value of some factor. Optionally the y-axis may be scaled logarithmically. Dotplot produces Cleveland dot plots. Barchart produces bar plots.

2. __________ produces bivariate scatterplots or time-series plots.
a) xyplot
b) dotplot
c) barchart
d) bwplot

Answer: a
Clarification: xyplot has points that show the relationship between two sets of data. Optionally the y-axis may be scaled logarithmically. dotplot produces Cleveland dot plots. barchart produces bar plots.

3. Annotation of plots in any plotting system involves adding points, lines, or text to the plot, in addition to customizing axis labels or adding titles. Different plotting systems have different sets of functions for annotating plots in this way. Which of the following functions can be used to annotate the panels in a multi-panel lattice plot?
a) points()
b) panel.abline()
c) lines()
d) axis()

Answer: b
Clarification: panel.abline() is one of the most used panel function.

4. ____________ produces one-dimensional scatterplots.
a) xyplot
b) stripplot
c) barchart
d) bwplot

Answer: b
Clarification: This function along with other high-level Lattice functions, respond to a common set of arguments.

5. which of the following functions can be used to finely control the appearance of all lattice plots ?
a) par()
b) print.trellis()
c) splom()
d) trellis.par.set()

Answer: d
Clarification: All high-level function in lattice are generic.

6. What is ggplot2 an implementation of?
a) the Grammar of Graphics developed by Leland Wilkinson
b) 3D visualization system
c) the S language originally developed by Bell Labs
d) the base plotting system in R

Answer: a
Clarification: The ggplot2 package, created by Hadley Wickham, offers a powerful graphics language for creating elegant and complex plots.

7. For barchart and _________ non-trivial methods exist for tables and arrays, documented at barchart.table.
a) scatterplot
b) dotplot
c) xyplot
d) scatterplot & xyplot

Answer: b
Clarification: The numeric methods are equivalent to a call with no left hand side and no conditioning variables in the formula.

8. What is a geom in the ggplot2 system?
a) a plotting object like point, line, or other shape
b) a method for making conditioning plots
c) a method for mapping data to attributes like color and size
d) a statistical transformation

Answer: a
Clarification: The bar geom is used to produce 1d area plots.

9. Logical flag is applicable to which of the following plots?
a) scatterplot
b) dotplot
c) xyplot
d) zyplot

Answer: b
Clarification: Logical flag applicable to bwplot, dotplot, barchart, and stripplot.

10. ___________ is used to determine what is plotted for each group.
a) panel.expose
b) panel.impose
c) panel.superpose
d) panel.depose

Answer: c
Clarification: panel.superpose can be combined with different panel.groups functions.

250+ TOP MCQs on Distribution and Answers

R Programming written test Questions & Answers focuses on “Distribution ”.

1. Two important methods in analysis is differentiation and __________ transformation.
a) Bernoulli
b) Fourier
c) Bohr
d) Rutherford

Answer: b
Clarification: Two important methods in the analysis is differentiation and Fourier transformation. Unfortunately, not all functions are differentiable or have a Fourier transform.

2. Every ________ function has a probability distribution function.
a) Continuous
b) Discrete
c) Categorical
d) Random

Answer: a
Clarification: Every continuous random variable x has a great Probability Density Function (PDF). The PDF can sometimes be greater than 1. This is in contrast to the discrete case.

3. Which package resorts to numerical methods when it encounters a model it does not recognize.
a) destrEx
b) ditrEx
c) distrEx
d) Exdistr

Answer: c
Clarification: distrEx provides some extensions of package distr and further functionalities like var, sd, IQR, mad, median, skewness, kurtosis truncated moments.

4. When µ = ___ and σ = ___ we say that the random variable has a standard normal distribution.
a) 0,1
b) 0,0
c) 1,0
d) 1,1

Answer: a
Clarification: When µ = 0 and σ = 1 we say that the random variable has a standard normal distribution and we typically write Z ∼ norm(mean = 0, sd = 1).

5. Which of the following is the symbol of standard normal PDF?
a) φ
b) #
c) &
d) *

Answer: a
Clarification: The lowercase Greek letter phi (φ) is used to denote the standard normal PDF and the capital Greek letter phi Φ is used to denote the standard normal CDF.

6. The __________ package has functionality to investigate transformations of univariate distributions.
a) Distri
b) Dirtr
c) Distr
d) Hyperbolic

Answer: c
Clarification: The distr package has a functionality to find the transformations of one variable distribution. There are exact results for the ordinary transformations for the standard distributions, and distr takes advantage of these in many cases.

7. Which distribution looks like a norm distribution but with very heavy tails?
a) Simple
b) Discrete
c) Continuous
d) Cauchy

Answer: d
Clarification: The Cauchy distribution looks like a norm distribution but with very heavy tails. The mean (and variance) do not exist, that is, they are infinite.

8. The ________ is represented by the location parameter.
a) Median
b) Mode
c) Mean
d) Variance

Answer: a
Clarification: The median is represented by the location parameter, and the scale parameter influences the spread of the distribution about its median.

9. The ______ parameter influences the spread of the distribution about its median.
a) Scale
b) Mode
c) Mean
d) Variance

Answer: a
Clarification: The scale parameter influences the spread of the distribution about its median. The median is represented by the location parameter.

10. Which distribution comes up a lot in Bayesian statistics because it is a good model for one’s prior beliefs about a population proportion?
a) Bohr
b) Discrete
c) Alpha
d) Beta

Answer: d
Clarification: Beta distribution comes up a lot in Bayesian statistics because it is a good model for one’s prior beliefs about a population proportion.

11. The associated R function is dlogis (x, location = 0, scale = 1) is for _________ distribution.
a) Logistic
b) Linear
c) Discrete
d) Beta

Answer: a
Clarification: The associated R function is dlogis (x, location = 0, scale = 1). The logistic distribution comes up in differential equations as a model for population growth under certain assumptions.

12. The _________ distribution comes up in differential equations as a model for population growth under certain assumptions.
a) Logistic
b) Linear
c) Discrete
d) Beta

Answer: a
Clarification: The associated R function is dlogis(x, location = 0, scale = 1). The logistic distribution comes up in differential equations as a model for population growth under certain assumptions.

13. Which of the following is a distribution derived from the normal distribution?
a) Logistic
b) Lognormal
c) Normal
d) Simple

Answer: b
Clarification: Lognormal distribution is a distribution derived from the normal distribution (hence the name). Its name also says that it is derived from the normal distribution.

14. The associated R function is dlnorm(x, meanlog = 0, sdlog = 1) is for ________ distribution.
a) Logistic
b) Lognormal
c) Normal
d) Simple

Answer: b
Clarification: The associated R function is dlnorm(x, meanlog = 0, sdlog = 1). Lognormal distribution is a distribution derived from the normal distribution (hence the name). Its name also says that it is derived from the normal distribution.

15. The associated R function is dweibull(x, shape, scale = 1) is for _________ distribution.
a) Logistic
b) Lognormal
c) Weibull
d) Simple

Answer: c
Clarification: The associated R function is dweibull(x, shape, scale = 1). There are exact results for ordinary transformations of the standard distributions, and distr takes advantage of these in many cases.

250+ TOP MCQs on Console Input and Evaluation and Answers

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.

250+ TOP MCQs on Vectorized Operations and Answers

R Programming Interview Questions and Answers for experienced focuses on “Vectorized Operations”

1. Which of the following function gives the day of the week?
a) weekdays
b) months
c) quarters
d) semesters

Answer: a
Clarification: Weekdays will give the day of the week. months function give the month name. Quarters divide the year into fourths. Semesters divide the year into halfs.

2. Point out the correct statement?
a) Times use the POSIXct and POSIXlt class
b) Dates and times have special classes in R that allow for numerical and statistical calculations
c) Character strings can be coerced to Date/Time classes using the strptime function
d) All of the mentioned

Answer: d
Clarification: Character strings can be coerced to Date/Time classes using the as.Date, as.POSIXlt, or as.POSIXct.

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

> p <- as.POSIXlt(x)
> names(unclass(p))
> p$wday

a) 1
b) 2
c) 3
d) NULL

Answer: a
Clarification: The POSIXlt object contains some useful metadata.

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

> datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
> x <- strptime(datestring, "%B %d, %Y %H:%M")
> x

a) “2012-01-10 10:40:00 EST” “2011-12-09 09:10:00 EST”
b) “2012-01-10 10:40:00 IST” “2011-12-09 09:10:00 IST”
c) “2012-01-10 10:40:00 GMT” “2011-12-09 09:10:00 GMT”
d) Error

Answer: a
Clarification: strptime() takes a character vector that has dates and times and converts them into to a POSIXlt object.

5. Point out the wrong statement?
a) POSIXct is just a very large integer under the hood
b) POSIXlt stores a bunch of other useful information like the day of the week, day of the year, month, day of the month
c) There are a number of generic functions that work on dates and times to help you extract pieces of dates and/or times
d) None of the mentioned

Answer: d
Clarification: POSIXct uses a useful class when you want to store times in something like a data frame.

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

> x <- as.Date("2012-01-01")
> y <- strptime("9 Jan 2011 11:34:21", "%d %b %Y %H:%M:%S")
> x-y

a) Time difference of 356.3095 days
b) Warning
c) NULL
d) Error

Answer: b
Clarification: You can use mathematical operations on dates and times.

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

> x <- as.Date("2012-03-01")
> y <- as.Date("2012-02-28")
> x-y

a) Time difference of 3 days
b) Time difference of 2 days
c) Time difference of 1 days
d) Time difference of 5 days

Answer: b
Clarification: Matrix operations are also vectorized, making for nicely compact notation.

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

> x <- as.POSIXct("2012-10-25 01:00:00")
> y <- as.POSIXct("2012-10-25 06:00:00", tz = "GMT")
> y-x

a) Time difference of 1 hour
b) Time difference of 1 min
c) Time difference of 1 sec
d) Time difference of 5 sec

Answer: a
Clarification: POSIXct is just a very large integer under the hood.