Practice high-yield Programming & Software Development MCQs on Functions — Part 1 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Type Hinting was introduced in which version of PHP?
Correct Answer: Option B — PHP 5
Detailed Academic Explanation:
Type hinting gives you the ability to force parameters to be objects of certain class or to be arrays. PHP 5 introduced this feature.
💡Key Conceptual Anchor:Functions
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will happen in this function call? <?php function calc ( $price, $tax ) { $total = $price + $tax; } $pricetag = 15 ; $taxtag = 3 ; calc ( $pricetag, $taxtag ) ; ?>
Correct Answer: Option A — Call By Value
Detailed Academic Explanation:
When you pass an argument in the above manner or say we pass 15 and 3 directly, it is called passing by value or call by value.
💡Key Conceptual Anchor:Functions
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function calc ( $price, $tax = "" ) { $total = $price + ( $price * $tax ) ; echo "$total" ; } calc ( 42 ) ; ?>
Correct Answer: Option C — 42
Detailed Academic Explanation:
You can designate certain arguments as optional by placing them at the end of the list and assigning them a default value of nothing.
💡Key Conceptual Anchor:Functions
Question 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following are valid function names? 1. function() 2. €() 3. .function() 4. $function()
Correct Answer: Option A — Only 2
Detailed Academic 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.
💡Key Conceptual Anchor:Functions
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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 () ; ?>
Correct Answer: Option D — I am a Error
Detailed Academic Explanation:
This will be the output- I am a Fatal error: Cannot redeclare b()
💡Key Conceptual Anchor:Functions
Question 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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 () ; ?>
Correct Answer: Option C — Error
Detailed Academic Explanation:
This will be the output- Fatal error: Call to undefined function b(). You cannot call a function which is inside a function without calling the outside function.
💡Key Conceptual Anchor:Functions
Question 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php $op2 = "blabla" ; function foo ( $op1 ) { echo $op1; echo $op2; } foo ( "hello" ) ; ?>
Correct Answer: Option B — Error
Detailed Academic Explanation:
The correct answer is Option B: Error Here's why: In PHP, variables defined outside of a function are not automatically accessible inside the function. Scope is a crucial concept in programming. The variable `$op2` is defined outside the function `foo()`. Inside `foo()`, you are trying to use `$op2` without it being defined within the function's scope. Therefore, PHP will throw an error (specifically, a notice about an undefined variable) when trying to access `$op2` within the function. Although the `echo $op1` will work fine , but after that when trying to access the `$op2` that is not defined inside the function scope it will throw error . To make `$op2` accessible inside the function, you would need to either: 1. Pass it as an argument to the function. 2. Use the `global` keyword to bring it into the function's scope . Since neither of these is done in the given code, an error occurs.
💡Key Conceptual Anchor:Functions
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
A function in PHP which starts with ______ (double underscore) is know as.
Correct Answer: Option A — Magic Function
Detailed Academic Explanation:
Option A: Magic Function In PHP, a function that starts with double underscores (__) is known as a magic function . These functions are predefined in PHP and are automatically called when certain events or operations occur. Examples include __construct, __destruct, __get, __set, and many others, which are automatically invoked when specific conditions are met in the code. Option B: Inbuilt Function Inbuilt functions in PHP are built into the language and are not necessarily prefixed with double underscores. They are regular functions like echo(), print(), and other standard PHP functions, but do not follow the magic function naming convention. Option C: Default Function There is no specific category called "default function" in PHP. This term is not commonly used in the context of PHP function naming or functionality. Option D: User Defined Function User-defined functions are those created by the developer, and they do not follow the double underscore convention. These functions are typically defined with a name chosen by the user and are not prefixed with any special characters like the magic functions. Conclusion: The correct answer is Option A: Magic Function . Magic functions in PHP start with double underscores and are automatically invoked by PHP under certain conditions.
💡Key Conceptual Anchor:Functions
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function foo ( $msg ) { echo "$msg" ; } $var1 = "foo" ; $var1 ( "will this work" ) ; ?>
Correct Answer: Option D — Will this work
Detailed Academic Explanation:
It is possible to call a function using a variable which stores the function name.
💡Key Conceptual Anchor:Functions
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which one of the following PHP functions can be used to build a function that accepts any number of arguments?
Correct Answer: Option B — func_get_argc()
Detailed Academic Explanation:
Here is an example- <?php function foo () { $args = func_get_args() ; foreach ( $args as $k => $v ) { echo "arg" .( $k + 1 ). ": $vn" ; } } foo () ; /* will print nothing */ foo ( "Hello" ) ; /* will print Hello */ foo ( "Hello" , "World" , "Bye" ) ; /* will print Hello World Bye */ ?>
💡Key Conceptual Anchor:Functions
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which one of the following PHP functions can be used to find files?
Correct Answer: Option A — glob()
Detailed Academic Explanation:
Here is an example- // get all php files AND txt files $files = glob( '*.{php,txt}' , GLOB_BRACE ) ; print_r( $files ) ; /* output looks like: Array ( [0] => phptest.php [1] => pi.php [2] => post_output.php . . . )
💡Key Conceptual Anchor:Functions
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following PHP functions can be used to get the current memory usage?
Correct Answer: Option C — get_memory_usage()
Detailed Academic Explanation:
We can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.
💡Key Conceptual Anchor:Functions
Question 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following PHP functions can be used for generating unique id’s?
Correct Answer: Option A — uniqueid()
Detailed Academic Explanation:
Many people use the md5() function for this, even though it’s not exactly meant for this purpose. uniqueid() is the function that is to be used.
💡Key Conceptual Anchor:Functions
Question 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which one of the following functions can be used to compress a string?
Correct Answer: Option D — gzcompress()
Detailed Academic Explanation:
We will be able to achieve almost 50% size reduction using this function. The gzuncompress() function is used to uncompress the string.
💡Key Conceptual Anchor:Functions
Question 15 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php echo "chr(52)" ; ?>
Correct Answer: Option D — 4
Detailed Academic Explanation:
The chr() function returns a character from the specified ASCII value. Since the ASCII value of 4 is 52, thus 4 was displayed.
💡Key Conceptual Anchor:Functions
Question 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php echo ord ( "hi" ) ; ?>
Correct Answer: Option C — 104
Detailed Academic Explanation:
The ord() function returns the ASCII value of the first character of a string. The ASCII value of h is 104, thus 104 was displayed.
💡Key Conceptual Anchor:Functions
Question 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php $str = "Hello World" echo wordwrap( $str, 5 , "n" ) ; ?>
Correct Answer: Option B — HelloWorld
Detailed Academic Explanation:
The wordwrap() function wraps a string into new lines when it reaches a specific length.
💡Key Conceptual Anchor:Functions
Question 18 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php echo ( atan ( 0.50 )); ?>
Correct Answer: Option C — 0.46364760900081
Detailed Academic Explanation:
The atan() function returns the arc tangent of arg as a numeric value between -Pi/2 and Pi/2 radians.
💡Key Conceptual Anchor:Functions
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php define ( "GREETING" , "Hello you! How are you today?" ) ; echo constant ( "GREETING" ) ; ?>
Correct Answer: Option A — Hello you! How are you today?
Detailed Academic Explanation:
The define() function defines a constant.
💡Key Conceptual Anchor:Functions
Question 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php define ( "GREETING1" , "Hello you! How are you today?" ) ; define ( "GREETING2" , "Hello you! How are you today?" ) ; define ( "GREETING3" , "Hello you! How are you today?" ) ; echo defined ( "GREETING" ) ; ?>
Correct Answer: Option B — 0
Detailed Academic Explanation:
The defined() function checks whether a constant exists.
💡Key Conceptual Anchor:Functions
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function sum ( $num1, $num2 ) { $total = $num1 + $num2; echo "chr($total)" ; } $var1 = "sum" ; $var1 ( 5, 44 ) ; ?>
Correct Answer: Option C — 1
Detailed Academic Explanation:
It is possible to call a function using a variable which stores the function name also the chr() function returns a character from the specified ASCII value.
💡Key Conceptual Anchor:Functions
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function sum ( $num1, $num2 ) { $total = $num1 + $num2; echo "cos($total)"; } sum ( 5 ,- 5 ) ; ?>
Correct Answer: Option B — 1
Detailed Academic Explanation:
cos() gives the cos value of the argument. Here the function returns 1.
💡Key Conceptual Anchor:Functions
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function b() { echo "b is executed" ; } function a() { b () ; echo "a is executed" ; b () ; } a () ; ?>
Correct Answer: Option D — b is executeda is executedb is executed
Detailed Academic Explanation:
imple order of execution.
💡Key Conceptual Anchor:Functions
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function addFunction ( $num1, $num2 ) { $sum = $num1 + $num2; return $sum; } $return_value = addFunction ( 10 , 20 ) ; echo "Returned value from the function : $return_value" ?>
Correct Answer: Option C — Returned value from the function : 30
Detailed Academic Explanation:
Functions returns value 30.
💡Key Conceptual Anchor:Functions
Question 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function sayHello () { echo "HelloWorld<br />" ; } $function_holder = "sayHello" ; $function_holder () ; ?>
Correct Answer: Option D — HelloWorld
Detailed Academic Explanation:
It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.
💡Key Conceptual Anchor:Functions
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? span> function one () { echo " this works" ; function two () { echo "this too works" ; } } one () ; two () ; ?>
Correct Answer: Option C — this worksthis too works
Detailed Academic Explanation:
Two is declared in one and is called after one.Hence it works.
💡Key Conceptual Anchor:Functions
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function do( $myString ) { echo strpos ( $myString , "donkey",0 ); } do ( "The donkey looks like a horse." ); ?>
Correct Answer: Option A — 4
Detailed Academic Explanation:
Donkey starts from position 4 in string.
💡Key Conceptual Anchor:Functions
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php function one span> () { define ( "const","I am awesome!" ); echo constant ( "const" ); } one () ; ?>
Correct Answer: Option A — I am awesome!!
Detailed Academic Explanation:
Using the define function to define the constant “const”.
💡Key Conceptual Anchor:Functions
Question 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php echo hex2bin( "48656c6c6f20576f726c6421" ); ?>
Correct Answer: Option A — Hello World!
Detailed Academic Explanation:
The hex2bin() function converts a string of hexadecimal values to ASCII characters.
💡Key Conceptual Anchor:Functions
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php $str = addslashes ( 'What does "yolo" mean?' ); echo ($str); ?>
Correct Answer: Option C — None of the above
Detailed Academic Explanation:
The addslashes() function returns a string with backslashes in front of predefined characters.
💡Key Conceptual Anchor:Functions
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php $str = "Hello world. It's a beautiful day." ; print_r (explode( " ",$str )) ; ?>
The explode() function breaks a string into an array.
💡Key Conceptual Anchor:Functions
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php echo strtr ( "Hilla Warld","ia","eo" ); ?>
Correct Answer: Option B — Hello World
Detailed Academic Explanation:
The strtr() function translates certain characters in a string.
💡Key Conceptual Anchor:Functions
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code? <?php echo stripos( "I love php, I love php too!","PHP" ); ?>
Correct Answer: Option B — 7
Detailed Academic Explanation:
The stripos() function finds the position of the first occurrence of a string inside another string.
💡Key Conceptual Anchor:Functions
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code ? <?php function A1 ( $x ) { switch ( $x ) { case 1 : //this statement is the same as if($x == 1) echo 'Case 1 was executed.' ; break ; case 2 : //this statement is the same as if($x == 2) echo 'Case 2 was executed.' ; break ; case 3 : //this statement is the same as if($x == 3) echo 'Case 3 was executed.' ; break; case 4 : //this statement is the same as if($x == 4) echo 'Case 4 was executed.' ; break ; default : //this statement is the same as if $x does not equal the other conditions echo 'Default was executed.' ; break ; } } A1 ( 9 ) ; ?>
Correct Answer: Option D — Case 4 was executed
Detailed Academic Explanation:
The switch statement is executed with $x = 9.
💡Key Conceptual Anchor:Functions
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code ? <?php function TV ( $string ) { echo "my favourite TV show is " .$string; function b() { echo " I am here to spoil this code" ; } } b () ; ?>
Correct Answer: Option B — Error
Detailed Academic Explanation:
b is undeclared if TV() is not called first.
💡Key Conceptual Anchor:Functions
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code ? <?php function TV ( $string ) { echo "my favourite TV show is " .$string; function b () { echo " I am here to spoil this code" ; } } function b () { echo " I am here to spoil this code" ; } b () ; ?>
Correct Answer: Option A — I am here to spoil this code
Detailed Academic Explanation:
This one works because b is declared independent of TV() also.
💡Key Conceptual Anchor:Functions
Question 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code ? <?php function TV ( $string ) { echo "my favourite TV show is " .$string; function b () { echo " I am here to spoil this code" ; } } function b () { echo " I am here to spoil this code" ; } b () ; TV ( "Sherlock" ) ; ?>
Correct Answer: Option B — Error
Detailed Academic Explanation:
Function b is declared twice.
💡Key Conceptual Anchor:Functions
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code ? <?php function TV ( $string ) { echo "my favourite TV show is " . $string; function b () { echo " I am here to spoil this code" ; } } a ( "Sherlock" ) ; b () ; ?>
Correct Answer: Option C — my favourite TV show is SherlockI am here ro spoil this code
Detailed Academic Explanation:
b is declared as TV() is executed first.
💡Key Conceptual Anchor:Functions
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code ? <?php function calc ( $num1, $num2 ) { $total = $num1 * $num2; } $result = calc( 42, 0 ); echo $result; ?>
Correct Answer: Option A — Error
Detailed Academic Explanation:
Function does not return anything.
💡Key Conceptual Anchor:Functions
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following PHP code ? <?php function calc ( $num1, $num2 ) { $total = $num1 * $num2; return $total; } $result = calc ( 42, 0 ) ; echo $result; ?>
Correct Answer: Option B — 0
Detailed Academic Explanation:
Function returns $total.
💡Key Conceptual Anchor:Functions
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.