R Programming - String MCQ Questions and Answers

In R, strings are used to store and manipulate text data. Knowing how to work with strings is essential for tasks like formatting text, extracting information, and processing data.

This quiz will help you review important string functions in R, such as concatenating, converting case, and finding string length. Each question provides a clear explanation to make the concepts easy to understand.

Let’s begin with these simple multiple-choice questions (MCQs) to improve your knowledge of handling strings in R.

1. Which function is used to concatenate strings in R?

a) concat()
b) paste()
c) join()
d) merge()

Answer:

b) paste()

Explanation:

The paste() function in R is used to concatenate two or more strings together.

2. How do you convert a string to uppercase in R?

a) toupper()
b) toUpperCase()
c) upper()
d) str_upper()

Answer:

a) toupper()

Explanation:

The toupper() function converts all characters of a string to uppercase in R.

3. Which function is used to extract a substring in R?

a) substr()
b) str_sub()
c) sub()
d) substring()

Answer:

a) substr()

Explanation:

The substr() function is used to extract a substring from a string, based on starting and ending positions.

4. How do you find the length of a string in R?

a) strlen()
b) length()
c) nchar()
d) str_len()

Answer:

c) nchar()

Explanation:

The nchar() function in R returns the number of characters in a string.

5. How do you replace a part of a string in R?

a) replace()
b) gsub()
c) str_replace()
d) substitute()

Answer:

b) gsub()

Explanation:

The gsub() function is used to replace all occurrences of a pattern in a string in R.

6. Which function is used to split a string in R?

a) split()
b) strsplit()
c) divide()
d) slice()

Answer:

b) strsplit()

Explanation:

The strsplit() function splits a string into substrings based on a specified delimiter.

7. How do you trim leading and trailing whitespaces in R?

a) strip()
b) trim()
c) trimws()
d) stripws()

Answer:

c) trimws()

Explanation:

The trimws() function removes leading and trailing whitespaces from a string in R.

8. How do you convert a string to lowercase in R?

a) tolower()
b) str_lower()
c) lowercase()
d) toLowerCase()

Answer:

a) tolower()

Explanation:

The tolower() function converts all characters of a string to lowercase in R.

9. Which function in R is used to search for a pattern in a string?

a) match()
b) search()
c) grep()
d) pattern()

Answer:

c) grep()

Explanation:

The grep() function is used to search for patterns in strings in R, returning matching elements.

10. What is the result of the expression substr("R Language", 3, 5)?

a) "Lang"
b) "Lan"
c) "Language"
d) "R L"

Answer:

b) "Lan"

Explanation:

The substr() function extracts characters from the 3rd to the 5th position, which gives "Lan".

11. How do you detect if a string starts with a specific substring in R?

a) str_starts()
b) startsWith()
c) start_string()
d) begins()

Answer:

b) startsWith()

Explanation:

The startsWith() function checks if a string starts with a given substring in R.

12. What is the output of the expression nchar("Data")?

a) 4
b) 3
c) 5
d) 2

Answer:

a) 4

Explanation:

The nchar() function returns the length of the string, which is 4 in this case.

13. How do you reverse the characters in a string in R?

a) reverse()
b) rev_string()
c) str_reverse()
d) rev()

Answer:

c) str_reverse()

Explanation:

The str_reverse() function reverses the order of characters in a string.

14. Which function in R converts a string to a raw vector?

a) rawToChar()
b) charToRaw()
c) str_raw()
d) raw_vector()

Answer:

b) charToRaw()

Explanation:

The charToRaw() function converts a character string into a raw vector in R.

15. How do you convert a numeric value to a string in R?

a) as.numeric()
b) as.character()
c) numToString()
d) str_numeric()

Answer:

b) as.character()

Explanation:

The as.character() function is used to convert a numeric value into a string in R.

These questions provide insight into working with strings in R. Mastering string manipulation techniques will allow you to handle and process text data efficiently. Keep practicing to strengthen your understanding of strings in R.

Comments