PHP Questions and Answers for focuses on “Strings and Regular Expressions – 2”.
Q 1. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular expressions.
A. 7
B. 8
C. 9
D. 10
Show Answer
Answer:-B. 8Explanation
The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(), preg_replace(), preg_replace_callback(), and preg_split().
Q 2. What will be the output of the following PHP code?
<?php
$foods = array(“pasta”, “steak”, “fish”, “potatoes”);
$food = preg_grep(“/^s/”, $foods);
print_r($food);
?>
A. Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
B. Array ( [3] => potatoes )
C. Array ( [1] => steak )
D. Array ( [0] => potatoes )
Show Answer
Answer:-C. Array ( [1] => steak )Explanation
This function is used to search an array for foods beginning with s.
Q 3. Say we have two compare two strings which of the following function/functions can you use?
i) strcmp()
ii) strcasecmp()
iii) strspn()
iv) strcspn()
A. i) and ii)
B. iii) and iv)
C. only i)
D. i), ii), iii) and iv)
Show Answer
Answer:-D. i), ii), iii) and iv)Explanation
All of the functions mentioned above can be used to compare strings in some or the other way.
Q 4. Which one of the following functions will convert a string to all uppercase?
A. strtoupper()
B. uppercase()
C. str_uppercase()
D. struppercase()
Show Answer
Answer:-A. strtoupper()Explanation
Its prototype follows string strtoupper(string str).
Q 5. What will be the output of the following PHP code?
<?php
$title = “O’malley wins the heavyweight championship!”;
echo ucwords($title);
?>
A. O’Malley Wins The Heavyweight Championship!
B. O’malley Wins The Heavyweight Championship!
C. O’Malley wins the heavyweight championship!
D. o’malley wins the heavyweight championship!
Show Answer
Answer:-A. O’Malley Wins The Heavyweight Championship!Explanation
The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).
Q 6. What will be the output of the following PHP code?
<?php
echo str_pad(“Salad”, 5).” is good.”;
?>
A. SaladSaladSaladSaladSalad is good
B. is good SaladSaladSaladSaladSalad
C. is good Salad
D. Salad is good
Show Answer
Answer:-D. Salad is goodExplanation
The str_pad() function pads a string with a specified number of characters.
Q 7. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
A. explode()
B. implode()
C. concat()
D. concatenate()
Show Answer
Answer:-B. implode()
Q 8. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
A. strlastpos()
B. strpos()
C. strlast()
D. strrpos()
Show Answer
Answer:-D. strrpos()
Q 9. What will be the output of the following PHP code?
<?php
$author = “nachiketh@example.com”;
$author = str_replace(“a”,”@”,$author);
echo “Contact the author of this article at $author.”;
?>
A. Contact the author of this article at nachiketh@ex@mple.com
B. Cont@ct the @uthor of this @rticle @t n@chiketh@ex@mple.com
C. Contact the author of this article at n@chiketh@ex@mple.com
D. Error
Show Answer
Answer:-C. Contact the author of this article at n@chiketh@ex@mple.comExplanation
The str_replace() function case sensitively replaces all instances of a string with another.
Q 10. What will be the output of the following PHP code?
<?php
$url = “nachiketh@example.com”;
echo ltrim(strstr($url, “@”),”@”);
?>
A. nachiketh@example.com
B. nachiketh
C. nachiketh@
D. example.com
Leave a Reply