Data Science R Language Introduction and MCQ

Data Science R Language

Q.1 Which of the following while loops will print numbers from 1 to 4?

       A. x<-1 while(x < 5) {x <- x+1; print(x);}

       B. x<-1 while(x < 5) do { print(x); x <- x+1;}

       C. x<-1 while(x < 5) do {x <- x+1; print(x);}

       D. x<-1 while(x < 5) { print(x); x <- x+1;}

Ans : x<-1 while(x < 5) { print(x); x <- x+1;}


Q.2 What would be the result of following code?

       A. x <- c(“x”, “y”, “z”) as.logical(x)

       B. “x” “y” “z”

       C. NA NA NA

       D. NaN NaN NaN

       E. x y z

Ans : NA NA NA


Q.3 What command will you enter in the R console to get help on how to quit R?

       A. help q

       B. man quit

       C. help(q)

       D. info(quit)

Ans : help(q)


Q.4 Which of the following statements is correct?

       A. Control structures like if, while, and for are used to control the flow of an R program

       B. The only way to exit a repeat loop is to call break

C. Infinite loops should generally be avoided

       D. All the options

Ans : All the options


Q.5 What is the output of the R code?
m <- c(1, 2, 3) n <- c(6, 5, 4) (m < 2) & (n > 5)

       A. An error is thrown because the wrong operator is used

       B. {1 2 4}

       C. True

       D. TRUE FALSE FALSE

Ans : True


Q.6 In R, the subsequent data types are all atomic data types except _____________.

       A. Logical

       B. Data frame

       C. Float

       D. Character

Ans : Data frame


Q.7 What would be the result of following code?
x <- 0:4 as.logical(x)

       A. FALSE TRUE TRUE TRUE TRUE

       B. “0” “1” “2” “3” “4”

       C. NA NA NA NA NA

       D. 0 1 2 3 4

Ans : FALSE TRUE TRUE TRUE TRUE


Q.8 What is the class of the object defined by the expression x<- c(4,”a”,TRUE) in R?

       A. Integer

       B. Logical

C. Character

       D. Numeric

       E. Float

Ans : Character


Q.9 Which of the following statements is correct?

       A. Use explicit TRUE and FALSE values when indicating logical values

       B. R operates on named data structures

       C. rm command is used to remove objects in R

       D. All the options

Ans : All the options


Q.10 Which of the following statements is correct?

       A. NaN can also be thought of as a missing value

       B. Number Inf represents infinity in R

       C. The value Nan represents undefined value

       D. All the options

Ans : The value Nan represents undefined value


Q.11 What would be the output of the subsequent code?
x <- c(“a”, “b”, “c”, “c”, “d”, “a”) x[c(1, 3, 4)]

       A. “a” “b” “c”

       B. “b” “c” “d”

       C. “a” “c” “c”

       D. “a” “c” “d”

Ans : “a” “c” “c”


Q.12 Assume that you have a vector x <- c(3,5,1,10,12,6) and you wish to set all elements of this vector that are smaller than 6 to be equal to 0, what R code accomplishes this?

       A. x[x==0]

       B. x[x==6]<-0

       C. x[x<6]==0

D. x[x<6]<-0

Ans : x[x<6]<-0


Q.13 If I have two vectors, x<-c(1,3,5) and y<-c(3,2,10), what does rbind(x,y) give?

       A. A vector length of 3

       B. A 3 x 3 matrix

       C. A 3 x 2 matrix

       D. A 2 x 3 matrix

       E. A vector length of 2

Ans : A 2 x 3 matrix


Q.14 Which function is used to generate Sequences in R?

       A. sequence()

       B. order()

       C. seqn()

       D. seq()

Ans : seq()


Q.15 An important property of vectors in R is that ___________.

       A. A vector cannot have attributes like dimensions

       B. Elements can only be character or numeric

       C. All elements must be of the same class

       D. Elements can be of different classes

Ans : All elements must be of the same class

Q.16 Which command allows you to get the median tree Height of the R sample dataset “trees”?

       A. median(trees$Height)

       B. median(trees.Height)

       C. median(trees)

       D. avg.median(trees$Height)

Ans : median(trees$Height)


Q.17 Which R command creates a 2 by 2 matrix with the values 1, 2, 3, and 4?

       A. m <- [1,2,;3,4]

       B. m <- matrix ( 1 -> 4; 2, 2)

       C. m <- matrix(1:4, 2, 2)

       D. m <- cbin( c(1,2),c(2,1) )

