Create New Post

PHP MCQs - 8

  1. What is the purpose of the array_search() function in PHP?

    • A) Searches for a value in an array and returns its key
    • B) Searches for a key in an array and returns its value
    • C) Reverses the order of elements in an array
    • D) Checks if a value exists in an array

    Answer: A) Searches for a value in an array and returns its key

  2. What is the output of the following code snippet?

    $str = "Hello"; echo strtoupper($str); 
    • A) hello
    • B) Hello
    • C) HELLO
    • D) Error

    Answer: C) HELLO

  3. Which of the following PHP functions is used to remove elements from the end of an array?

    • A) array_pop()
    • B) array_push()
    • C) array_shift()
    • D) array_unshift()

    Answer: A) array_pop()

  4. What is the purpose of the parse_str() function in PHP?

    • A) Parses a URL
    • B) Parses query string into variables
    • C) Parses JSON string into an array
    • D) Parses XML string into an object

    Answer: B) Parses query string into variables

  5. Which of the following is used to access the first character of a string in PHP?

    • A) $string[0]
    • B) $string(0)
    • C) substr($string, 0, 1)
    • D) substr($string, 1, 0)

    Answer: A) $string[0]

  6. What is the output of the following code snippet?

    $a = 5;
    $b = 10;
    echo "The sum of $a and $b is " . ($a + $b);
    
    • A) The sum of 5 and 10 is 15
    • B) The sum of $a and $b is 15
    • C) The sum of 5 and 10 is $a + $b
    • D) Error

    Answer: A) The sum of 5 and 10 is 15

  7. Which of the following PHP functions is used to sort an array in ascending order?

    • A) sort()
    • B) rsort()
    • C) asort()
    • D) ksort()

    Answer: A) sort()

  8. What is the purpose of the json_decode() function in PHP?

    • A) Converts a JSON string into an array
    • B) Converts an array into a JSON string
    • C) Encodes data using MIME base64
    • D) Parses a URL

    Answer: A) Converts a JSON string into an array

  9. What is the output of the following code snippet?

    
     

    $str = "Hello, World!";
    echo str_replace("World", "PHP", $str);

    • A) Hello, PHP!
    • B) PHP, World!
    • C) Hello, World!
    • D) Error

    Answer: A) Hello, PHP!

  10. Which of the following PHP functions is used to generate a random number?

    • A) random()
    • B) rand()
    • C) random_number()
    • D) generate_random()

    Answer: B) rand()

Comments

Leave a Reply

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

78301