14
PHP Basic and Fundamental Questions and Answers with Detail Explanation Page 1 of 14 PART I - Multiple choice questions focuses on PHP basics and fundamentals: 1. PHP files have a default file extension of... A. .html B. .xml C. .php D. .ph 2. A PHP script should start with ... and end with ... A. <php > B. <? php ?> C. <? ?> D. <?php ?> Description: <?php ?> The standard opening and closing tags <? ?> In addition PHP also allows for short open tag and can be enabled and disabled targeting --enable-short-tags option in the PHP configuration file php.ini. This is method is not recommended since it is only available if enabled! 3. Which of the following must be installed on your computer to run and test PHP script? A. Adobe Dreamweaver B. PHP C. Apache D. Notepad++ E. All of the mentioned. Description: To test and run PHP script it is required to have PHP and a web server installed and configured.

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Embed Size (px)

Citation preview

Page 1: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 1 of 14

PART I - Multiple choice questions focuses on PHP basics and

fundamentals:

1. PHP files have a default file extension of... A. .html

B. .xml

C. .php

D. .ph

2. A PHP script should start with ... and end with ... A. <php >

B. <? php ?>

C. <? ?>

D. <?php ?>

Description:

<?php ?> The standard opening and closing tags

<? ?> In addition PHP also allows for short open tag and can be enabled

and disabled targeting --enable-short-tags option in the PHP configuration

file php.ini. This is method is not recommended since it is only available if

enabled!

3. Which of the following must be installed on your computer to run and test PHP script?

A. Adobe Dreamweaver

B. PHP

C. Apache

D. Notepad++

E. All of the mentioned.

Description: To test and run PHP script it is required to have PHP and a web server

installed and configured.

Page 2: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 2 of 14

4. We can use ... to comment a single line? A. /?

B. //

C. #

D. /* */

E. All of the mentioned.

Description: # and // are used and recommended for single line comment, and /* */

can also be used to comment either a single line or multi line.

5. Which of the following PHP statement will store 125 in variable number?

A. int $number = 125;

B. int number = 125;

C. $number = 125;

D. 125 = $number;

Description: PHP is not a strong typed language like C and Java, therefore, no need

not specify the datatype during variable declaration and initialization.

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

<?php

$a = 1;

$b = 2;

echo $a . "+". $b;

?>

A. 3

B. 1+2

C. 1.+.2

D. Error

Description: The dot (.) operator is used to concatenate two or more parts together.

Page 3: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 3 of 14

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

<?php

$a = "1";

$b = "2";

echo $a + $b;

?>

A. 3

B. 1+2

C. Error

D. 12

Description: The + operator in PHP is used for mathematical operations; therefore,

the numbers inside the double quotes during the calculation are considered and

parsed as integers and not string.

8. Which of following variables are valid and can be assigned a value to it?

A. $3hello

B. $_hello

C. $this

D. $This

E. All of the mentioned

Description: Similar as other languages such as Java, C and C++ a variable can't

start with a number in general. The $this is a reference to the current object, and it

is the way to reference an instance of a class from within itself and most commonly

used in object oriented. PHP is a Case Sensitive language, therefore, $This is a valid

variable name.

Page 4: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 4 of 14

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

<?php

$name = 'Abdul Rahman';

$anotherName = &$name;

$anotherName = "My name is $anotherName";

echo $anotherName . " " . $name;

?>

A. Error

B. My name is Abdul Rahman Abdul Rahman

C. My name is Abdul Rahman My name is Abdul Rahman

D. My name is Abdul Rahman Abdul Rahman

Description: The statement $anotherName = &$name; will assign the reference and

address of variable $name to $anotherName. This means any modification and

changes to the variable $name will impact $anotherName.

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

<?php

$colors = "Red, Green, Blue";

echo $colors[2];

?>

A. Blue

B. Error

C. d

D. Green

Description: PHP treats strings same as arrays, allowing for specific characters to be

accessed via array offset notation. Therefore, index 0 will print character "R", index 1

will print character "e", and index 2 will print character "d".

Page 5: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 5 of 14

11. 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 10

Description: The + operator in PHP is used for arithmetic operations, therefore, the

value "25" at the beginning of the original $total string is parsed as integer.

However if it begins with anything but a numerical value, the parse result will be

value 0.

12. 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

Description: The += is shortcut assignment for addition operation, for example, a

+= b is equivalent to a = a + b. The same can be done with subtraction,

multiplication, division, modulus, etc.

13. Which statement will output $name on the screen? A. echo "\$name";

B. echo "$$name";

C. echo "/$name";

D. echo "$name;";

Description: The \$ is used to escape the dollar sign ($), therefore, the $name is

treated as a normal string character.

Page 6: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 6 of 14

14. 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. 012

Description: The static retains its previous value each time the function is called.

Therefore, it makes the function remember the value of the given variable ($count in

the above example) between multiple calls.

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

<?php

$name = "Abdul Rahman";

$name .= " Sherzad";

echo $name;

?>

A. Abdul Rahman

B. true

C. false

D. Abdul Rahman Sherzad

Description: The (.=) is a shortcut assignment for concatenation operation. Hence,

the $name equals its current value concatenated with the string " Sherzad".

The statement $name .= " Sherzad"; is equivalent to $name = $name . " Sherzad";

Page 7: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 7 of 14

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

<?php

$a = 5;

$b = 5;

echo ($a === $b);

?>

A. 5 === 5

B. Error

C. 1

D. False

Description: The == operator compares the values of variables for equality and type

casting as necessary. But the === operator checks if the two variables are of the

same type AND have the same value. Hence, variable $a, and variable $b, both are

integers and have same value it prints 1.

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

<?php

$age = 10;

echo 'What is his age? \n He is $age years old';

