r programming banner

R Programming Multiple Choice Questions (MCQs) and Answers

Master R Programming with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of R Programming concepts. Begin your placement preparation journey now!

Q91

Q91 Why might the sd() function return an error when applied to a dataset in R?

A

The dataset contains non-numeric values

B

The dataset is empty

C

Both reasons mentioned

D

None of the above

Q92

Q92 What function is used to read CSV files into R?

A

read.csv()

B

load.csv()

C

import.csv()

D

fetch.csv()

Q93

Q93 Which function in R is used to write data frames to a CSV file?

A

write.csv()

B

export.csv()

C

save.csv()

D

output.csv()

Q94

Q94 What is the purpose of the file() function in R?

A

To delete files

B

To create a connection to a file for reading or writing

C

To list files

D

To copy files

Q95

Q95 In R, what does the read.table() function do?

A

Reads data stored in a table format

B

Creates a table from vector data

C

Visualizes data in table format

D

None of the above

Q96

Q96 What is the main difference between read.csv() and read.csv2() in R?

A

The former is used for reading large files, the latter for small files

B

The former uses comma as delimiter, the latter uses semicolon

C

The former supports UTF-8 encoding, the latter does not

D

There is no difference

Q97

Q97 How do you save an R data frame as a tab-separated values file?

A

write.table(df, "data.tsv", sep="\t")

B

save.table(df, "data.tsv", sep="\t")

C

write.csv(df, "data.tsv", sep="\t")

D

output.tsv(df, "data.tsv", sep="\t")

Q98

Q98 Which function would you use to read a JSON file into R?

A

read.json()

B

fromJSON()

C

json()

D

load.json()

Q99

Q99 What is the correct way to read an Excel file into R using the readxl package?

A

read_excel("data.xlsx")

B

load_excel("data.xlsx")

C

excel_read("data.xlsx")

D

get_excel("data.xlsx")

Q100

Q100 How can you connect to and read from a text file that is continuously being updated, in R?

A

file("log.txt", "r") and then use readLines()

B

open("log.txt", "r") and use readText()

C

connect("log.txt") and use getText()

D

link("log.txt", "read")

Q101

Q101 If write.csv() fails to save a data frame with error in R, what might be a common cause?

A

Incorrect file path

B

File is open in another program

C

Both reasons

D

None of the above

Q102

Q102 Why might read.table() return an error when reading a file that includes different data types?

A

The function expects uniform data types

B

Incorrect delimiter used

C

File contains improper formatting

D

Both the first and second options

Q103

Q103 What is an environment in R?

A

A collection of data types

B

A collection of packages

C

A collection of symbol-value pairs

D

A collection of functions

Q104

Q104 How does R determine the value of a variable when it is used in a function?

A

By checking global variables only

B

By searching the function's environment and then parent environments

C

By checking the global environment first

D

By looking only in the base environment

Q105

Q105 Which environment is at the top of the environment hierarchy in R?

A

Base environment

B

Global environment

C

Empty environment

D

Parent environment

Q106

Q106 What does the <<- operator do in R?

A

Assigns a value in the current environment

B

Assigns a value in the global environment

C

Assigns a value in the parent environment

D

None of the above

Q107

Q107 How do you create a new environment in R and assign a variable to it?

A

new.env(); e$a <- 1

B

env(); e$a <- 1

C

create.env(); e$a <- 1

D

new.environment(); e$a <- 1

Q108

Q108 How do you list all objects in the current environment?

A

ls()

B

objects()

C

list()

D

all_objects()

Q109

Q109 How can you access the parent environment of a given environment env in R?

A

parent.env(env)

B

env$parent

C

parent(environment)

D

get.parent(env)

Q110

Q110 If a variable in a function is not found in the current environment, what is R’s next step in finding it?

A

R throws an error

B

R searches in the global environment

C

R searches in the parent environment

D

R returns NULL

Q111

Q111 How can you avoid potential scoping issues in R functions when using global variables?

A

Always use <<- operator

B

Prefix global variables with global::

C

Avoid using global variables

D

Use local variables with the same name

Q112

Q112 What is the primary purpose of the try() function in R?

A

To attempt execution of code that might fail without stopping the entire script

B

To handle errors silently

C

To debug code

D

To improve code performance

Q113

Q113 What does the traceback() function do in R?

A

Shows the call stack leading to the error

B

Provides detailed error descriptions

C

Fixes the error automatically

D

Logs the error to a file

Q114

Q114 Which function in R can be used to provide custom error messages?

A

stop()

B

error()

C

alert()

D

throw()

Q115

Q115 How do you use the tryCatch() function to handle errors in R?

A

tryCatch(expr, error = function(e) { ... })

B

tryCatch(expr, silent = TRUE)

C

try(expr)

D

catch(expr)

Q116

Q116 How can you use debug() to step through a function named my_func in R?

A

debug(my_func); my_func()

B

step(my_func)

C

trace(my_func)

D

watch(my_func)

Q117

Q117 If you encounter an "object not found" error in R, what is a likely cause?

A

Misspelled variable name

B

Incorrect function call

C

Package not loaded

D

All of the above

Q118

Q118 How can you debug an R script that runs without error but produces incorrect results?

A

Use print statements to inspect variables

B

Use a step-through debugger

C

Check for logical errors

D

All of the above

Q119

Q119 When using tryCatch(), how can you capture and analyze the error object?

A

error = function(e) { print(e) }

B

catch = function(e) { analyze(e) }

C

errorHandler(e) { inspect(e) }

D

tryError(e) { log(e) }

Q120

Q120 Which function is used to get the current date and time in R?

A

now()

B

Sys.Date()

C

Sys.time()

D

current()

ad verticalad vertical
ad