String Handling - searching, replacing, formatting, string library functions - Question Bank

1. Which function returns the string with all characters converted to lowercase?
A) strtoupper()
B) ucwords()
C) strtolower()
D) lcfirst()
2. What is the primary function of `str_replace()`?
A) To find the position of a substring.
B) To replace all occurrences of a search string with a replacement string.
C) To split a string into an array.
D) To calculate the length of a string.
3. Which function is used to remove characters from the beginning of a string?
A) ltrim()
B) rtrim()
C) trim()
D) strip_left()
4. What does `str_ireplace()` do?
A) Performs a case-sensitive string replacement.
B) Performs a case-insensitive string replacement.
C) Replaces the first occurrence of a string.
D) Replaces only specific characters.
5. Which function is used to get the length of a string in bytes?
A) strlen()
B) mb_strlen()
C) sizeof()
D) byte_length()
6. What is the main difference between `strstr()` and `strchr()`?
A) `strstr()` searches for a substring, `strchr()` searches for a character.
B) `strstr()` is case-insensitive, `strchr()` is case-sensitive.
C) `strstr()` returns the string from the start, `strchr()` returns the string from the end.
D) They are synonyms and perform the same function.
7. Which function splits a string by another string?
A) explode()
B) str_split()
C) preg_split()
D) chunk_split()
8. What is the purpose of `str_pad()`?
A) To add characters to a string until it reaches a specified length.
B) To remove characters from a string.
C) To repeat a string.
D) To join two strings together.
9. Which function returns the string with the first character of the string capitalized?
A) ucwords()
B) strtoupper()
C) ucfirst()
D) capitalize_first()
10. What does `str_rot13()` do?
A) Reverses a string.
B) Applies the ROT13 cipher to a string.
C) Shuffles the characters of a string.
D) Converts a string to uppercase.
11. Which function returns the portion of a string starting from a specified offset up to a specified length?
A) substr()
B) strstr()
C) strchr()
D) strrchr()
12. The `substr_replace()` function can replace part of a string with another string. What is the default behavior if the length parameter is omitted?
A) It replaces until the end of the original string.
B) It replaces only the specified offset.
C) It throws an error.
D) It replaces the entire string.
13. Which function compares two strings using a 'natural order' algorithm?
A) strcmp()
B) strnatcmp()
C) strcasecmp()
D) strncmp()
14. What is the purpose of `strip_tags()`?
A) To remove HTML and PHP tags from a string.
B) To remove all whitespace from a string.
C) To remove special characters from a string.
D) To convert HTML entities to characters.
15. Which function returns the position of the last occurrence of a substring, ignoring case?
A) strrpos()
B) strripos()
C) strrchr()
D) strrstr()
16. What does `str_contains()` check for?
A) If a string contains a specific substring.
B) If a string contains only alphabetic characters.
C) If a string contains only numeric characters.
D) If a string contains a specific character at the beginning.
17. Which function returns the string representation of a variable?
A) strval()
B) string()
C) toString()
D) var_export()
18. What is the primary role of `sscanf()`?
A) To format a string according to a specified format.
B) To parse a string according to a specified format.
C) To search for a pattern within a string.
D) To replace parts of a string based on a pattern.
19. Which function is used to convert a string into an array by splitting it on lines?
A) preg_split('/\r\n|\n|\r/', $string)
B) explode('\n', $string)
C) str_getcsv($string, '\n')
D) split_lines($string)
20. What does `sha1()` generate?
A) A SHA-1 hash of a string.
B) An MD5 hash of a string.
C) A Base64 encoded string.
D) A ROT13 encoded string.
21. Which function is used to get a substring of a string, starting from a specified offset and with a specified length, ignoring case?
A) substr_compare()
B) strncasecmp()
C) strncmpi()
D) substr_case_compare()
22. What is the purpose of `str_repeat()`?
A) To repeat a string a specified number of times.
B) To repeat characters in a string.
C) To repeat a pattern within a string.
D) To repeat lines from a string.
23. Which function returns a string, obtained by inserting a string into another string at a specified position?
A) substr_replace()
B) str_insert()
C) string_overlay()
D) insert_string()
24. What does `str_word_count()` return by default?
A) An array of words found in the string.
B) The number of words in the string.
C) The first word of the string.
D) The length of the longest word.
25. Which function is used to parse a string into variables?
A) parse_str()
B) string_parse()
C) str_getcsv()
D) sscanf()
26. The `str_ireplace()` function performs a case-insensitive version of which function?
A) str_replace()
B) str_rot13()
C) str_shuffle()
D) strcasecmp()
27. Which function is used to find the position of the first occurrence of a substring, ignoring case?
A) strpos()
B) stripos()
C) stristr()
D) strstr()
28. What is the primary use of `str_shuffle()`?
A) To sort the characters of a string alphabetically.
B) To reverse the order of characters in a string.
C) To randomly shuffle the characters of a string.
D) To remove duplicate characters from a string.
29. Which function is used to get the length of a string in characters, supporting multi-byte encodings?
A) strlen()
B) mb_strlen()
C) str_len()
D) char_count()
30. What does `levenshtein()` calculate?
A) The similarity between two strings.
B) The number of characters that differ between two strings.
C) The Levenshtein distance between two strings.
D) The length of the longest common substring.
31. Which function returns the string with the first character of each word capitalized?
A) ucfirst()
B) strtoupper()
C) ucwords()
D) capitalize()
32. The `wordwrap()` function in PHP is used to:
A) Count the number of words in a string.
B) Wrap a string to a specified number of characters.
C) Convert a string to title case.
D) Remove duplicate words from a string.
33. Which function translates or replaces characters in a string?
A) strtr()
B) str_replace()
C) translate()
D) replace_chars()
34. What is the output of `strrev('hello')`?
A) 'hello'
B) 'olleh'
C) 'Holel'
D) An error
35. Which function compares two strings or parts of strings from a specified starting point?
A) strcmp()
B) strncmp()
C) substr_compare()
D) strnatcmp()
36. The `substr_count()` function returns:
A) The position of the substring.
B) The number of occurrences of a substring.
C) The length of the substring.
D) True if the substring exists, false otherwise.
37. Which function returns the last occurrence of a substring in a string?
A) strrpos()
B) strripos()
C) strrchr()
D) strstr()
38. What is the purpose of `implode()` in PHP?
A) To break a string into smaller parts.
B) To join array elements into a string with a glue string.
C) To count the number of characters in a string.
D) To find the last occurrence of a substring.
39. Which function removes whitespace from the beginning and end of a string?
A) ltrim()
B) rtrim()
C) trim()
D) strip_tags()
40. To check if a string starts with a specific substring, which function is most suitable?
A) str_starts_with()
B) substr_compare()
C) str_contains()
D) preg_match()
41. What does `md5()` function do in PHP?
A) Encrypts a string using MD5 algorithm.
B) Decrypts a string encrypted with MD5.
C) Generates an MD5 hash of a string.
D) Compares two strings for equality using MD5.
42. Which function pads a string to a certain length with another string?
A) str_pad()
B) str_repeat()
C) str_repeat_with()
D) pad_string()
43. What is the difference between `strpos()` and `stripos()` in PHP?
A) `strpos()` is case-sensitive, `stripos()` is case-insensitive.
B) `strpos()` returns the character, `stripos()` returns the position.
C) `strpos()` searches from the end, `stripos()` searches from the beginning.
D) There is no functional difference; they are synonyms.
44. Which function returns a part of a string, starting from a specified position?
A) strstr()
B) substr()
C) strchr()
D) strrchr()
45. The `str_split()` function in PHP divides a string into an array of:
A) Words
B) Sentences
C) Substrings of a specified length
D) Characters
46. Which PHP function converts a string to all uppercase letters?
A) ucfirst()
B) ucwords()
C) strtoupper()
D) strtolower()
47. What is the primary purpose of the `explode()` function in PHP?
A) To join an array into a string.
B) To split a string into an array by a delimiter.
C) To reverse a string.
D) To convert a string to lowercase.
48. Which function is used to replace all occurrences of a substring with another substring in PHP?
A) str_replace()
B) substr_replace()
C) preg_replace()
D) strtr()
49. What does the PHP function `strlen()` return?
A) The length of the string in bytes.
B) The length of the string in characters.
C) The number of words in the string.
D) The number of occurrences of a specific character.
50. Which PHP function is used to find the position of the first occurrence of a substring within a string?
A) strpos()
B) strstr()
C) strrpos()
D) str_replace()