R Programming MCQs

R Programming MCQs

Try to answer these 100+ R Programming MCQs and check your understanding of the R Programming subject.
Scroll down and let's begin!

1:

Find the output of the following code on the R Interpreter.

months <- c{"JAN"."FEB"."MARCH"."APRIL"."MAY"."JUNE"."JULY")

output <- months[c(1.TRUE.4.TRUE.5)]

print(output)


A.  

[1]'JAN' 'JAN' 'APRIL' 'JAN' 'MAY' 

B.  

[1]'JAN' 'FEB' 'APRIL' 'JAN' 'MAY'

C.  

[1]' JAN' 'JAN' 'APRIL' 'MAY' ‘JUNE'

D.  

[1]' JAN' 'FEB' 'MARCH' 'APRIL' 'MAY'

2: Which of the following statements terminates the loop statement and transfers execution to the immediate statement?

A.   if

B.   for

C.   break

D.   next

3: Which of the following functions is used to join multiple vectors to create a data frame?

A.   cbind()

B.   rbind()

C.   merge()

D.   meit()

4:

R uses the function. barplotii. to create bar charts. The basic syntax to create a bar chart In R is:

barplot(H. xlab. yiab. main. namesarg. col)

Which of the following options are correct?


A.  

H is the title of the bar chart.

B.  

main is a vector or matrix containing numeric values used in the bar chart.

C.  

namesarg is a vector of names appearing under each bar.

D.  

col is used to give colors to the bars in the graph.

5:

Which of the given tasks are performed by the following code?

library("rjso")

