MCQs on Class 10
PHP Strings and String Functions
1. What will be the output of the following code?
$text = "Hello, World!";
echo strlen($text);
- A) 10
- B) 12
- C) 13
- D) 14
Answer: C) 13
(Explanation: The strlen() function returns the length of the string, including spaces and punctuation. "Hello, World!" has 13 characters.)
2. Which function is used to find the position of the first occurrence of a substring in a string?
- A) strpos()
- B) strlength()
- C) strcount()
- D) str_find()
Answer: A) strpos()
(Explanation: strpos() finds the position of the first occurrence of a substring in a string.)
3. What does the following code output?
$text = "PHP is popular.";
echo strtoupper($text);
- A) php is popular.
- B) PHP IS POPULAR.
- C) Php is popular.
- D) PHP is POPULAR.
Answer: B) PHP IS POPULAR.
(Explanation: strtoupper() converts all characters in a string to uppercase.)
4. Which function extracts a part of a string in PHP?
- A) strcut()
- B) substring()
- C) substr()
- D) strslice()
Answer: C) substr()
(Explanation: substr() is used to extract a substring from a string based on a starting position and optional length.)
5. What will the following code print?
$text = "Learning PHP";
$newText = str_replace("PHP", "programming", $text);
echo $newText;
- A) Learning PHP
- B) Learning programming
- C) PHP programming
- D) Learning
Answer: B) Learning programming
(Explanation: str_replace() replaces occurrences of "PHP" with "programming" in the given string.)
6. Which of the following functions removes whitespace from the beginning and end of a string?
- A) trim()
- B) strip()
- C) trim_space()
- D) strtrim()
Answer: A) trim()
(Explanation: trim() removes whitespace from both the beginning and end of a string.)
7. What will be the output of this code?
$text = "Hello";
$text .= " World!";
echo $text;
- A) Hello
- B) World!
- C) Hello World!
- D) Hello, World!
Answer: C) Hello World!
(Explanation: The .= operator is used to concatenate and assign a string to the existing variable $text.)
8. Which function counts the number of times a substring appears in a string?
- A) strcount()
- B) str_find_count()
- C) strlen()
- D) substr_count()
Answer: D) substr_count()
(Explanation: substr_count() returns the number of times a substring appears in a string.)
9. Which function would you use to make the first letter of each word in a string uppercase?
- A) ucfirst()
- B) strupper()
- C) ucwords()
- D) strtoupper()
Answer: C) ucwords()
(Explanation: ucwords() converts the first character of each word to uppercase.)
10. What is the output of the following code?
$text = " PHP is fun! ";
echo trim($text);
- A) PHP is fun!
- B) " PHP is fun! "
- C) PHP
- D) is fun!
Answer: A) PHP is fun!
(Explanation: trim() removes whitespace from the beginning and end of the string, but not from the middle.)
MCQ Set on PHP Strings and String Functions
1. Which function calculates the length of a string in PHP?
- A) strsize()
- B) strlen()
- C) strlength()
- D) sizeof()
Answer: B) strlen()
2. What does strtoupper("hello") output?
- A) hello
- B) HELLO
- C) Hello
- D) HeLLo
Answer: B) HELLO
3. Which PHP function converts the first character of each word in a string to uppercase?
- A) ucfirst()
- B) ucwords()
- C) strtoupper()
- D) capitalize()
Answer: B) ucwords()
4. What is the output of the following code?
echo substr("abcdef", 1, 3);
- A) abc
- B) bcd
- C) def
- D) cde
Answer: B) bcd
5. Which function replaces occurrences of a substring within a string?
- A) replace()
- B) substr_replace()
- C) str_replace()
- D) str_swap()
Answer: C) str_replace()
6. What will trim(" PHP Rocks! ") output?
- A) PHP Rocks!
- B) " PHP Rocks!"
- C) "PHP Rocks"
- D) " Rocks! "
Answer: A) PHP Rocks!
7. Which function would you use to find the position of the first occurrence of a character in a string?
- A) strstr()
- B) strpos()
- C) strchr()
- D) strrpos()
Answer: B) strpos()
8. What does strtolower("PHP") output?
- A) php
- B) PHP
- C) Php
- D) phP
Answer: A) php
9. Which function removes whitespaces only from the beginning of a string?
- A) rtrim()
- B) ltrim()
- C) trim_start()
- D) strip()
Answer: B) ltrim()
10. What is the output of this code?
echo strrev("PHP");
- A) PHP
- B) HPP
- C) PPH
- D) HPP
Answer: D) HPP
11. What will strcmp("Hello", "hello") return?
- A) 1
- B) -1
- C) 0
- D) None of the above
Answer: A) 1
12. Which function would you use to find if a string contains a specific substring?
- A) strsearch()
- B) strpos()
- C) find()
- D) search_substring()
Answer: B) strpos()
13. Which function counts the number of occurrences of a substring in a string?
- A) strlen()
- B) str_count()
- C) substr_count()
- D) count_substr()
Answer: C) substr_count()
14. Which function is used to concatenate two strings in PHP?
- A) strcat()
- B) concat()
- C) implode()
- D) The . operator
Answer: D) The . operator
15. What will be the output of the following code?
$text = "Hello World";
echo str_replace("World", "PHP", $text);
- A) Hello PHP
- B) World PHP
- C) Hello World
- D) PHP World
Answer: A) Hello PHP
16. What will strpos("hello", "l") return?
- A) 2
- B) 3
- C) 4
- D) 1
Answer: A) 2
17. Which function converts the first letter of a string to uppercase?
- A) ucfirst()
- B) ucwords()
- C) strtoupper()
- D) strupper()
Answer: A) ucfirst()
18. Which function will replace all occurrences of the string "PHP" with "JavaScript" in the string "PHP is amazing. PHP is powerful."?
- A) strstr()
- B) str_replace()
- C) replace()
- D) str_replace_all()
Answer: B) str_replace()
19. The function strcasecmp() in PHP:
- A) Compares two strings case-sensitively.
- B) Compares two strings case-insensitively.
- C) Converts string to uppercase.
- D) Converts string to lowercase.
Answer: B) Compares two strings case-insensitively.
20. What does strlen("Hello PHP") return?
- A) 9
- B) 8
- C) 10
- D) 11
Answer: A) 9
21. Which function would convert "php is fun!" to "PHP IS FUN!"?
- A) strtoupper()
- B) strlower()
- C) ucfirst()
- D) ucwords()
Answer: A) strtoupper()
22. What will str_repeat("PHP ", 3) output?
- A) PHP PHP PHP
- B) PHP PHP
- C) PHP 3
- D) Error
Answer: A) PHP PHP PHP
23. Which of these functions would check if two strings are exactly the same?
- A) strcmp()
- B) strstr()
- C) strpos()
- D) strrpos()
Answer: A) strcmp()
24. What does ltrim(" PHP is great") return?
- A) "PHP is great"
- B) " PHP is great"
- C) "PHP"
- D) "is great"
Answer: A) "PHP is great"
25. Which function would you use to extract the last 3 characters of a string?
- A) strend()
- B) strcut()
- C) substr()
- D) strslice()
Answer: C) substr()
26. Which function can replace a part of a string, starting at a specified position, with another string?
- A) str_replace()
- B) substr_replace()
- C) strcut()
- D) str_swap()
Answer: B) substr_replace()
27. What does strpos("abcdef", "z") return if the substring "z" is not found?
- A) -1
- B) NULL
- C) 0
- D) False
Answer: D) False
28. How does strcasecmp() differ from strcmp()?
- A) It is case-sensitive.
- B) It is case-insensitive.
- C) It removes whitespaces.
- D) It only compares numeric values.
Answer: B) It is case-insensitive.
29. Which function would convert "123" to an integer?
- A) intval()
- B) strint()
- C) str_to_int()
- D) to_int()
Answer: A) intval()
30. Which of the following functions can find the last occurrence of a character in a string?
- A) strrchr()
- B) strstr()
- C) strrpos()
- D) strpos()
Answer: C) strrpos()
31. Which function finds a substring starting from the end of the string?
- A) strstr()
- B) strrchr()
- C) strpos()
- D) strchr()
Answer: B) strrchr()
32. What will strlen(" ") return?
- A) 0
- B) 1
- C) 2
- D) False
Answer: B) 1
33. Which of these functions returns an array of words in a string?
- A) split()
- B) str_split()
- C) explode()
- D) implode()
Answer: C) explode()
34. What will str_pad("PHP", 7, "-") output?
- A) PHP---
- B) ---PHP
- C) PHP-
- D) -PHP--
Answer: A) PHP---
35. How does trim(" PHP ") modify the string?
- A) Removes whitespace only from the start.
- B) Removes whitespace from both ends.
- C) Removes all spaces in the string.
- D) Removes whitespace only from the end.
Answer: B) Removes whitespace from both ends.
36. What does str_repeat("Hi", 4) return?
- A) Hi
- B) HiHiHiHi
- C) Hi Hi Hi Hi
- D) Error
Answer: B) HiHiHiHi
37. What is the result of strpos("hello world", "world")?
- A) 4
- B) 5
- C) 6
- D) 7
Answer: B) 6
38. Which function finds the length of a multibyte string accurately?
- A) strlen()
- B) mb_strlen()
- C) str_multibyte_length()
- D) length()
Answer: B) mb_strlen()
39. Which function converts all characters in a string to lowercase?
- A) strtolower()
- B) strupper()
- C) to_lower()
- D) lower()
Answer: A) strtolower()
40. What will ucwords("php is fun") output?
- A) PHP is Fun
- B) Php Is Fun
- C) php Is Fun
- D) Php is fun
Answer: B) Php Is Fun
41. Which function removes only the newline characters from the end of a string?
- A) rtrim()
- B) chop()
- C) trim()
- D) endtrim()
Answer: B) chop()
42. Which function returns a portion of a string starting from a specified position to the end?
- A) substr()
- B) str_part()
- C) strsect()
- D) sub_string()
Answer: A) substr()
43. How do you count the number of words in a string in PHP?
- A) wordcount()
- B) count_words()
- C) str_word_count()
- D) str_word()
Answer: C) str_word_count()
44. What will strstr("hello@example.com", "@") return?
- A) hello
- B) example.com
- C) @example.com
- D) .com
Answer: C) @example.com
45. Which function checks if a string contains only numeric characters?
- A) is_int()
- B) is_numeric()
- C) is_digit()
- D) num_check()
Answer: B) is_numeric()
46. What is the output of strcmp("apple", "banana")?
- A) Positive integer
- B) Negative integer
- C) 0
- D) None of the above
Answer: B) Negative integer
47. Which function reverses a string?
- A) str_reverse()
- B) strflip()
- C) reverse()
- D) strrev()
Answer: D) strrev()
48. What does substr("hello world", -5) return?
- A) world
- B) hello
- C) d
- D) Error
Answer: A) world
49. What will strlen("PHP ") return?
- A) 4
- B) 3
- C) 5
- D) 6
Answer: C) 5
50. Which function converts all characters in a string to uppercase?
- A) strtoupper()
- B) upper()
- C) strupper()
- D) capitalize()
Answer: A) strtoupper()
51. Which function converts the first character of a string to uppercase?
- A) str_upper()
- B) ucfirst()
- C) ucwords()
- D) capitalize()
Answer: B) ucfirst()
52. What will strpos("abcdef", "e") return?
- A) 5
- B) 4
- C) 3
- D) 2
Answer: B) 4
53. What does str_pad("PHP", 6, "*") output?
- A) PHP***
- B) ***PHP
- C) **PHP
- D) Error
Answer: A) PHP***
54. What is the output of substr("abcdef", 0, -1)?
- A) abcde
- B) def
- C) abc
- D) Error
Answer: A) abcde
55. Which function trims whitespace from the right side of a string?
- A) ltrim()
- B) rtrim()
- C) trim()
- D) end_trim()
Answer: B) rtrim()
56. Which function breaks a string into an array using a delimiter?
- A) explode()
- B) str_split()
- C) split()
- D) implode()
Answer: A) explode()
57. Which function calculates the MD5 hash of a string?
- A) hash()
- B) md5()
- C) sha1()
- D) str_hash()
Answer: B) md5()
58. What is the output of strrev("PHP")?
- A) PHP
- B) HPP
- C) HTP
- D) PHPH
Answer: A) HPP
59. Which function formats a string by replacing placeholders with variable values?
- A) sprintf()
- B) str_format()
- C) format()
- D) str_replace()
Answer: A) sprintf()
60. What does strtoupper("php code") return?
- A) php code
- B) PHP CODE
- C) Php Code
- D) php Code
Answer: B) PHP CODE
61. Which of the following can remove all characters after a specified length?
- A) str_clip()
- B) chop()
- C) truncate()
- D) substr()
Answer: D) substr()
62. What does ucfirst("example") return?
- A) example
- B) Example
- C) EXAMPLE
- D) eXAMPLE
Answer: B) Example
63. How do you compare two strings without case sensitivity?
- A) strcmp()
- B) strcasecmp()
- C) strcmpi()
- D) str_match()
Answer: B) strcasecmp()
64. Which function calculates the SHA-1 hash of a string?
- A) hash()
- B) md5()
- C) sha1()
- D) str_hash()
Answer: C) sha1()
65. What will strlen(" ") return?
- A) 0
- B) 1
- C) 2
- D) False
Answer: B) 1
66. Which function escapes HTML entities?
- A) htmlentities()
- B) html_escape()
- C) escape()
- D) to_html()
Answer: A) htmlentities()
67. Which function repeats a string?
- A) strcopy()
- B) str_repeat()
- C) str_replicate()
- D) str_loop()
Answer: B) str_repeat()
68. What is the purpose of str_shuffle()?
- A) Sorts characters in alphabetical order
- B) Reverses the string
- C) Randomly shuffles characters
- D) Removes spaces from the string
Answer: C) Randomly shuffles characters
69. How does strtolower() handle uppercase letters?
- A) Converts them to lowercase
- B) Removes them
- C) Reverses them
- D) Leaves them unchanged
Answer: A) Converts them to lowercase
70. What does strlen("php developer") return?
- A) 11
- B) 12
- C) 13
- D) 14
Answer: B) 12
71. Which function splits a string into chunks of a specified length?
- A) str_split()
- B) str_chunk()
- C) str_slice()
- D) explode()
Answer: A) str_split()
72. Which function finds the last position of a substring?
- A) strchr()
- B) strrchr()
- C) strrpos()
- D) strpos()
Answer: C) strrpos()
73. What will strlen(" PHP ") return?
- A) 3
- B) 5
- C) 6
- D) 4
Answer: B) 5
74. Which function generates a string from an array of words?
- A) split()
- B) implode()
- C) merge()
- D) str_join()
Answer: B) implode()
75. What will strtolower("PHP Language") return?
- A) Php Language
- B) php language
- C) PHP Language
- D) php Language
Answer: B) php language
76. Which function removes the whitespace from the left side of a string?
- A) trim()
- B) rtrim()
- C) ltrim()
- D) whitespace()
Answer: C) ltrim()
77. Which function can replace the first occurrence of a string?
- A) replace()
- B) str_replace_once()
- C) preg_replace()
- D) replace_once()
Answer: C) preg_replace()
78. Which function counts the occurrences of a substring?
- A) strcount()
- B) count_substr()
- C) substr_count()
- D) find_count()
Answer: C) substr_count()
79. How do you remove whitespace only from the right side of a string?
- A) rtrim()
- B) ltrim()
- C) trim()
- D) chop()
Answer: A) rtrim()
80. What does str_repeat("abc", 3) return?
- A) abcabcabc
- B) abc3
- C) abcabc
- D) abc abc abc
Answer: A) abcabcabc