PHP MCQs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

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

2. <?php
3. $y = 2;
4. if ($y-- == ++$y)
5. {
6. echo $y;
7. }
8. ?>

A. 2
B. 1
C. 3
D. no output

Answer: Option A

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

1. <?php
2. $x = 4;
3. $y = -3;
4. $z = 11;
5. echo 4 + $y * $z / $x;
6. ?>

A. 4.25
B. 3.25
C. -3.25
D. -4.25

Answer: Option D

3. If session.use_cookie is set to 0, this results in use of _____________


A. Session
B. Cookie
C. URL rewriting
D. Nothing happens

Answer: Option C

4. 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: Option C

5. 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: Option B

6. 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: Option C

7. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?


A. 12
B. 1
C. Error
D. 5

Answer: Option D
8. 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: Option D

9. Which statement will output $x on the screen?


A. echo “\$x”;
B. echo “$$x”;
C. echo “/$x”;
D. echo “$x;

Answer: Option A

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


< ?php
$num = 10;
echo 'What is her age? \n She is $num years old';
?>

A. What is her age? \n She is $num years old


B. What is her age?
She is $num years old
C. What is her age? She is 10 years old
D. What is her age?
She is 10 years old

Answer: Option A

Reference Website for above questions is: http://www.allindiaexams.in/engineering/cse/php-


questions-and-answers/php-basics

11. Which of the following variables does PHP use to authenticate a user?

i) $_SERVER['PHP_AUTH_USER'].
ii) $_SERVER['PHP_AUTH_USERS'].
iii) $_SERVER['PHP_AUTH_PU'].
iv) $_SERVER['PHP_AUTH_PW'].
A. and ii)
B. and iv)
C. and iv)
D. and iii)

Answer: Option C

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

1. <?php
2. $cars = array("Volvo", "BMW", "Toyota");
3. echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . ".";
4. ?>

A. I like Volvo, Toyota and BMW


B. I like Volvo, BMW and Toyota
C. I like BMW, Volvo and Toyota
D. I like Toyota, BMW and Volvo

Answer: Option D

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

1. class Person
2. {
3. function getName() { return "Bob"; }
4. function getAge() { return 44; }
5. function __toString() {
6. $desc = $this->getName();
7. $desc .= " (age ".$this->getAge().")";
8. return $desc;
9. }
10. }
11. $person = new Person();
12. print $person;

A. Object Not Found


B. PHP Catchable fatal error
C. BOB (age 44)
D. BOB

Answer: Option C
14. What will be the output of the following PHP code?

1. <?php
2. $a1 = array_fill(1, 4, "hello");
3. $b1 = array_fill(5, 1, "php");
4. $a2 = array_merge($a1, $a2);
5. print_r($a2);
6. echo "<br>";
7. print_r($b1);
8. ?>

A. Array ( [1] => hello [4] => hello [5] => php )
Array ( [5] => php )
B. Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [5] => php )
C. Array ( [1] => hello [2] => hello [3] => hello [4] => hello [5] => php )
Array ( [5] => php )
D. Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [1] => php )

Answer: Option C

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

1. <?php
2. $names = array("Sam", "Bob", "Jack");
3. echo $names[0]."is the brother of ".$names[1]." and ".$names[1].".".$brother;
4. ?>

A. Sam is the brother of Bob and Bob) $brother


B. Sam is the brother of Bob and Bob)
C. $brother
D. Error

Answer: Option D

Reference Website for above questions is:


http://www.allindiaexams.in/engineering/cse/php-questions-and-answers/php-functions/

https://www.sanfoundry.com/1000-php-questions-answers/

16. What will be the value of $a and $b after the function call in the following PHP code?

1. <?php
2. function doSomething( &$arg ) {
3. $return = $arg;
4. $arg += 1;
5. return $return;
6. }
7. $a = 3;
8. $b = doSomething( $a );
9. ?>

A. a is 3 and b is 4
B. a is 4 and b is 3
C. Both are 3
D. Both are 4

Answer: Option B

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


< ?php
$fruits = array ("mango", "apple", "pear", "peach");
$fruits = array_flip($fruits);
echo ($fruits[0]);
?>

A. mango
B. Error
C. peach
D. 0

Answer: Option B

18. 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: Option B

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


< ?php
$fruits = array ("apple", "mango", "peach", "pear",
"orange");
$subset = array_slice ($fruits, 2);
print_r ($subset);
?>
A. Array ( [0] => peach )
B. Array ( [0] => apple [1] => mango [2] => peach )
C. Array ( [0] => apple [1] => mango )
D. Array ( [0] => peach [1] => pear [2] => orange )

Answer: Option D

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


< ?php
$number = array ("4", "hello", 2);
echo (array_sum ($number));
?>
A. 4hello2
B. 4
C. 2
D. 6
Answer: Option D

Reference Website for above questions is:


http://www.allindiaexams.in/engineering/cse/php-questions-and-answers/php-arrays/

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

1. <?php
2. $x = 75;
3. $y = 25;
4. function addition()
5. {
6. $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
7. }
8. addition();
9. echo $z;
10. ?>

A. 100
B. error
C. 75
D. 25

Answer: Option A

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


1. <?php
2. $user = array("Ashley", "Bale", "Shrek", "Blank");
3. for ($x = 0; $x < count($user); $x++)
4. {
5. if ($user[$x] == "Shrek")
6. continue;
7. printf ($user[$x]);
8. }
9. ?>

A. AshleyBaleBlank
B. AshleyBale
C. AshleyBaleShrek
D. No output

Answer: Option A

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

1. <?php
2. for ($i = 0; $i % ++$i; $i++)
3. {
4. print"i";
5. }
6. ?>

A. error
B. infinite loop
C. no output
D. 0

Answer: Option B

Reference Website for above questions is:


http://www.allindiaexams.in/engineering/cse/php-questions-and-answers/exception-
handling/

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

1. <?php
2. $color1 = "red";
3. $color2 = "1";
4. echo "$color1" + "$color2";
5. ?>
A. red1
B. red 1
C. 0
D. 1

Answer: Option D

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

A. <?php
B. $i = 0;$j = 1;$k = 2;
C. print !(( + + $i + $j) > ($j - $k));
D. ?>
A. 1
B. no output
C. error
D. 0

Answer: Option B

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

1. <?php
2. $a = 'a' ;
3. print $a * 2;
4. ?>

A. 192
B. 2
C. error
D. 0

Answer: Option D

27. Which of the following method sends input to a script via a URL?
A. Get
B. Post
C. Both
D. None

Answer: Option A
28. Which of the following function returns a text in title case from a variable?
A. ucwords($var)
B. upper($var)
C. toupper($var)
D. ucword($var)

Answer: Option A

29. Which one of the following functions finds the last occurrence of a string, returning its
numerical position?
A. strlastpos()
B. strpos()
C. strlast()
D. strrpos()

Answer: Option D

30. Which one of the following regular expression matches any string containing zero or one
p?
A. p+
B. p*
C. P?
D. p#

Answer: Option C
31. What will be the output of the following PHP code?

1. <?php
2. echo ord ("hi");
3. ?>
A. 106
B. 103
C. 104
D. 209

Answer: Option C

32. Which magic method is used to implement overloading in PHP?


A. __call
B. __invoke
C. __wakeup
D. __unset
Answer: Option A
Reference Website for above questions is:
http://www.allindiaexams.in/engineering/cse/php-questions-and-answers/strings-regular-
expressions
https://www.examtiger.com/mcq/php-programming-questions-answers/
https://www.sanfoundry.com/1000-php-questions-answers/

You might also like