result <- fromJSON(lile = '"data.json"]

print(result)

A.  

I It loads the package required to read JSON files.

B.  

It reads the file

C.  

It prints the contents of the [son file.


D.  

None of the above.

6: In R programming language, the indexing vectors, xl-[lzn)]. will:

A.   get the first n elements.

B.   get elements from n+1 to the end.

C.   get the nth element

D.   get all elements but the nth element.

7: Which of the following options represents an atomic vector of the integer type?

A.   print(24L)

B.   print(24)

C.   print(12.5)

D.   print(2+3i)

A.   Xl-(iznll

B.   Xllnil

C.   ['1'] xf‌l'name

D.   F1 x[i,j]

9:

What will be the output of the following program on R interpreter?

v <- LETTERS[9.55:15.86]

for ( i in v)

{

if (i = "0")(


next


]

print(i)

}


A.  

[1]'1'

[1] 'J'

[1]'k'


[1] 'L'

[1]M'


[1] 'N'


[1] '0'

B.  

[1] 'l'

[11 'J'

[1] 'K'

[1]'’L'

[1]'M'

[1]W

C.  

No output.

D.  

[1] 'G'

10: What will be the output of the following line of code on R Interpreter?

A.   Empty output

B.   TESTOUTPUT

C.   Compilation error

D.   [1] TESTOUTPUT

11:

What will be the output of the following program on R Interpreter?

v <- 3+16i

printiclass()

v <— 3"46

print[class[v])


v <~ 3l+16

printiclassMJ


v <- 316


print(class[v))

A.  

O [t] 'complex'

[1119

[1] 'character'

[1] 'numeric‘         


B.  

[[1] 'complex'

[1] 'numeric'

[1] 'complex'

[1] 'numeric' 

C.  

Compilation error

D.  

Runtime error

12: In R programming. in order to read a text file, the read.table() function is used. Which of the following options are the valid arguments for this function?

A.   file

B.   header

C.   what

D.   sep

E.   quote

13:

In R programming language. the arguments of a function can be partially matched using the following checks:

Check for a partial match.

Check for a positional match.

Check for exact match for a named argument

Which of the following Is the correct sequence of checks followed by the function to partially match the supplied arguments?


A.  

ABC 

B.  

BCA 

C.  

CAB 

D.  

AcB

14:

What will be the output of the following code on the R Interpreter?

print(seq(7,12. by = 0.68))

A.  

 [1] 7.68 8.36 9.04 9.7210.4011.0811.76  

B.  

[1] 7.00 7.68 8.36 9.04 9.7210.4011.0811.76  

C.  

[1] 7.00 7.68 8.36 9.04 9.7210.4011.08

D.  

[1] 7.68 8.36 9.04 9.7210.4011.08

15:

Following is a function definition in R programming language:


dummy.function <- function(a.b.c) [

result -=- a ‘+ b ' c

print(result)

]


Which of the following function calls will give the output: 41895?

A.  

dummy.function(1345.200.31)

B.  

dummy.function(200.1345.31)

C.  

dummy.function(1345.31.200)

D.  

dumnly.function(b=1345.a=200.c=31]

16: In R programming. which of the following are the valid values for the data type, Integer?

A.   13 4L

B.   4

C.   0L

D.   0

17:

What will be the output of the following program on R interpreter?

OUTPUTMATRIX = matrix( c("1","2"."3"."4"."5"."6"."7"."8"."9"."10"."11","12"). nrow = 4, ncol = 3. byrow = FALSE)

print(OUTPUTMATRlX)


A.  

 [,1] [,2] [.3]

[1,] '1' '2' '3'

[2.] '4' '5' '6'

[3']'7' '8' '9'

[4.] '10' '11' '12'

B.  

[1].[2][.3][,4]

[1'] 1' '2' '3' '4'

[2’] '5' '6' '7' '8'

[3'] '9' '10' 11'12' 

C.  

[111.211.311.41

[1,] '1' ' 4III7IIH10'

[2,] '2' '5ll118lll11'

[3’] '3' '6' '9' '12'

D.  

[.1] [.2] [.3]

[1.] '1' ' 5' '9'

[2.] '2' '6' '10'

[3.] '3' '7' '11'

[4.] '4' '8' '12'   


18:

Following Is a function def‌inition in R programming language:

foo.function <- function(a,b)

{

print{a)

print{b‘b)

}

Which of the following function calls will give an error?

A.  

 foo.function

B.  

 foo.functlon(9,5) 

C.  

 foo.function[b=2,4)

D.  

 foo.functlon(9)

19: in R programming. a pie chart is created using the piel] function. Which of the following parameters indicates the title of the chart?

A.   col

B.   labels

C.   radius

D.   main

20: Which of the following options represents an atomic vector of the numeric type?

A.   print(24L)

B.   print(24)

C.   printr12.5)

D.   print(2+3i)

21:

What will be the output of the following program on R interpreter?

temp <- c("Comp|ex","Simple")

cnt <- 10

repeat

[

print(temp)

cnt <— cnt-1

if(cnt < 6.5) {

break

1

}

A.  

[1]"Complex""Simple"

[1] "Complex""Simple"

[1] "Complex""Simple"

[1] "Complex""Simple"

B.  

Error in code execution

C.  

System timeout error.

D.  

[1]"Complex""Simple"

[1] "Complex""Simple"

22: In R programming language. which of the following statements is NOT correct about operators?

A.   operator creates the series of numbers in sequence for a vector.

B.   0%in% operator is used to multiply a matrix with its transpose.

C.   %/% operator gives result of the division of f‌irst vector with second (quotient).

D.   ^ operator gives the remainder of the division of first vector with the second.

23: Which of the following are the valid vector Inputs to the pie() function?

A.   foo <- c[O. 0.0.0)

B.   foo <. c[10. 10. 10. 70)

C.   foo <- c(-10. 10. 0. 90)

D.   foo <- c[0. 10. 10. 90)

24: Which of the following decision-making statements is used to test a variable for equality against a list of values?

A.   if

B.   If...else

C.   while

D.   Switch

25: There is a csv f‌ile named "data.csv". Which of the following options will read that csv f‌ile in R programming?

A.   output <- read.csv('data.csv')

B.   output <- readcsv(‘'data.csv')

C.   output <- read('data.csv')

D.   output <- readf‌ile.csv('data.csv‘)

26: Which of the following commands is used to check and verify whether or not the ”xlsx" package is installed?

A.   install.packages('xlsx‘)

B.   any(grepl(‘x|sx',instalIed.packages()))

C.   library('xlsx')

D.   read.xlsx()

27: In R programming. a pie chart is created using the plea function. Which of the following parameters is used to give description to the slices?

A.   col

B.   labels

C.   radius

D.   main

28:

Following is a code snippet to be used to draw a boxplot in R:

boxplot(mpg "‘ Cyl, data = mtcars,

xiab = "Number of Cylinders",

ylab = Miles Per Gallon“,

main = "Mileage Data",

notch = TRUE,

varwldth = FALSE.

col = ched","Biue","Green"],

names = c("High","Medium","Low")

Which of the following options is NOT correct?

A.  

 it will draw a boxplot with 'Number of Cylinders' values on x axis and the Miles Per Gallon' data on y axis.

B.  

it will give a title to the graph as Mileage Data   

C.  

it will draw the notch graphs

D.  

It will draw a box with the width proportionate to the sample (data inputted) size.     

E.  

 it will fill the color of the boxes as red, blue and green, respectively.

29: Which of the following functions computes the combinations of k events among n repetitions = ner_(n-k)!kl]?

A.   which(n = k)

B.   match(n, k)

C.   merge(n,k)

D.   choose(n. K)

30:

What wlll be the Output of the following program on R Interpreter?

v <- LETTERS[5:1]

{or (i in v)

{

print(i)

}

A.  

[1] “E”

[1] "D"

[1] “C”

[1] "B"

[1] "A.” 

B.  

[1] “A”

[1] 'B-.

[I] "C'

C.  

[1] “A"

[‘1 “B

[1] "C'

[1]  D"

[1] "E'

D.  

No output.

31: Which of the following functions is used for variable conversions?

A.   as.array

B.   ls.data.frame

C.   class(x)

D.   whichmax(x)

32:

Find the error in following code snippet

foodata <- data.frame(

empid = C(11:15),

empname = c("EMP2"."EMP3"."EMP4","EMP5"),

"" = c(100,200,300,400,500},

empjoindate = asDatetcf'2017—01-01", '2017~02~01", '2017-03-01‘, "2017-0401”,"2017~05-01‘)),

stringsAsFactors = FALSE

)

print(foo.data)

A.  

 One of the column names has a zero-length variable name.

B.  

The column

C.  

The data stored in a data frame is not of character type.

D.  

 The row names are not unique.

33:

Following is a code snippet executed on R interpreter:

sales <- c(10,20.30.40.50)

months <- c{"JAN".“FEB"."MARCH"."APRIL"."MAY")

png(file = "months_sales.png")

barplot(sales,names.arg = months.xlab = "Month".ylab = "Sales",col = "Red",main = "Sales Chart“,border =

"Black")

What does this code do?


A.  

It creates the vector data for the bar chart.

B.  

It gives a name to the chart f‌ile.

C.  

It plots the bar chart.

D.  

It saves the chart as per the mentioned f‌ile name.

34: Which of the following statements is/are correct?

A.   print(rootnode[[1]]11]]) will get the f‌irst element of the second node

B.   printfrootnodef‌limm) will get the f‌irst element of the f‌irst node.

C.   xmlToDataFrarnefinputxml

D.   xmlSize(rootnode) will f‌ind the number of child nodes under the root node.

35:

Following is a function def‌inition in R programming language:

foo.function <- function(a =1, b = 2. c = 3)

{result <- a ’ b+ c

print(result)

}

A.  

 foo.functlon()

B.  

 foo.functlon(8,6,4)

C.  

 foo.functlon(b=10,a=9)

D.  

 foo.function(1;1,1,1)

36:

What will be the output of the following program on R Interpreter?

foo <- c(‘Testing‘.’while statement‘)

cnt <- 7

while [cnt<11.64)

[

printltoo)

cnt = count + 1

]

A.  

No output.

B.  

[1] 'Testlng’”while statement’

[1] "Testing‘'while statement'

[1] “Testing“while statement‘

[1] "Testing"‘while statement'

[1] "Testing”while statement‘ 

C.  

[1] 'Testing"'while statement'

[1] "Testing-while statement‘

[1] "Testing‘"‘while statement‘

D.  

Error in code executlon

37:

What will be the output ofthe following program on R interpreter?

foo <- C("Testing","loop")

count <— 5

repeat {


print(foo)


count <- count +1


if(count > 8.34)[


break

}

]


A.  

[1]"Testing""loop"

[1] "Testing""loop"

[1] "Testing""loop"

[1] "Testing""loop"

B.  

[1] "Testing""loop"

[1] "Testing""loop"

[1] "Testing""loop"

C.  

[1]"Testing""loop"

[1] "Testing"“loop"

D.   No output.

38:

What will be the output of the following R code?

car_colors <~ ci‘black',‘red','blue'.'blue'.'-.vltite'.‘metallic',‘red')

tactor_cars <~ Iactor(car_colors)

Print (factor_cars)

print(nlevels(factot_cats))

A.  

Levels: black blue metallic red white

[1]5  


B.  

 [1] black red blue blue White metallic red

Levels: black blue metallic red white

[1] 5   


C.  

 [l] 5

D.  

[1] black red blue blue white metallic red

Levels: black blue metallic red white


39:

The middle-most value in a data series is called the median. The median() function is used. in R. to calculate this value. Which of the following are the valid parameters in the median() function?

A.  

x, which is the input vector.

B.  

trlm, which Is used to drop values from both ends of the sorted vector.

C.  

na.rm, which is used to remove the missing values from the input vector.

40:

R has four in-built functlons to generate a normal distributlon. Which of the following functions takes the probability value and gives a number. whose cumulative value matches the probability value?

A.  

dnorm(x.mean. sd) 

B.  

 pnorm(x.mean. sd)  

C.  

qnorm(p.mean. sd)  

D.  

rnorm(n. mean, sd)

41:

in R language, in order to calculate the mean of a given set of values, the mean() function is used. Following is the basic syntax for the same:

mean(x, trim = O, na.rm = FALSE, ...)

Which of the following parameters is used to remove the missing values from the input vector?

A.  


B.  

trim

C.  

na.rm

42: In R programming. which of the following functions are included in the standard Strings Library?

A.   grep(pattern.x)

B.   match(x,table)

C.   merge(a,b)

D.   choose(n, k)

43:

R has four in-built functions to generate a normal distribution. Which of the following functions gives the height of the probability distribution at each point for a given mean and standard deviation?

A.  

dnorm(x, mean, sd)  

B.  

pnorm(x, mean, sd)

C.  

qnorm(p, mean, sd)

D.  

rnorm(n, mean, sd)

44: In R programming. which of the following are the valid values for the data type, Numeric?

A.   123L

B.   1 1 123

C.   999L

D.   2 999

45:

You have an Excel workbook with the name "data.xlsx". it has 3 sheets. Which of the following options will load the Excel workbook and read the second sheet?

A.  

data <- readodsxrdataxlsx', sheetlndex =1)

B.  

data <~ read.xlsx(‘data.xlsx', sheetlndex = 2) 

C.  

data <- read.xlsxt(data.xlsx'. sheetlndex = 3)


D.  

data <- read.xlsx('data.xlsx', sheetlndex = 0)

46: In R programming language, the library(x) function:

A.   will load specif‌ied data sets.

B.   will load add-on packages

C.   will load the datasets written with save.

D.   will start the HTML version of help.

47: To create a relationship model, the lrn() function is used in R. Which of the following are the valid parameters for this function?

A.   formula, which is a symbol. presenting the relation between x and y.

B.   data. which is the vector on which the formula will be applied.

C.   object, which is the formula that is already created using the lm0 function.

D.   newdata, which is the vector containing the new value for the predictor variable.

48:

What will be the output of the following program on R interpreter?

custom_cub_function <- function()

i

for(i in 1:4)

{

i

cubeoutput <- i"3

print[cubeoutput)

}

A.  

[1]1

[1]8

[1]27

[1]64 


B.  

 [1]3

[1]6

[1]9

[1]12   


C.  

[1]0.3333333

[1] 0.6666667

[1]1

[1]1.333333   


D.  

No output.  

49:

What will be the output of the following program on R interpreter?

foo <- 'red'

switch(foo, fruit = "apple", vegetable = “chilli‘. "Exception')

A.   red

B.  

apple 

C.  

chilli

D.  

Exception 

50:

What will be the output of the following program on R interpreter?

tempString <- "Hello.World!"

print (tempString)

tempString

A.  

[1] "Hello,World!"

[1] "Hello,World!"

B.  

[1] "Hello.World!"

C.  

Compilation error

D.  

Empty output