MCQs on Working with Strings | PHP Basics

Enhance your PHP string manipulation skills with this set of 30 multiple-choice questions covering crucial topics like string functions, regular expressions, and string formatting. Perfect for developers looking to sharpen their knowledge of PHP string handling and regular expression techniques.


Topic 1: String Manipulation Functions (10 Questions)

  1. What will strlen("Hello") return?
    A) 4
    B) 5
    C) 6
    D) “Hello”
  2. What is the result of substr("Hello World", 6, 5)?
    A) World
    B) World!
    C) Hello
    D) rld
  3. What does the strpos("PHP is fun", "fun") function return?
    A) 4
    B) 7
    C) 9
    D) false
  4. Which function is used to remove whitespace from the beginning and end of a string in PHP?
    A) trim()
    B) str_replace()
    C) ltrim()
    D) rtrim()
  5. What will strrev("abc") return?
    A) “abc”
    B) “cba”
    C) “ab”
    D) “abc!”
  6. Which function is used to find the length of a string in PHP?
    A) strlen()
    B) count()
    C) size()
    D) length()
  7. What does strtoupper("hello") return?
    A) hello
    B) HELLO
    C) hEllO
    D) Hello
  8. Which function is used to find a substring in a string in PHP?
    A) strstr()
    B) substr()
    C) strpos()
    D) search()
  9. What will substr("abcdefgh", 3, 4) output?
    A) abcd
    B) defg
    C) efgh
    D) abc
  10. How do you replace all occurrences of a substring in a string in PHP?
    A) substr()
    B) replace()
    C) str_replace()
    D) str_erase()

Topic 2: Regular Expressions in PHP (10 Questions)

  1. Which function is used to perform a regular expression match in PHP?
    A) preg_match()
    B) reg_match()
    C) preg_replace()
    D) match_regex()
  2. What is the result of preg_match("/hello/", "Hello world")?
    A) 1
    B) 0
    C) true
    D) false
  3. Which function is used to replace a pattern in a string using regular expressions?
    A) preg_match()
    B) preg_replace()
    C) replace()
    D) regex_replace()
  4. What will the regular expression /\d+/ match in the string “abc 123 xyz”?
    A) abc
    B) 123
    C) xyz
    D) 123 xyz
  5. What does the regular expression ^a match?
    A) The letter “a” anywhere in the string
    B) The letter “a” at the end of the string
    C) The letter “a” at the start of the string
    D) Any string containing “a”
  6. What will preg_match("/\d{3}/", "abc123xyz") return?
    A) 123
    B) true
    C) false
    D) abc
  7. How do you specify the case-insensitive flag in PHP regular expressions?
    A) /i
    B) /s
    C) /m
    D) /c
  8. Which function checks if a regular expression matches a given string?
    A) preg_match()
    B) preg_test()
    C) regex_test()
    D) match()
  9. What does preg_replace("/\s+/", "-", "hello world") return?
    A) hello world
    B) hello-world
    C) hello-world-
    D) hello—world
  10. Which of the following represents a regular expression for matching a number in PHP?
    A) \d
    B) \D
    C) \w
    D) \b

Topic 3: String Formatting (10 Questions)

  1. What will the sprintf("I have %d apples", 5) output?
    A) I have 5 apples
    B) I have %d apples
    C) I have 5 apple
    D) 5 apples
  2. Which function is used to output formatted strings in PHP?
    A) printf()
    B) format()
    C) echo_format()
    D) print_format()
  3. What does printf("Pi is approximately %.2f", 3.14159) display?
    A) 3.14159
    B) 3.14
    C) 3.1
    D) 3.1416
  4. Which placeholder is used for an integer in sprintf()?
    A) %d
    B) %s
    C) %f
    D) %i
  5. How would you format a floating-point number with two decimal places using printf()?
    A) %.2f
    B) %f.2
    C) %d.2
    D) %.2d
  6. What is the result of the following code? echo sprintf("The number is %02d", 5); A) 05
    B) 5
    C) 005
    D) 05.00
  7. Which of these functions is used to return a formatted string without printing it?
    A) echo()
    B) print()
    C) sprintf()
    D) printf()
  8. What will sprintf("The temperature is %.1f°C", 36.6) output?
    A) The temperature is 36.60°C
    B) The temperature is 36.6°C
    C) The temperature is 36°C
    D) The temperature is 36.66°C
  9. How do you specify a minimum width for a string in sprintf()?
    A) %5s
    B) %s5
    C) 5s
    D) s5
  10. What will printf("Name: %-10s Age: %d", "John", 25) output?
    A) Name: John Age: 25
    B) Name: John Age: 25
    C) Name: John Age: 25
    D) Name: John Age: 25

Answer Key

QnoAnswer (Option with the text)
1B) 5
2A) World
3B) 7
4A) trim()
5B) cba
6A) strlen()
7B) HELLO
8C) strpos()
9B) defg
10C) str_replace()
11A) preg_match()
12A) 1
13B) preg_replace()
14B) 123
15C) The letter “a” at the start of the string
16A) 123
17A) /i
18A) preg_match()
19B) hello-world
20A) \d
21A) I have 5 apples
22A) printf()
23B) 3.14
24A) %d
25A) %.2f
26A) 05
27C) sprintf()
28B) The temperature is 36.6°C
29A) %5s
30B) Name: John Age: 25

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