MCQ

PHP Questions and Answers – Functions – Part 1

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

1. Why should we use functions?

A Easily maintained

B Reusability

C Easier error detection

D All of the above

D
Functions provide reusability of code, easier to detect error, and easier to maintain.

 

 

2. How many categories of functions are there in PHP?

A 1

B 2

C 3

D 4

B
PHP allows us using two major categories of functions: Built-in functions and User Defined Functions.

 

 

3. Which function is not a built-in function in PHP ?

A fopen()

B print()

C addNumber()

D gettype()

C
addNumber() is not a built-in function in PHP.

 

&
 

4. How to define a function in PHP?

A function {function body}

B datatype functionName(parameters) {function body}

C functionName(parameters) {function body}

D function functionName(parameters) {function body}

D
PHP provids us a way to create a user-defined functions. Any name ending with an open and closed parenthesis () is a function. The keyword function is always used to start a function.

 

 

5. Type Hinting was provided in which version of PHP?

A PHP 4

B PHP 5

C PHP 5.3

D PHP 6

B
With type hinting, we can specify the expected data type of an argument in a function declaration and as well as the return type.

 

 

6. A function always starts with the keyword _______

A def

B function

C fun

D None of the above

B
A function always starts with the keyword function.

 

 

7. A function’s name cannot begin with a _______.

A number

B underscore

C alphabet

D Both A and B

A
A function name cannot begin with a number. It can start with underscore or an alphabet.

 

 

8. A function name is not case-sensitive?

A False

B True

C Only user-defined function is case-sensitive

D None of the above

B
A function’s name is not case-sensitive.

 

 

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

    function calculate($cost, $tax="")
    {
        $total = $cost + ($cost * $tax);
        echo "$total"; 
    }

    calculate(5);	

?>

A 0

B 5

C 25

D Error

B
You can specify some arguments as optional by puting them at the end of the list and assigning them a default value of nothing.

 

 

10. What is the type of function call used in line 10 in the following code?
<?php
    function add($n1, $n2)	
    {
        $sum = $n1 + $n2;
    }

    $n1 = 2;
    $n2 = 4;

    add($n1, $n2);	
?>

A Call By Reference

B Call By Value

C Call By Address

D Default Argument Value

B
If we call a function by value, we actually transfer the values of the arguments which are kept or copied into the formal parameters of the function. As a result, the original values are untouched only the parameters inside the function changes.

 

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 *