(PHP) Hyper text Preprocessor.
Click here to Download this file
Multiple Choice Questios & Answers:- 

1.This set of PHP multiple choice questions and answers focuses on PHP basics. It will be useful for anyone learning PHP Basics and Fundamentals.
1. What does PHP stand for?
i) Personal Home Page
ii) Hypertext Preprocessor
iii) Pretext Hypertext Processor
iv) Preprocessor Home Page
a) Both i) and iii)
b) Both ii) and iv)
c) Only ii)
d) Both i) and ii)

Answer: d

2.Explanation:PHP previously stood for Personal Home Page now stands for Hypertext Preprocessor.

2. PHP files have a default file extension of..
a) .html
b) .xml
c) .php
d) .ph

Answer: c

3.Explanation:None.

3. A PHP script should start with ___ and end with ___:
a) < php >
b) < ? php ?>
c) <? ?>
d) <?php ?>

Answer: c or d

Explanation:Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them.

4. Which of the following is/are a PHP code editor?
i) Notepad
ii) Notepad++
iii) Adobe Dreamweaver
iv) PDT
a) Only iv)
b) All of the mentioned.
c) i), ii) and iii)
d) Only iii)

Answer: b

Explanation:Any of the above editors can be used to type php code and run it.

5. Which of the following must be installed on your computer so as to run PHP script?
i) Adobe Dreamweaver
ii) PHP
iii) Apache
iv) IIS
a) All of the mentioned.
b) Only ii)
c) ii) and iii)
d) ii), iii) and iv)

Answer: d

Explanation:To run PHP code you need to have PHP and a web server, both IIS and Apache are web servers.You can choose either one according to your platform.

6. Which version of PHP introduced Try/catch Exception?
a)PHP 4
b)PHP 5
c)PHP 5.3
d)PHP 6

Answer: b

Explanation: Version 5 added support for Exception Handling.

7. We can use ___ to comment a single line?
i) /?
ii) //
iii) #
iv) /* */
a) Only ii)
b) i), iii) and iv)
c) ii), iii) and iv)
d) Both ii) and iv)

Answer: c

Explanation: /* */ can also be use to comment just a single line although it is used for paragraphs. // and # are used only for single line comment.

8. Which of the following php statement/statements will store 111 in variable num?
i) int $num = 111;
ii) int mum = 111;
iii) $num = 111;
iv) 111 = $num;
a) Both i) and ii)
b) All of the mentioned.
c) Only iii)
d) Only i)

Answer: c

Explanation:You need not specify the datatype in php.

9. What will be the output of the following php code?

    <?php
    $num  = 1;
    $num1 = 2;
    print $num . "+". $num1;
    ?>
a) 3
b) 1+2
c) 1.+.2
d) Error

Answer: b

Explanation: .(dot) is used to combine two parts of the statement. Example ( $num . “Hello World” ) will output 1Hello World.

10. What will be the output of the following php code?

    <?php
    $num  = "1";
    $num1 = "2";
    print $num+$num1;
    ?>
a) 3
b) 1+2
C) Error
d) 12

Answer: a

Explanation:The numbers inside the double quotes are considered as integers and not string, therefore the value 3 is printed and not 1+2.

11. Which of following variables can be assigned a value to it?
i) $3hello
ii) $_hello
iii) $this
iv) $This
a) All of the mentioned
b) Only ii)
c) ii), iii) and iv)
d) ii) and iv)

Answer: d

Explanation:A variable can’t start with a number. Also $this is a special variable that can’t be assigned, but $This can be assigned.

12. What will be the output of the following code?

    <?php 
    $foo = 'Bob';              
    $bar = &$foo;              
    $bar = "My name is $bar";  
    echo $bar;
    echo $foo;
    ?>
a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob

Answer: c

Explanation:The $bar = &$foo; line will reference $foo via $bar.

13. Which of the following PHP statements will output Hello World on the screen?
i) echo (“Hello World”);
ii) print (“Hello World”);
iii) printf (“Hello World”);
iv) sprintf (“Hello World”);
a) i) and ii)
b) i), ii) and iii)
c) All of the mentioned
d) i), ii) and iv)

Answer: b

Explanation:echo(), print() and printf() all three can be used to output a statement onto the screen. The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser.

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

    <?php
    $color = "maroon";
    $var = $color[2];
    echo "$var";
    ?>
a) a
b) Error
c) $var
d) r

Answer: d

Explanation:PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via array offset notation.

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

    <?php
    $score = 1234;
    $scoreboard = (array) $score;
    echo $scoreboard[0];
    ?>
