MCQs on String Manipulation | Lua

1. String Library Functions

  1. Which function is used to find the position of a substring in a string in Lua?
    • a) string.sub
    • b) string.find
    • c) string.match
    • d) string.split
  2. What does the string.sub function do in Lua?
    • a) It returns a substring from the given string
    • b) It replaces substrings in a string
    • c) It checks if a substring exists in a string
    • d) It converts a string to uppercase
  3. Which of the following Lua functions is used to replace occurrences of a substring in a string?
    • a) string.sub
    • b) string.match
    • c) string.find
    • d) string.gsub
  4. What is the default behavior of string.find in Lua if the substring is not found?
    • a) It returns nil
    • b) It throws an error
    • c) It returns false
    • d) It returns an empty string
  5. Which function in Lua can be used to split a string into multiple parts?
    • a) string.split
    • b) string.sub
    • c) string.match
    • d) Lua does not have a built-in string split function
  6. What does string.gsub return in Lua?
    • a) The number of replacements made
    • b) A new string with the replacements
    • c) A table of replaced strings
    • d) Both a and b
  7. Which Lua function can extract a substring from a given string based on start and end indices?
    • a) string.sub
    • b) string.find
    • c) string.match
    • d) string.split
  8. In Lua, what does string.gsub("hello", "e", "a") return?
    • a) "hallo"
    • b) "hello"
    • c) "halla"
    • d) "hella"
  9. Which function in Lua returns the first match of a pattern in a string?
    • a) string.match
    • b) string.find
    • c) string.sub
    • d) string.gsub
  10. Which Lua function is used to remove leading and trailing spaces from a string?
    • a) string.trim
    • b) string.strip
    • c) string.sub
    • d) Lua does not have a built-in trim function

2. Patterns and Basic String Matching

  1. Which character in Lua pattern matching is used to match any character?
    • a) .
    • b) *
    • c) +
    • d) ?
  2. In Lua, what does the pattern ^a match?
    • a) Any string that starts with “a”
    • b) Any string that ends with “a”
    • c) Any string containing “a”
    • d) Any string without “a”
  3. What is the purpose of the pattern + in Lua pattern matching?
    • a) It matches one or more occurrences of the previous character
    • b) It matches exactly one occurrence of the previous character
    • c) It matches zero or more occurrences of the previous character
    • d) It matches exactly two occurrences of the previous character
  4. What is the correct pattern to match a digit in Lua string patterns?
    • a) \d
    • b) \D
    • c) %d
    • d) %D
  5. How do you match a space character in Lua patterns?
    • a) \s
    • b) %s
    • c) \space
    • d) space
  6. In Lua, which function allows you to search for a pattern in a string?
    • a) string.match
    • b) string.find
    • c) string.gsub
    • d) All of the above
  7. Which pattern in Lua is used to match any letter (uppercase or lowercase)?
    • a) %a
    • b) %A
    • c) %l
    • d) %L
  8. Which of the following is a valid Lua pattern to match a word character (letters and digits)?
    • a) %w
    • b) %s
    • c) %d
    • d) \w
  9. How would you match a string that contains the word “Lua” using Lua patterns?
    • a) Lua
    • b) %Lua%
    • c) ^Lua$
    • d) %Lua
  10. What does the Lua pattern + signify in a regular expression?
    • a) Matches one or more occurrences of the previous element
    • b) Matches exactly one occurrence of the previous element
    • c) Matches zero or more occurrences of the previous element
    • d) None of the above

3. Escaping and Capturing

  1. In Lua, how do you escape special characters in patterns?
    • a) Using a backslash \
    • b) Using double quotes ""
    • c) Using single quotes ''
    • d) Special characters do not need escaping in Lua patterns
  2. What does the pattern %b[] match in Lua?
    • a) A balanced pair of square brackets
    • b) Any character inside square brackets
    • c) Any string that starts with [
    • d) Any string that ends with ]
  3. In Lua, how do you capture a part of a matched pattern for later use?
    • a) By enclosing the pattern in parentheses
    • b) By using the capture() function
    • c) By using curly braces {}
    • d) By using the sub() function
  4. Which of the following patterns captures a word in Lua?
    • a) %w+
    • b) %s+
    • c) \w+
    • d) [a-zA-Z]+
  5. What does the pattern () do in Lua pattern matching?
    • a) Captures the matched portion for later use
    • b) Matches parentheses in the string
    • c) Matches any character
    • d) Defines a non-capturing group
  6. In Lua, how can you match any string that does not contain digits?
    • a) [^%d]+
    • b) %d*
    • c) \D+
    • d) [^0-9]
  7. What does the string.match() function return if no match is found?
    • a) nil
    • b) false
    • c) An empty string
    • d) An error
  8. How do you capture the first word in a string using Lua pattern matching?
    • a) string.match(str, "%w+")
    • b) string.match(str, "%a+")
    • c) string.sub(str, "%w")
    • d) string.find(str, "%w")
  9. What does the Lua pattern \b match?
    • a) A word boundary
    • b) A digit
    • c) A non-word character
    • d) A backspace
  10. How do you escape a literal ( character in Lua patterns?
    • a) \(
    • b) \\(
    • c) \/(
    • d) %(

Answer Key

QnoAnswer (Option with the text)
1b) string.find
2a) It returns a substring from the given string
3d) string.gsub
4a) It returns nil
5d) Lua does not have a built-in string split function
6d) Both a and b
7a) string.sub
8a) "hallo"
9a) string.match
10d) Lua does not have a built-in trim function
11a) .
12a) Any string that starts with “a”
13a) It matches one or more occurrences of the previous character
14c) %d
15b) %s
16d) All of the above
17a) %a
18a) %w
19b) %Lua%
20a) Matches one or more occurrences of the previous element
21a) Using a backslash \
22a) A balanced pair of square brackets
23a) By enclosing the pattern in parentheses
24a) %w+
25a) Captures the matched portion for later use
26a) [^%d]+
27a) nil
28a) string.match(str, "%w+")
29a) A word boundary
30a) \(

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top