R Lists MCQ

1. What is a list in R?

a) A sequence of numbers
b) A collection of elements of the same type
c) A collection of elements of potentially different types
d) A key-value pair

Answer:

c) A collection of elements of potentially different types

Explanation:

A list in R is a collection that can hold elements of different types, including numbers, strings, vectors, and even other lists.

2. How do you create a list in R?

a) list()
b) c()
c) vector()
d) array()

Answer:

a) list()

Explanation:

The list() function is used to create a list in R.

3. How do you access the third element of a list named 'myList' in R?

a) myList[3]
b) myList(3)
c) myList[[3]]
d) myList{3}

Answer:

c) myList[[3]]

Explanation:

Double square brackets [[ ]] are used to access a single element of a list in R.

4. How can you add an element to an existing list in R?

a) append()
b) push()
c) c()
d) list()

Answer:

a) append()

Explanation:

The append() function is used to add elements to an existing list in R.

5. What does the lapply() function do in R?

a) Applies a function over a list and returns a list
b) Applies a function over a vector and returns a vector
c) Sorts a list
d) Converts a list to a vector

Answer:

a) Applies a function over a list and returns a list

Explanation:

The lapply() function applies a function to each element of a list and returns a list of the results.

6. How do you remove an element from a list in R?

a) delete()
b) remove()
c) list$element <- NULL
d) unset()

Answer:

c) list$element <- NULL

Explanation:

To remove an element from a list, you can set it to NULL using list$element <- NULL.

7. Which function checks if an object is a list in R?

a) is.list()
b) typeof()
c) is.object()
d) is.collection()

Answer:

a) is.list()

Explanation:

The is.list() function is used to check if an object is a list in R.

8. How do you concatenate two lists in R?

a) c()
b) append()
c) merge()
d) concat()

Answer:

a) c()

Explanation:

The c() function can be used to concatenate two or more lists in R.

9. What is the result of the expression length(list(1, 2, 3, 4)) in R?

a) 1
b) 4
c) 3
d) Error

Answer:

b) 4

Explanation:

The length() function returns the number of elements in a list, so length(list(1, 2, 3, 4)) is 4.

10. How do you create a named list in R?

a) list(name = "value")
b) list("name" = "value")
c) list[name] = "value"
d) list{"name" = "value"}

Answer:

a) list(name = "value")

Explanation:

A named list is created by specifying the name of each element in the list() function, as in list(name = "value").

11. How do you access a named element 'age' in a list named 'person' in R?

a) person["age"]
b) person$age
c) person(age)
d) Both a and b

Answer:

d) Both a and b

Explanation:

In R, a named element of a list can be accessed using either the $ operator (person$age) or the [ ] operator with the name as a string (person["age"]).

12. What does the sapply() function do in R?

a) Applies a function over a list and returns a vector or matrix
b) Sorts a list
c) Searches for an element in a list
d) Splits a list into sub-lists

Answer:

a) Applies a function over a list and returns a vector or matrix

Explanation:

The sapply() function in R applies a function to each element of a list and simplifies the result into a vector or matrix.

13. How do you update an existing element in a list in R?

a) list[element] <- new_value
b) update(list, element, new_value)
c) list$element <- new_value
d) Both a and c

Answer:

d) Both a and c

Explanation:

To update an existing element in a list, you can use the $ operator or the [ ] operator, such as list$element <- new_value or list[element] <- new_value.

14. How do you check if a list is empty in R?

a) isempty(list)
b) length(list) == 0
c) null(list)
d) empty(list)

Answer:

b) length(list) == 0

Explanation:

To check if a list is empty in R, you can use the condition length(list) == 0.

15. What is the outcome of combining a list and a vector using the c() function in R?

a) A combined list
b) A combined vector
c) Error
d) A data frame

Answer:

a) A combined list

Explanation:

When a list and a vector are combined using the c() function, the result is a list containing the elements of both the original list and the vector.

Leave a Comment

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

Scroll to Top