a) 1
b) Error
c) 1234
d) 2

Answer: c

Explanation:The (array) is a cast operator which is used for converting values from other data types to array.

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

    <?php
    $total = "25 students";
    $more = 10;
    $total = $total + $more;
    echo "$total";
    ?>
a) Error
b) 35 students
c) 35
d) 25 students

Answer: c

Explanation:The integer value at the beginning of the original $total string is used in the calculation. However if it begins with anything but a numerical value, the value will be 0.

17. Which of the below statements is equivalent to $add += $add ?
a) $add = $add
b) $add = $add +$add
C) $add = $add + 1
d) $add = $add + $add + 1

Answer: b

Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction,multiplication,division etc.

18. Which statement will output $x on the screen?
a) echo “\$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;

Answer: a

Explanation:A backslash is used so that the dollar sign is treated as a normal string character rather than prompt PHP to treat $x as a variable. The backslash used in this manner is known as escape character.

19. What will be the output of the following code?

    <?php
    function track() {
    static $count = 0;
    $count++;
    echo $count;
    }
    track();
    track();
    track();
    ?>
a) 123
b) 111
c) 000
d) 011

Answer: a

Explanation:Because $count is static, it retains its previous value each time the function is executed.

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

    <?php
    $a = "clue";
    $a .= "get";
    echo "$a";
    ?>
a) get
b) true
c) false
d) clueget

Answer: d

Explanation: .= is a concatenation-assignment. $a equals its current value concatenated with “get”.

21. Which one of the following is the right way of defining a function in PHP?
a) function { function body }
b) data type functionName(parameters) { function body }
c) functionName(parameters) { function body }
d) function fumctionName(parameters) { function body }

Answer: d

22. Type Hinting was introduced in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6

Answer: b

23. What will happen in this function call?

   <?php
    function calc($price, $tax) 
    {
        $total = $price + $tax;
    }
    $pricetag = 15;
    $taxtag = 3;
    calc($pricetag, $taxtag);   
    ?>
a) Call By Value
b) Call By Reference
c) Default Argument Value
d) Type Hinting

Answer: a

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

    <?php
    function calc($price, $tax="")
    {
        $total = $price + ($price * $tax);
        echo "$total"; 
    }
    calc(42);   
    ?>
a) Error
b) 0
c) 42
d) 84

Answer: c

25. Which of the following are valid function names?
i) function()
ii) €()
iii) .function()
iv) $function()
a) Only ii)
b) None of the mentioned.
c) All of the mentioned.
d) iii) and iv)

Answer: a

Explanation:Except a) others are invalid names. According to the specified regular expression ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*), a function name like this one is valid.

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

    <?php
    function a()
    {
        function b()
        {
            echo 'I am b';
        }
        echo 'I am a';
    }
    a();
    a();
    ?>
a) I am b
b) I am bI am a
c) Error
d) I am a Error

Answer: d

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

   <?php
    function a()  
    {
        function b()
        {
            echo 'I am b';
        }
        echo 'I am a';
    }
    b();
    a();
    ?>
a) I am b
b) I am bI am a
c) Error.
d) I am a Error

Answer: c

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

    <?php
    $op2 = "blabla";
    function foo($op1)
    {
        echo $op1;
        echo $op2;
    }
    foo("hello");
    ?>
a) helloblabla
b) Error
c) hello
d) helloblablablabla

Answer: c

Explanation: If u want to put some variables in function that was not passed by it, you must use “global”. Inside the function type global $op2.

29. A function in PHP which starts with __ (double underscore) is know as..
a) Magic Function
b) Inbuilt Function
c) Default Function
d) User Defined Function

Answer: a

Explanation:PHP functions that start with a double underscore – a “__” – are called magic functions in PHP. They are functions that are always defined inside classes, and are not stand-alone functions.

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

    <?php
    function foo($msg)
    {
        echo "$msg";
    }
    $var1 = "foo";
    $var1("will this work");
    ?>
a) Error.
b) $msg
c) 0
d) will this work

Answer: d

31. PHP’s numerically indexed array begin with position __.
a) 1
b) 2
c) 0
d) -1

Answer: c

32. Which of the following are correct ways of creating an array?
i) state[0] = “karnataka”;
ii) $state[] = array(“karnataka”);
iii) $state[0] = “karnataka”;
iv) $state = array(“karnataka”);
a) iii) and iv)
b) ii) and iii)
c) Only i)
d) ii), iii) and iv)

Answer: a

