MCQ

PHP Questions and Answers – String – Part 1

This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “String”.
 

1. A sequence of characters that is processed as a unit is called ______

A Arrays

B Functions

C Methods

D Strings

D
A sequence of characters that is processed as a unit is called Strings.

 

 

2. How many approaches you can use to create a string in PHP?

A 1

B 2

C 3

D 4

B
There are two approaches you can use to create a string in PHP: single quote and double quote. Example:

$str1 = 'Welcome to StackHowTo!';
    
$str2 = "Welcome to StackHowTo!";

 

 

3. Which kind of string can treats special characters inside quotes?

A Single quote string

B Double quote string

C Both A and B

D None of the above

B
Double-quote strings in PHP is capable of treating special characters. Example:

echo("Welcome to StackHowTo!\n");
    
echo('Welcome to StackHowTo!\n');

Output:

Welcome to StackHowTo!
Welcome to StackHowTo!\n

 

 

4. Which is not a Built-in function for string in PHP?

A strpos()

B strreverse()

C str_replace()

D strlen()

B
strreverse() is not a Built-in function for string in PHP.

 

 

5. What’s the use of trim() function in PHP?

A to remove underscore

B to remove uppercase alphabet

C to remove lowercase alphabet

D to remove whitespaces

D
trim() function is used to remove whitespaces or strings from both sides of a string.

 

 

6. What is the result of the following PHP code?
<?php 
  
    echo strpos("Welcome to StackHowTo!", "to"), " \n";

?>

A 7

B 6

C 8

D 20

C
strpos() function used to locate the position of the first occurrence of a substring in a string.

 

 

7. If you want to compare two strings which of the following function can be used?

A strcspn()

B strspn()

C strcasecmp()

D strcmp()

A, B, C, D
All of the above functions can be used to compare two strings in PHP.

 

 

8. Which of the following function used to convert a string to uppercase?

A uppercase()

B str_uppercase()

C strtoupper()

D struppercase()

C
strtoupper() function used to convert a string to uppercase. Example:

<?php 
    echo strtoupper("Welcome to StackHowTo!");   
    // Output: WELCOME TO STACKHOWTO!
?>

 

 

9. Which of the following functions can be used to concatenate array elements to a single string?

A explode()

B implode()

C concat()

D concatenate()

B
implode() function is used to concatenate array elements to a single string. Example:

<?php 
    $arr = array('Welcome','to','StackHowTo!');
    echo implode(" ",$arr);
?>

Output:

Welcome to StackHowTo!

 

 

10. What is the result of the following PHP code?
<?php 
    $url = "[email protected]";
    echo ltrim(strstr($url, "@"),"@");
?>

A admin

B stackhowto.com

C [email protected]

D @stackhowto.com

B
The strstr() function returns the rest of a string beginning with the first occurrence of a predefined string.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *