R Strings MCQ

1. How do you concatenate two strings in R?

a) +
b) &
c) paste()
d) join()

Answer:

c) paste()

Explanation:

In R, the paste() function is used to concatenate or join two or more strings together.

2. Which function in R is used to determine the length of a string?

a) len()
b) strlen()
c) length()
d) str_length()

Answer:

b) strlen()

Explanation:

The strlen() function is used to find the length of a string in R, indicating the number of characters it contains.

3. How do you convert a string to upper case in R?

a) toUpper()
b) uppercase()
c) touppercase()
d) toupper()

Answer:

d) toupper()

Explanation:

The toupper() function is used to convert all characters of a string to upper case in R.

4. What is the output of substr("Hello, World!", 1, 5) in R?

a) Hello
b) Hello,
c) Hell
d) H

Answer:

a) Hello

Explanation:

The substr() function extracts a substring from a string. Here, it extracts characters from the 1st to the 5th position, which is "Hello".

5. How do you split a string on a specific character in R?

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

Answer:

b) strsplit()

Explanation:

The strsplit() function is used to split a string into substrings based on a specified delimiter.

6. What is the R function to trim leading and trailing whitespaces from a string?

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

Answer:

c) trimws()

Explanation:

The trimws() function in R is used to remove leading and trailing whitespaces from a string.

7. In R, how do you replace part of a string?

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

Answer:

c) gsub()

Explanation:

The gsub() function is used for pattern matching and replacement in strings in R.

8. Which function in R extracts a substring based on pattern matching?

a) substr()
b) str_extract()
c) grep()
d) regexpr()

Answer:

d) regexpr()

Explanation:

The regexpr() function in R is used to perform pattern matching and extract substrings based on regular expressions.

9. How is a character string defined in R?

a) With single quotes
b) With double quotes
c) Either single or double quotes
d) With backticks

Answer:

c) Either single or double quotes

Explanation:

In R, a character string can be defined using either single or double quotes.

10. What does the nchar() function do in R?

a) Finds the numerical value of characters
b) Counts the number of characters in a string
c) Repeats a string n times
d) Splits a string into n parts

Answer:

b) Counts the number of characters in a string

Explanation:

The nchar() function in R is used to count the number of characters in a string.

11. What is the result of the expression grepl("world", "Hello world") in R?

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

Answer:

a) TRUE

Explanation:

The grepl() function searches for a pattern in a string and returns TRUE if the pattern exists.

12. How do you format numbers as strings in R?

a) str_format()
b) sprintf()
c) format()
d) number_to_string()

Answer:

b) sprintf()

Explanation:

The sprintf() function in R is used for formatting numbers and other types of data as strings.

13. What is the output of the expression tolower("HELLO") in R?

a) HELLO
b) hello
c) HELLo
d) Error

Answer:

b) hello

Explanation:

The tolower() function converts all characters in a string to lower case.

14. How do you extract the first character of a string in R?

a) substr(str, 0, 1)
b) substr(str, 1, 1)
c) str[1]
d) first(str)

Answer:

b) substr(str, 1, 1)

Explanation:

The substr() function can be used to extract a specific substring, and using indices 1, 1 extracts the first character.

15. Which function is used to join elements of a vector into a single string in R?

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

Answer:

b) paste()

Explanation:

The paste() function can be used to concatenate elements of a vector into a single string, with an optional separator.

16. What does the function str_detect() do in R?

a) Detects the type of a string
b) Searches for a pattern in a string
c) Detects the encoding of a string
d) Measures the length of a string

Answer:

b) Searches for a pattern in a string

Explanation:

The str_detect() function in R is used for pattern detection within a string.

17. How do you reverse a string in R?

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

Answer:

b) str_reverse()

Explanation:

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

18. Which function in R checks if a string starts with a specific character or substring?

a) starts_with()
b) str_starts()
c) begins()
d) startswith()

Answer:

b) str_starts()

Explanation:

The str_starts() function in R checks if a string starts with a specified character or substring.

19. What is the output of the expression charToRaw("R") in R?

a) "R"
b) The ASCII value of "R"
c) A raw vector representation of "R"
d) Error

Answer:

c) A raw vector representation of "R"

Explanation:

The charToRaw() function in R converts a character string to a raw vector representing the ASCII values of the characters.

20. How do you find the position of the first occurrence of a substring in a string in R?

a) find()
b) strpos()
c) match()
d) regexpr()

Answer:

d) regexpr()

Explanation:

The regexpr() function in R is used to find the position of the first occurrence of a pattern (substring) in a string.

Leave a Comment

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

Scroll to Top