"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Friday, August 16, 2024

MCQ 7-String Manipulation and Basic Operations

 

String Manipulation and Basic Operations-1


1.     What will be the output of the following PHP code?

<?php

$str = "hello world";

$result = strtoupper($str);

echo $result;

?>

a) HELLO WORLD
b) hello world
c) Hello World
d) HELLO_WORLD

Answer: a) HELLO WORLD

2.     Which PHP function is used to find the length of a string?
a) strlen()
b) str_len()
c) length()
d) get_length()

Answer: a) strlen()

3.     What is the result of the following PHP code?

php

 

<?php

$str = "PHP is fun!";

$result = substr($str, 4, 2);

echo $result;

?>

a) is
b) is
c) fun
d) PHP

Answer: a) is

4.     Which function is used to replace all occurrences of a substring in a string?
a) str_replace()
b) replace()
c) str_substitute()
d) str_replac()

Answer: a) str_replace()

5.     What will be the output of the following PHP code if $str is "Hello"?

php

 

<?php

$str = "Hello";

$result = strtolower($str);

echo $result;

?>

a) hello
b) HELLO
c) Hello
d) hEllO

Answer: a) hello

6.     What function is used to concatenate two strings in PHP?
a) concat()
b) strcat()
c) . (dot operator)
d) join()

Answer: c) . (dot operator)

7.     What will be the result of the following PHP code?

php

 

<?php

$str = "   Trim me!   ";

$result = trim($str);

echo $result;

?>

a) Trim me!
b) Trim me!
c) Trim me!
d) Trim me!

Answer: c) Trim me!

8.     Which PHP function is used to find the position of the first occurrence of a substring in a string?
a) strpos()
b) indexOf()
c) find()
d) str_index()

Answer: a) strpos()

9.     What is the result of the following PHP code?

php

 

<?php

$str = "hello";

$result = str_repeat($str, 3);

echo $result;

?>

a) hellohellohello
b) hellohellohellohellohellohello
c) hello, hello, hello
d) hello hello hello

Answer: a) hellohellohello

10.Which function is used to split a string into an array by a delimiter?
a) explode()
b) split()
c) substr()
d) str_split()

Answer: a) explode()

11.What will be the output of the following PHP code?

php

 

<?php

$str = "world";

$result = "Hello, " . $str . "!";

echo $result;

?>

a) Hello, world!
b) Hello, world
c) Hello, , world!
d) Hello, !

Answer: a) Hello, world!

12.Which function is used to get a substring of a string starting from a specific position?
a) substr()
b) substr_start()
c) slice()
d) cut()

Answer: a) substr()

13.What will be the result of the following PHP code if $str is "Hello World"?

php

 

<?php

$str = "Hello World";

$result = strpos($str, "World");

echo $result;

?>

a) 6
b) 7
c) 11
d) Error

Answer: b) 6

14.What does the strlen() function return?
a) The length of a string
b) The first character of a string
c) The last character of a string
d) The number of words in a string

Answer: a) The length of a string

15.Which function is used to find the last occurrence of a substring in a string?
a) strrpos()
b) strpos_last()
c) find_last()
d) str_last()

Answer: a) strrpos()

String Manipulation and Basic Operations-2

1.     What will be the output of the following PHP code if $str is "PHP"?

 

 

<?php

$str = "PHP";

$result = str_pad($str, 10, "*");

echo $result;

?>

a) PHP*******
b) ***PHP****
c) PHP
d) PHP******

Answer: a) PHP*******

2.     Which PHP function is used to remove whitespace from both ends of a string?
a) trim()
b) strip_tags()
c) rtrim()
d) ltrim()

Answer: a) trim()

3.     What does the addslashes() function do?
a) Adds slashes to escape special characters
b) Removes slashes from a string
c) Adds spaces between words
d) Trims whitespace from the string

Answer: a) Adds slashes to escape special characters

4.     What will be the output of the following PHP code if $str is "Welcome to PHP"?

php

 

<?php

$str = "Welcome to PHP";

$result = str_replace("PHP", "Programming", $str);

echo $result;

?>

a) Welcome to PHP
b) Welcome to Programming
c) Welcome to PHP Programming
d) Welcome Programming

Answer: b) Welcome to Programming

5.     What is the result of the following PHP code?

php

 

<?php

$str = "hello world";

$result = ucfirst($str);

echo $result;

?>

a) Hello world
b) hello World
c) Hello World
d) HELLO WORLD

Answer: a) Hello world

6.     Which function is used to find the position of the last occurrence of a substring in a string?
a) strrpos()
b) str_last_pos()
c) find_last()
d) last_index_of()

Answer: a) strrpos()

7.     What will be the output of the following PHP code if $str is "2024"?

php

 

<?php

$str = "2024";

$result = intval($str);

echo $result;

?>

a) 2024
b) 2024.0
c) string
d) 0