Ans : m <- matrix(1:4, 2, 2)


Q.18 What would be the result of following code?
x <- 4 class(x)

       A. integer

       B. numeric

       C. double

       D. real

Ans : numeric


Q.19 Which function can be used to create collection of vectors objects?

       A. cp()

       B. c()

       C. concat()

       D. None of the options

Ans : c()


Q.20 What is the function in R to get the # of observations in a data frame?

       A. nrow()

       B. nobs()

       C. n()

       D. ntotal()

Ans : nrow()


Q.21 Which of the following statement would print “0” “1” “2” “3” “4” “5” “6” for the following R code?
> x <- 0:6

       A. as.character(x)

       B. as.logical(x)

       C. as.numeric(x)

       D. as.num(y)

Ans : as.character(x)


Q.22 Point out the correct statement?

       A. rep() is be used for replicating an object in various complicated ways

       B. seq() function has four arguments

       C. sequence() is a more general facility for generating sequences

       D. numerical vectors are generated by conditions

Ans : rep() is be used for replicating an object in various complicated ways


Q.23 What will be the output of the following R code?
x <- c(“a”, “b”, “c”) > as.numeric(x)

       A. Warning

       B. Error

       C. Abnormal termination

       D. Prints x

Ans : Warning


Q.24 What will be the output of the following R code?
> x <- c(“a”, “b”, “c”) > as.logical(x)

       A. a b c

       B. NA NA NA

       C. 0 1 2

       D. 6 8 9

Ans : NA NA NA


Q.25 Point out the correct statement?

       A. The elements of a logical vector cannot have the values TRUE, FALSE, and NA

       B. Matrices are vectors with a dimension attribute

       C. Numerical vectors are generated by conditions

       D. seq() function has four arguments

Ans : Matrices are vectors with a dimension attribute

Q.26 Which of the following is invalid assignment?

       A. > m <- matrix(nrow = 2, ncol = 3)

       B. > m <- matrix(nrow = 2, ncol = 3.5)

       C. > m <- mat(nrow = 2, ncol = 5)

       D. > m <- mat(nrow = 2, ncol = 3)

Ans : > m <- matrix(nrow = 2, ncol = 3)


Q.27 What will be the output of the following R code?
> m <- matrix(nrow = 2, ncol = 3)
> dim(m)

       A. 3 2

       B. 2 3

       C. 2 2

       D. 4 5

Ans : 2 3


Q.28 What will be the output of the following R code?
> m <- 1:10
> m

       A. 1 2 3 4 5 6 7 8 9 10

       B. 1 2 3 4 5 6 7 8 9 10 11

       C. 0 1 2 3 4 5 6 7 8 9 10

       D. 10 9 8 6 5 4 2 3 1 2

Ans : 1 2 3 4 5 6 7 8 9 10


R Language more questions

Q.1 R has how many atomic classes of objects?

       A. 1

       B. 2

       C. 3

       D. 5

Ans : 5


Q.2 Point out the correct statement?

       A. Empty vectors can be created with the vector() function

       B. A sequence is represented as a vector but can contain objects of different classes

       C. “raw” objects are commonly used directly in data analysis

       D. The value NaN represents undefined value

Ans : Empty vectors can be created with the vector() function


Q.3 Numbers in R are generally treated as _______ precision real numbers.

       A. double

       B. single

       C. real

       D. imaginary

Ans : double


Q.4 If you explicitly want an integer, you need to specify the _____ suffix.

       A. D

       B. R

       C. L

 D. K

Ans : L


Q.5 Point out the correct statement?

       A. The value NaN represents undefined value

       B. Number Inf represents infinity in R

       C. NaN can also be thought of as a missing value

       D. “raw” objects are commonly used directly in data analysis

Ans : Number Inf represents infinity in R


Q.6 Attributes of an object (if any) can be accessed using the ______ function.

       A. objects()

       B. attrib()

       C. attributes()

       D. obj()

Ans : attributes()


Q.7 R objects can have attributes, which are like ________ for the object.

       A. metadata

       B. expression

       C. features

       D. dimensions

Ans : metadata


Q.8 Which of the following can be considered as object attribute?

       A. dimensions

       B. class

       C. length

D. all of the mentioned

Ans : all of the mentioned


Q.9 The ________ function can be used to create vectors of objects by concatenating things together.

       A. cp()

       B. c()

       C. concat()

       D. con()

Ans : c()


Q.10 What will be the output of the following R code?
> x <- vector(“numeric”, length = 10)
> x

       A. 10

       B. 0 0 0 0 0 0 0 0 0 0

       C. 0 1

       D. 00120

Ans : 0 0 0 0 0 0 0 0 0 0


Q.11 Which of the following statement is invalid?

       A. x >- c(1+0i, 2+4i)

       B. x >- c(TRUE, FALSE)

       C. x >- c(T, F)

       D. None of the mentioned

Ans : None of the mentioned


Q.12 Point out the correct statement?

       A. Use explicit TRUE and FALSE values when indicating logical values

       B. rm command is used to remove objects in R

       C. R operates on named data structures

       D. All of the mentioned

Ans : All of the mentioned

Q.13 What will be the output of the following R code?
> x <- 6
> class(x)

       A. “integer”

       B. “numeric”

       C. “real”

       D. “imaginary”

Ans : “numeric”


Q.14 Point out the correct statement?

       A. The usual operator, <-, can be thought of as a syntactic shortcut to expression operation

       B. Assignment can also be made using the function assignment()

       C. Vectors can be used in arithmetic expressions, in which case the operations are performed element by element

       D. seq() is used to delete the numbers

Ans : Vectors can be used in arithmetic expressions, in which case the operations are performed element by element


Q.15 Which of the following is invalid assignment?

       A. > c(10.4, 5.6, 3.1, 6.4, 21.7) -> x

       B. > assign(“x”, c(10.4, 5.6, 3.1, 6.4, 21.7))

       C. > x <- c(10.4, 5.6, 3.1, 6.4, 21.7)

       D. None of the mentioned

Ans : None of the mentioned


Q.16 What will be the output of the following R code?
> sqrt(-17)

       A. -4.02

       B. 4.02

C. NaN

       D. 3.67

Ans : NaN


Q.17 _______ function returns a vector of the same size as x with the elements arranged in increasing order.

       A. sort()

       B. orderasc()

       C. orderby()

       D. sequence()

Ans : sort()


Q.18 Which of the following code constructs vector of length 11?

       A. > v <- 3*x + y + 1

       B. > v <- 3*x + y + 2

       C. > v <- 2*x + y + 1

       D. > v <- 2*x + y + 4

Ans : > v <- 2*x + y + 1


Q.19 Find the following type of vector?
a <- c(1,2,5.3,6,-2,4)

       A. Numeric

       B. Character

       C. Integer

       D. Logical

Ans : Numeric


Q.20 All columns in a matrix must have the same mode and the _________ length.

       A. Different

       B. Same

       C. May be the same

       D. May be different

Ans : Same


Q.21 ___________ provides optional labels with the columns and rows.

       A. Disnames

       B. Dimnames

       C. Denmes

       D. Demnesd

Ans : Dimnames


Q.22 ________ are similar to matrices but can have more than two dimensions.

A. Functions

       B. Packages

       C. Arrays

       D. Columns

Ans : Arrays


Q.23 Which is more general than a matrix, in that different columns can have different modes?

       A. Data types

       B. Data frames

       C. Data sets

       D. Databases

Ans : Data sets


Q.24 An ordered collection of objects or components are called ________.

       A. Data frames

       B. Datasets

       C. Databases

       D. Lists

Ans : Lists


Q.25 The ________ stores the nominal values as a vector of integers in the range of 1 to unique values in the nominal variable.

       A. Factor

       B. Matrix

       C. Lists

       D. Functions

Ans : Factor


Q.26 An ordered factor is used to represent an __________.

       A. Ordinal variable

       B. Simple variable

       C. Coordinal variable

       D. Biordinal variable

Ans : Ordinal variable


Q.27 On what basis of a variable, OS allocates memory and decides what can be stored in the reserved memory?

       A. Data bases

       B. Data sets

       C. Data types

       D. Lists

Ans : Data types


Q.28 The data type of the R-object becomes the data type of the ________.

       A. Functions

       B. Packages

       C. Variables

       D. Lists

Ans : Variables


About Author


After years of Technical Work, I feel like an expert when it comes to Develop wordpress website. Check out How to Create a Wordpress Website in 5 Mins, and Earn Money Online Follow me on Facebook for all the latest updates.

Leave a Comment