33. What will be the output of the following php code?

    <?php
    $states = array("karnataka" => array
    ( "population" => "11,35,000", "captial" => "Bangalore"),
    "Tamil Nadu" => array( "population" => "17,90,000",
    "captial" => "Chennai") );
    echo $states["karnataka"]["population"];
    ?>
a) karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) karnataka population

Answer: b

34. Which function will return true if a variable is an array or false if it is not?
a) this_array()
b) is_array()
c) do_array()
d) in_array()

Answer: b

35. Which in-built function will add a value to the end of an array?
a) array_unshift()
b) into_array()
c) inend_array()
d) array_push()

Answer: d

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

    <?php
    $state = array ("Karnataka", "Goa", "Tamil Nadu",
    "Andhra Pradesh");
    echo (array_search ("Tamil Nadu", $state) );
    ?>
a) True
b) 1
c) False
d) 2

Answer: d

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

    <?php
    $fruits = array ("apple", "orange", "banana");
    echo (next($fruits));       
    echo (next($fruits));
    ?>
a) orangebanana
b) appleorange
c) orangeorange
d) appleapple

Answer: a

38. Which function can be used to move the pointer to the previous array position?
a) last()
b) before()
c) prev()
d) previous()

Answer: c

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

    <?php
    $fruits = array ("apple", "orange", array ("pear", "mango"),
    "banana");
    echo (count($fruits, 1));
    ?>
a)3
b)4
c)5
d)6

Answer: d

40. Which function returns an array consisting of associative key/value pairs?
a) count()
b) array_count()
c) array_count_values()
d) count_values()
View Answer

Answer: c

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

    <?php
    echo (checkdate(4,31,2010) ? 'Valid' : 'Invalid');
    ?>
a) TRUE
b) FALSE
c) Valid
d) Invalid

Answer: d

Explanation:April has 30 days and the above date is 31 therefore Invalid is returned.

42. The date() function returns ___ representation of the current date and/or time.
a) Integer
b) String
c) Boolean
d) Float
View Answer

Answer: b

43. Which one of the following format parameter can be used to identify timezone?
a) T
b) N
c) E
d) I

Answer: c

44. If the format is F then which one of the following will be returned?
a) Complete text representation of month
b) Day of month, with leading zero
c) Daylight saving time
d) Day of month, without zeros

Answer: a

45. What will be the output of the following code? If say date is 22/06/2013.

    <?php
    echo "Today is ".date("F d, Y")
    ?>
a) Today is 22 June, 2013
b) Today is 22-06-2013
c) Today is 06-22-2013
d) Today is June 22, 2013

Answer: d

46. Which one of the following function is useful for producing a timestamp based on a given date and time.
a) time()
b) mktime()
c) mrtime()
d) mtime()

Answer: b

47. Which function displays the web page’s most recent modification date?
a) lastmod()
b) getlastmod()
c) last_mod()
d) get_last_mod()

Answer: b

48. What will be the output of the following PHP code? If say date is 22/06/2013.

    <?php
    printf( date("t") )
    ?>
a) 30
b) 22
c) JUNE
d) 2013

Answer: a

49. Say you want to calculate the date 45 days from the present date which one of the following statement will you use?
a) totime(“+45”)
b) totime(“+45 days”)
c) strtotime(“+45 days”)
d) strtotime(“-45 days”)

Answer: c

50. To create an object and set the date to JUNE 22, 2013, which one of the following statement should be executed?
a) $date = Date(“22 JUNE 2013”)
b) $date = new Date(“JUNE 22 2013”)
c) $date = DateTime(“22 JUNE 2013”)
d) $date = new DateTime(“22 JUNE 2013”)

Answer: d

51. Which one of the following statements should be used to disable just the fopen(), and file() functions?
a) disable_functions = fopen(),file()
b) disable_functions = fopen,file
c) functions_disable = fopen(),file()
d) functions_disable = fopen,file

Answer: b

52. Which one of the following statements should be used to disable the use of two classes administrator and janitor?
a) disable_classes = “administrator, janitor”
b) disable_classes = class administrator, class janitor
c) disable_classes = class “administrator”, class “janitor”
d) disable_class = class “administrator”, class “janitor”

Answer: a

53. What is the default value of max_execution_time directive? This directive specifies how many seconds a script can execute before being terminated.
a) 10
b) 20
c) 30
d) 40

Answer: c

54. The memory_limit is only applicable if ___ is enabled when you configure PHP. Fill in the blank.
a) –enable-limit
b) -enable-memory-limit
c) –enable-memory-limit
d) -memory-limit

Answer: c