Answer: a) 2024

8.     Which function is used to convert a string to an array of individual characters?
a) str_split()
b) explode()
c) split()
d) char_array()

Answer: a) str_split()

9.     What is the result of the following PHP code if $str is "HelloWorld"?

php

 

<?php

$str = "HelloWorld";

$result = substr($str, -5);

echo $result;

?>

a) World
b) Hello
c) lloWorld
d) World

Answer: a) World

10.Which function is used to get a substring from a string, starting from a specific position to the end?
a) substr()
b) str_slice()
c) str_cut()
d) cut_string()

Answer: a) substr()

11.What will be the result of the following PHP code if $str is "test123test"?

php

 

<?php

$str = "test123test";

$result = strrpos($str, "test");

echo $result;

?>

a) 0
b) 4
c) 8
d) -1

Answer: c) 8

12.What does the strtoupper() function do?
a) Converts a string to uppercase
b) Converts a string to lowercase
c) Capitalizes the first letter of each word
d) Converts a string to title case

Answer: a) Converts a string to uppercase

13.Which function would you use to remove all occurrences of a specific character from a string?
a) str_replace()
b) remove_chars()
c) str_remove()
d) delete()

Answer: a) str_replace()

14.What is the result of the following PHP code?

php

 

<?php

$str = "PHP is fun!";

$result = substr_count($str, "is");

echo $result;

?>

a) 1
b) 2
c) 0
d) 3

Answer: a) 1

15.What will be the output of the following PHP code if $str is "Hello"?

php

 

<?php

$str = "Hello";

$result = strrev($str);

echo $result;

?>

a) olleH
b) Hello
c) Heoll
d) Holle

Answer: a) olleH

String Manipulation and Basic Operations-3

1.     What will be the output of the following PHP code if $str is "HelloWorld"?

<?php

$str = "HelloWorld";

$result = substr($str, 0, 5);

echo $result;

?>

a) Hello
b) World
c) HelloWorld
d) HelloW

Answer: a) Hello

2.     Which function is used to convert all characters of a string to lowercase?
a) strtolower()
b) strtoupper()
c) lcfirst()
d) convert_lower()

Answer: a) strtolower()

3.     What does the strtr() function do?
a) Translates characters in a string
b) Trims whitespace from a string
c) Reverses a string
d) Converts a string to an array

Answer: a) Translates characters in a string

4.     What will be the result of the following PHP code?

php

 

<?php

$str = "hello world";

$result = ucwords($str);

echo $result;

?>

a) Hello world
b) Hello World
c) HELLO WORLD
d) hello World

Answer: b) Hello World

5.     Which PHP function is used to find and replace a substring within a string?
a) str_replace()
b) substr_replace()
c) str_find_replace()
d) replace_substr()

Answer: a) str_replace()

6.     What is the result of the following PHP code?

php

 

<?php

$str = "abc123";

$result = ctype_alpha($str);

echo $result;

?>

a) true
b) false
c) 1
d) 0

Answer: b) false

7.     Which function can be used to remove a specific character from a string?
a) str_replace()
b) remove_char()
c) strip()
d) str_remove()

Answer: a) str_replace()

8.     What will be the output of the following PHP code?

php

 

<?php

$str = "data.csv";

$result = pathinfo($str, PATHINFO_EXTENSION);

echo $result;

?>

a) data
b) csv
c) data.csv
d) pathinfo

Answer: b) csv

9.     Which function is used to find the length of a string in bytes?
a) mb_strlen()
b) strlen()
c) strlen_byte()
d) byte_length()

Answer: a) mb_strlen()

10.What will be the result of the following PHP code if $str is " spaced "?

php

 

<?php

$str = "   spaced   ";

$result = ltrim($str);

echo $result;

?>

a) spaced
b) spaced
c) spaced
d) spaced

Answer: a) spaced

11.What does the str_split() function do?
a) Splits a string into an array of characters
b) Joins an array of characters into a string
c) Finds a substring in a string
d) Reverses a string

Answer: a) Splits a string into an array of characters

12.What will be the output of the following PHP code if $str is "hello_world"?

php

 

<?php

$str = "hello_world";

$result = str_replace("_", " ", $str);

echo $result;

?>

a) hello world
b) hello_world
c) hello
d) world

Answer: a) hello world

13.Which PHP function is used to return the part of a string between two specified positions?
a) substr()
b) strcut()
c) slice()
d) get_substring()

Answer: a) substr()

14.What is the result of the following PHP code?

php

 

<?php

$str = "  extra spaces  ";

$result = rtrim($str);

echo $result;

?>

a) extra spaces
b) extra spaces
c) extra spaces
d) extra spaces

Answer: b) extra spaces

15.Which PHP function can be used to get the ASCII value of the first character of a string?
a) ord()
b) chr()
c) ascii()
d) get_ascii()

Answer: a) ord()

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template