R Variables MCQ

1. What is the correct way to assign a value to a variable in R?

a) variable = value
b) variable == value
c) variable <- value
d) variable -> value

Answer:

c) variable <- value

Explanation:

In R, the most common way to assign a value to a variable is using the '<-' operator. The '=' operator can also be used, but '<-' is preferred in R programming for clarity and tradition.

2. Which of these is a valid variable name in R?

a) .myVar
b) 3variable
c) my-Var
d) _myVar

Answer:

a) .myVar

Explanation:

Variable names in R can start with a period ('.') followed by a character, but they cannot start with a number, have dashes, or only consist of special characters.

3. How can you check the data type of a variable in R?

a) type(variable)
b) typeof(variable)
c) datatype(variable)
d) varType(variable)

Answer:

b) typeof(variable)

Explanation:

In R, 'typeof()' is used to determine the data type of a variable, such as integer, double, character, etc.

4. Which of the following is not a basic data type in R?

a) numeric
b) list
c) character
d) integer

Answer:

b) list

Explanation:

List is a data structure in R, not a basic data type. The basic data types include numeric, character, and integer.

5. How can you create a vector in R?

a) vector = c(1, 2, 3)
b) vector = [1, 2, 3]
c) vector = {1, 2, 3}
d) vector = <1, 2, 3>

Answer:

a) vector = c(1, 2, 3)

Explanation:

In R, vectors are created using the 'c()' function, which combines values into a vector.

6. What will be the output of the following code? x <- "5"; as.numeric(x)

a) 5
b) "5"
c) NULL
d) Error

Answer:

a) 5

Explanation:

The 'as.numeric()' function converts the character string "5" into the numeric value 5.

7. Which of the following is used to create a sequence of numbers in R?

a) seq()
b) range()
c) sequence()
d) numbers()

Answer:

a) seq()

Explanation:

The 'seq()' function is used in R to create a sequence of numbers with specified start, end, and step values.

8. What will be the result of the expression 'is.na(NA)' in R?

a) TRUE
b) FALSE
c) NA
d) Error

Answer:

a) TRUE

Explanation:

'is.na()' checks if a value is 'NA' (missing value) in R, and returns TRUE if it is.

9. How do you comment a single line in R script?

a) // This is a comment
b) # This is a comment
c) <!– This is a comment –>
d) /* This is a comment */

Answer:

b) # This is a comment

Explanation:

In R, a single line comment is initiated with the '#' symbol.

10. Which function is used to install packages in R?

a) library()
b) require()
c) install.packages()
d) load.packages()

Answer:

c) install.packages()

Explanation:

The 'install.packages()' function is used to install new packages in R, while 'library()' or 'require()' are used to load them into the current session.

11. What does the R function 'rm()' do?

a) Removes variables from memory
b) Removes missing values from a dataset
c) Resets the R environment
d) Removes files from the working directory

Answer:

a) Removes variables from memory

Explanation:

The 'rm()' function is used to remove specified objects from the memory (workspace) in R.

12. Which of these is the correct way to access the third element of a vector 'vec' in R?

a) vec[3]
b) vec(3)
c) vec<3>
d) vec{3}

Answer:

a) vec[3]

Explanation:

In R, elements of a vector are accessed using square brackets, with the index of the element inside the brackets.

13. How is a missing value represented in R?

a) NULL
b) NA
c) 0
d) ""

Answer:

b) NA

Explanation:

In R, 'NA' is used to represent missing or unavailable data.

14. Which function can be used to read a CSV file in R?

a) read.csv()
b) open.csv()
c) load.csv()
d) get.csv()

Answer:

a) read.csv()

Explanation:

The 'read.csv()' function is widely used for reading CSV (Comma Separated Values) files into R.

15. What is the outcome of the expression 'sqrt(-1)' in R?

a) -1
b) 0
c) NaN
d) Error

Answer:

c) NaN

Explanation:

In R, taking the square root of a negative number results in 'NaN', which stands for 'Not a Number'.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top