55. Suppose all web material is located within the directory /home/www. To prevent users from viewing and manipulating files such as /etc/password, which one of the following statements should you use?
a) open_dir = “/home/www/”
b) open_dir = /home/www/
c) open_basedir = /home/www/
d) open_basedir = “/home/www/”

Answer: d

56. Which Apache directive outputs Apache’s server version, server name, port and compile-in modules?
a) ServerSignature
b) ServerName
c) ServerDetails
d) ServerInfo

Answer: a

57. Which directive determines which degree of server details is provided if the ServerSignature directive is enabled?
a) ServerAddons
b) ServerExtra
c) ServerTokens
d) ServerDetails

Answer: c

58. Which directive should we disable to obscure the fact that PHP is being used on our server?
a) show_php
b) expose_php
c) print_php
d) info_php

Answer: b

59. Say I want to change the extension of a PHP file, which of the following statements should I edit to change from .php to .html in the httpd.conf file?
a) AddType application/x-httpd-php .php
b) AddType application/x-httpd-php .asp
c) AddType application/x-httpd-asp .php
d) AddType application/x-httpd-asp .asp

Answer: a

60. The developers of PHP deprecated the safe mode feature as of which PHP version.
a) PHP 5.1.0
b) PHP 5.2.0
c) PHP 5.3.0
d) PHP 5.3.1

Answer: c

61. Which of the following is/are an external data?
i) Cookies
ii) Input data from a form
iii) Server Variables
iv) Web services data
a) Only ii)
b) ii) and iii)
c) None of the mentioned
d) All of the mentioned

Answer: d

62. How many types of filtering are present in PHP?
a) 3
b) 2
c) 4
d) None

Answer: b

63. Which one of the following filter is used to filter several variables with the same or different filters?
a) filter_var_array()
b) filter_var()
c) filter_input
d) filter_input_array

Answer: a

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

    <?php
    $num = "123";
    if (!filter_var($num, FILTER_VALIDATE_INT))
        echo("Integer is not valid");
    else
        echo("Integer is valid");
    ?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error

Answer: c

65. Which one of the following does not describe a validating filter?
a) Are used to allow or disallow specified characters in a string
b) Are used to validate user input
c) Strict format rules
d) Returns the expected type on success or FALSE on failure

Answer: a

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

    <?php
    $var=300;
    $int_options = array("options"=>array ("min_range"=>0, "max_range"=>256));
    if (!filter_var($var, FILTER_VALIDATE_INT, $int_options))
        echo("Integer is not valid");
    else
        echo("Integer is valid");
    ?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error

Answer: b

67. If the input variable is a string like this “http://www.saåånfoøøundry.com/”, the $url variable after the sanitizing will look like
a) http://www.saåånfoøøundry.com/
b) http://www.saaanfoooundry.com/
c) http://www.saånfoøundry.com/
d) http://www.sanfoundry.com/

Answer: d

68. Which one of the following filter checks if variable of specified type exists?
a) filter_has_var
b) filter_var
c) filter_id
d) filter_var_array

Answer: a

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

    <?php
    $value = 'car';
    $result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
    ?>
a) FALSE
b) TRUE
c) NULL
d) ERROR

Answer: c

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

    <?php
    function convertSpace($string)
    {
        return str_replace("_", " ", $string);
    }
    $string = "Peter_is_a_great_guy!";
    echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
    ?>
a) Peter_is_a_great_guy!
b) Peterisagreatguy!
c) Peter is a great guy!
d) Error

Answer: c

71. Which of the following is/are an external data?
i) Cookies
ii) Input data from a form
iii) Server Variables
iv) Web services data
a) Only ii)
b) ii) and iii)
c) None of the mentioned
d) All of the mentioned

Answer: d

72. How many types of filtering are present in PHP?
a) 3
b) 2
c) 4
d) None

Answer: b

73. Which one of the following filter is used to filter several variables with the same or different filters?
a) filter_var_array()
b) filter_var()
c) filter_input
d) filter_input_array

Answer: a

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

    <?php
    $num = "123";
    if (!filter_var($num, FILTER_VALIDATE_INT))
        echo("Integer is not valid");
    else
        echo("Integer is valid");
    ?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error

Answer: c

75. Which one of the following does not describe a validating filter?
a) Are used to allow or disallow specified characters in a string
b) Are used to validate user input
c) Strict format rules
d) Returns the expected type on success or FALSE on failure

Answer: a

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

   <?php
    $var=300;
    $int_options = array("options"=>array ("min_range"=>0, "max_range"=>256));
    if (!filter_var($var, FILTER_VALIDATE_INT, $int_options))
        echo("Integer is not valid");
    else
        echo("Integer is valid");
    ?>