?>

A. What is his age? \n He is $age years old

B. What is his age?

He is $age years old

C. What is his age? He is 10 years old

D. What is his age?

He is 10 years old

Description: The escape sequence characters and variables when are enclosed

within single quotes '' will not be interpreted when the string is parsed!

Page 8: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 8 of 14

18. Which of the below symbols is a newline character? A. \r

B. \n

C. /n

D. /r

Description: The \n escape character in PHP and other most programming

languages is treated as newline character.

19. Which of the conditional statements is/are supported by PHP? A. if statements

B. if-else statements

C. if-elseif statements

D. switch statements

E. A), B) and C)

F. B), C) and D)

G. All of the mentioned.

Description: PHP supports all the mentioned conditional statements.

20. Which of the looping statements is/are supported by PHP? A. for loop

B. while loop

C. do-while loop

D. foreach loop

E. A) and B)

F. A), B) and C)

G. All of the mentioned

H. None of the mentioned

Description: PHP supports advanced foreach loop along with other mentioned

common loop statements.

Page 9: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 9 of 14

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

<?php

$today = "Monday";

switch ($today) {

case "Saturday":

echo "Beginning of the week. ";

case "Monday":

echo "Middle of the week. ";

case "Thursday":

echo "End of the week. ";

}

?>

A. Error

B. Beginning of the week.

C. Middle of the week.

D. Middle of the week. End of the week.

E. End of the week.

Description: When the break statement is not present, all subsequent case blocks

will execute until a break statement is found.

22. What will be returned when the following PHP code is executed?

<?php

$a = 12;

echo ($a == 12) ? 5 : 1;

?>

A. 12

B. 1

C. Error

D. 5

Description: The above PHP segment used ternary operator. If condition is true then

the part just after the ? operator is executed otherwise the part after : operator.

Page 10: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 10 of 14

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

<?php

$users = array("absherzad", "salam", "sattar", "wahid");

for ($x=0; $x < count($users); $x++) {

if ($users[$x] == "absherzad") continue;

echo $users[$x] . " ";

}

?>

A. absherzad

B. salam sattar

C. absherzad salam sattar

D. salam sattar wahid

Description: The continue statement works opposite of break statement, thus,

causes execution of the current loop iteration to end and start at the beginning of

the next iteration.

24. What is the value of $a and $b after the function call?

<?php

function doSomething( &$arg ) {

$return = $arg;

$arg += 1;

return $return;

}

$a = 3;

$b = doSomething( $a );

echo "$a<br>$b";

?>

A. a is 3 and b is 4.

B. a is 4 and b is 3.

C. Both are 3.

D. Both are 4.

Description: Because $arg is passed by reference the variable $a is 4, on the other

hand, because the return value of the function is a copy of the initial value of the

$arg the variable $b is 3.

Page 11: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 11 of 14

25. Who is the father of PHP? A. Rasmus Lerdorf

B. Willam Makepiece

C. Drek Kolkevi

D. List Barely

PART II – Multiple choice questions focuses on PHP built-in functions,

arrays, filters, regular expression, and file systems:

1. Which one of the following PHP functions can be used to find files? A. glob()

B. file()

C. fold()

D. get_file()

Description: The glob() function in PHP searches for all the files based on the

matching pattern passed to it, as an example, glob("*.txt") searches all the text files

in the current directory.

2. Which of the following are correct ways of creating an array? A. state[0] = "West Zone";

B. $state[] = array("West Zone");

C. $state[0] = " West Zone";

D. $state = array("West Zone");

Description: option (A) is not correct because in PHP a variable name must start

with $ symbol. Option (B) is not correct because the square brackets [] are not valid

characters in a variable name.

Page 12: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 12 of 14

3. 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

Description: The echo (count($fruits)); statement does not count the items of the

multidimensional arrays, therefore, it prints 4. If the second optional mode

parameter is set to 1, the count() function will recursively count the array, therefore,

the array entity holding "pear" and "mango" items is also counted.

4. 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

Description: Since the integer value of the variable $var is "300"; and 300 is not in

the specified range, so, the output of the code above will be: "Integer is not valid".

Page 13: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 13 of 14

5. Which one of the following regular expression matches any string containing zero or one p?

A. p+

B. p*

C. P?

D. p#

Description:

The ? (question mark) matches when the preceding character occurs 0 or 1

times only.

The * (asterisk or star) matches when the preceding character occurs 0 or

more times.

The + (plus) matches when the preceding character occurs 1 or more times.

6. The filesize() function returns the file size in ___. A. bits

B. bytes

C. kilobytes

D. gigabytes

Description: The filesize() function returns the size of the file in bytes, or FALSE (and

generates an error of level E_WARNING) in case of an error.

7. Which one of the following function reads a directory into an Array? A. scandir()

B. readdir()

C. scandirectory()

D. readdirectory()

Description: The scandir() function returns an array consisting of files and directories

found in directory or returns FALSE on error.

Page 14: PHP Basic and Fundamental Questions and Answers with Detail Explanation

PHP Basic and Fundamental Questions and Answers with Detail Explanation

Page 14 of 14

PART III – Explain these easy but yet tricky questions:

1. Explain why the following PHP code returns 3.2 instead of 5:

<?php

$number = 020;

echo $number / 5;

?>

Description: The output of the above code segment is 3.2. This is because in PHP

starting a number with 0 means octal (base 8), therefore, octal 020 is equal 16 in

decimal. Thus, 16 / 5 = 3.2

2. What is the output of the following PHP snippet? Explain why is that

the output?

<?php

echo print(10);

?>

Description: The print always returns 1. Therefore, print statement first print the

value 10, and then return 1 which is then echoed by the echo statement, as a result,

the output is 101.