a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error

Answer: b

77. If the input variable is a string like this “http://www.saåånfoøøundry.com/”, the $url variable after the sanitizing will look like
a) http://www.saåånfoøøundry.com/
b) http://www.saaanfoooundry.com/
c) http://www.saånfoøundry.com/
d) http://www.sanfoundry.com/

Answer: d

78. Which one of the following filter checks if variable of specified type exists?
a) filter_has_var
b) filter_var
c) filter_id
d) filter_var_array

Answer: a

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

    <?php
    $value = 'car';
    $result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
    ?>
a) FALSE
b) TRUE
c) NULL
d) ERROR

Answer: c

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

    <?php
    function convertSpace($string)
    {
        return str_replace("_", " ", $string);
    }
    $string = "Peter_is_a_great_guy!";
    echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
    ?>
a) Peter_is_a_great_guy!
b) Peterisagreatguy!
c) Peter is a great guy!
d) Error

Answer: c

81. Which two predefined variables are used to retrieve information from forms?
a) $GET & $SET
b) $_GET & $_SET
c) $__GET & $__SET
d) GET & SET

Answer: b

82. The attack which involves the insertion of malicious code into a page frequented by other users is known as..
a) basic sql injection
b) advanced sql injection
c) cross-site scripting
d) scripting

Answer: c

83. When you use the $_GET variable to collect data, the data is visible to..
a) none
b) only you
c) everyone
d) selected few

Answer: c

84. When you use the $_POST variable to collect data, the data is visible to..
a) none
b) only you
c) everyone
d) selected few

Answer: b

85. Which variable is used to collect form data sent with both the GET and POST methods?
a)$BOTH
b)$_BOTH
c)$REQUEST
d)$_REQUEST

Answer: d

86. Which one of the following should not be used while sending passwords or other sensitive information?
a) GET
b) POST
c) REQUEST
d) NEXT

Answer: a

87. Which function is used to remove all HTML tags from a string passed to a form?
a) remove_tags()
b) strip_tags()
c) tags_strip()
d) tags_remove()

Answer: b

88. What will be the value of the variable $input in the following PHP code?

    <?php
    $input = "Swapna<td>Lawrence</td>you are really<i>pretty</i>!";
    $input = strip_tags($input,"<i></i>");
    ?>
a) Swapna Lawrence you are really pretty!
b) Swapna <td>Lawrence</td> you are really<i>pretty</i>!
c) Swapna <td>Lawrence</td> you are really pretty!
d) Swapna Lawrence you are really<i>pretty</i>!

Answer: d

89. To validate an e-mail address, which flag is to be passed to the function filter_var()?
a) FILTER_VALIDATE_EMAIL
b) FILTER_VALIDATE_MAIL
c) VALIDATE_EMAIL
d) VALIDATE_MAIL

Answer: a

90. How many validation filters like FILTER_VALIDATE_EMAIL are currently available?
a) 5
b) 6
c) 7
d) 8

Answer: c


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

<?php
"Hello World"
?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error

Answer: c

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

<?php
print_r "Hello world"
?>
a) Error
b) Hello World
c) Nothing
d) Missing semicolon error

Answer: a

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

<?php
echo 'Hello World';
<html>
Hello world
</html>
?>
a) Hello world
b) Hello World Hello World
c) Hello world
Hello World
d) Syntax Error

Answer: d

94. What will be the output of the following PHP code ?
<?php
Echo "Hello World1";
echo " Hello world2";
ECHO " Hello world3";
?>
a) Hello world1 Hello world2 Hello World3
b) Hello world1
Hello world2
Hello World3
c) Error
d) Hello world1 Hello world3

Answer: a

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

<?php
$color = "red";
echo "$color";
echo "$COLOR";
echo "$Color";
?>
a) redredred
b) redred
c) red
d) Error

Answer: c

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

<?php
 # echo "Hello world";
 echo "# Hello world"; 
?>
a) # Hello world
b) Hello world# Hello world
c) Hello world
d) Error

Answer: a

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

<?php
echo "<i>Hello World</i>"
?>
a) Hello world
b) Hello world in italics
c) Nothing
d) Error

Answer: b

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

<?php
echo "echo "Hello World"";
?>
a) Hello world
b) echo “Hello world”
c) echo Hello world
d) Error

Answer: d

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

<?php
<?php
echo "Hello world";
?>
?>
a) 
b) Hello world
c) Nothing
d) Error

Answer: d

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

<?php
$color = red;
echo "\$color";
?>
a) red
b) $color
c) \red
d) Error
View Answer

Answer:b