MCQs > IT & Programming > PHP 5 MCQs > Basic PHP 5 MCQs

Basic PHP 5 MCQ

1.

On compilation. the following PHP 7.1 code was supposed to produce the output 24.

the code lines with errors.


1.

2.

3. class example

4- {

5. public $foo;

G.

7. public function__construct()[

8. $this->function = foo()[

9. return 24;

10. };

11. }

12. }

13. $exp = new example(]:

14. echo ($cxp->function)(). PHP_EOL;


Answer

Correct Answer:

Line 8


Note: This Question is unanswered, help us to find answer for this one

2.

Which of the following options are used for specifying a string literal in PHP 7.1?

i) Single quotes

ii) Double quotes

iii) heredoc syntax

iv) now doc syntax


Answer

Correct Answer:

All options i), ii), iii) and iv).


Note: This Question is unanswered, help us to find answer for this one

3.

Which of the given statements is correct about the following PHP 7.1 code snippet?

$x = 2.98765431;

$y = 2.98765430;

$epsilon = 0.00001; if(abs($x-$y) < $epsilon) {

echo "true";

}

?>

Answer

Correct Answer:

The code will compile and will print true. 


Note: This Question is unanswered, help us to find answer for this one

4. In PHP 7.1, which of the following is the correct syntax of the session_gc() function, which is used to perform session data garbage collection?

Answer

Correct Answer: int session_gc (void )

Note: This Question is unanswered, help us to find answer for this one

5.

Find the output of the following PHP 7.1 code.

$example = array(

5 => "e",

"5" => "f",

5.5 => "g",

true => "i",

);

var_dump($example);

?>


Answer

Correct Answer:

array(2) {

[5]=>

string(1) "i" ">


array(2) {

[5]=>

string(1) "f" ">


array(2) {

[5]=>

string(1)   "i” ">


array(2) {

[5]=>

string(1)  "g" ">


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

6.

What will be the output of the following PHP code?

echo $x = 'D7';

for ($i=0; $i<5; $i++) {

echo ++$x . PHP_EOL;

}

?>

'>

Answer

Correct Answer:

-Result-

D8

D9

E0

E1

E2



Note: This Question is unanswered, help us to find answer for this one

7.

Find the output of the following PHP 7.1 code.

example.php

$x =

Answer

Correct Answer:

example.php 


Note: This Question is unanswered, help us to find answer for this one

8. Which of the following is the correct syntax of the PHP 7.1 magic method, __Sleep()?

Answer

Correct Answer: public array __sleep ( void )

Note: This Question is unanswered, help us to find answer for this one

9.

Which of the given statements is correct about the following PHP 7.1 code?

function example()

{

static $cnt = 0;

$cnt++;

echo $cnt;

if ($cnt < 5) {

example();

}

$cnt--;

}

?>

Answer

Correct Answer:

The code will compile successfully but will not print any output. 


Note: This Question is unanswered, help us to find answer for this one

10. In relation to PHP 7.1 throwable interface, which of the following options is used for getting a string representation of a thrown object?

Answer

Correct Answer: Throwable::__toString

Note: This Question is unanswered, help us to find answer for this one

11.

Carefully analyze the following program.

1.

2.class example

3.{

4.static function check()

5.{

6.echo "Check\n";

7.}

8.function test()

9.{

10.echo "Test\n";

11.}

12.}

13.$f = new array("example", "check");    исправил:) $f = array( new example, "check");

14.$f();

15.$f = array(new example, test);  исправил:)    $f = array(new example, "test");

16.$f();

17.$f = "example::check";

18.$f();

19.?>

The given program was supposed to produce the following output on execution.

Check

Test

Check

However, the program contains errors. Identify the line numbers that contain errors.


Answer

Correct Answer:

Line 13


Line 15


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

12. In relation to PHP 7.1 global space, prefixing a name with which of the following options specifies that the name is required from the global space even in the context of the namespace?

Answer

Correct Answer: \

Note: This Question is unanswered, help us to find answer for this one

13.

What will be the output of the following PHP code?

var_dump("pqrstu"[-2]);

var_dump(strpos("ppqqrr","q", -3)); ?>

Answer

Correct Answer:

string(1) int(3) 


Note: This Question is unanswered, help us to find answer for this one

14.

What will be the output of the following PHP 7.1 code snippet?

-

abstract class A_Class

{

abstract protected function name_P($name);

}

class Example extends A_Class

{

public function Name_P($name, $separator = ".") {

if ($name == "Mango") {

$product = "Fruit";

} elseif ($name == "Carrot") {

$product = "Vegitable";

} else {

$product ="";

}

return "{$name}{$separator} {$product}";

}

}

$class = new Example;

echo $class->Name_P("Mango"), "\n";

echo $class->Name_P("Carrot"), "\n";

?>

Answer

Correct Answer:

-Mango. Fruit

Carrot. Vegitable



Note: This Question is unanswered, help us to find answer for this one

15. In relation to PHP array cast, Booleans can be cast to which of the following options?

Answer

Correct Answer: Integer

Note: This Question is unanswered, help us to find answer for this one

16. In PHP 7.1, which of the following options can be used to mark the type declarations for parameters and return values as nullable, by prefixing the type name?

Answer

Correct Answer: Question mark (?)

Note: This Question is unanswered, help us to find answer for this one

17. In PHP 7.1, which of the following operators are non-associative?

Answer

Correct Answer: &&
??

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

18. In PHP 7.1, which of the following is the correct syntax of the is_iterable() function, which is used to verify whether or not the content of a variable is an iterable value?

Answer

Correct Answer: bool is_iterable (mixed $var)

Note: This Question is unanswered, help us to find answer for this one

19. While creating or modifying an array in PHP 7.1, what will happen if an empty index operator is applied on a string?

Answer

Correct Answer: It will throw a fatal error.

Note: This Question is unanswered, help us to find answer for this one

20.

Find the output of the following PHP code.

$string = echo "The output for '$string' is '$string[-1]'.\n"; ?>

Answer

Correct Answer:

The output for 'example' is 'e'.


Note: This Question is unanswered, help us to find answer for this one

21.

What will be the output of the following PHP 7.1 code snippet?

class Red {

public static function b_func() {

static::a_func();

}

public static function a_func() {

echo "Colour is ",__CLASS__."\n";

}

}

class Blue extends Red {

public static function run() {

Red::b_func();

parent ::b_func();

self::b_func();

}

public static function a_func() {

echo "Colour is ",__CLASS__."\n";

}

}

class Green extends Blue {

public static function a_func() {

echo "Colour is ",__CLASS__."\n";

}

}

Green::run();

?>


Answer

Correct Answer:

Colour is Red

Colour is Green

Colour is Green



Note: This Question is unanswered, help us to find answer for this one

22.

In PHP 7.1, under which of the following types of scenarios, a TypeError may be thrown?

i) When the argument type, which is being passed to a function, does not match its corresponding declared parameter type.

ii) When the returned value from a function does not match the declared function return type.

iii) When an invalid number of arguments are passed to a built-in PHP function.


Answer

Correct Answer:

All options i), ii) and iii).


Note: This Question is unanswered, help us to find answer for this one

23.

In PHP 7.1, which of the following is the correct syntax for a function with iterable generator return type?

Answer

Correct Answer:

function example(): iterable {

yield 1;

yield 2;

yield 3;

}

?>



Note: This Question is unanswered, help us to find answer for this one

24.

What will be the output of the following PHP code?

interface Exp { public function x();}

class First implements Exp {

public function x() {}

}

class Second {}

function x(Exp $e){

echo get_class($e)."\n";

}

x(new First);

x(new Second);

?>


Answer

Correct Answer:

First Fatal error


Note: This Question is unanswered, help us to find answer for this one

25.

What will be the output of the following PHP 7.1 code?

declare(ticks=1);

function t_handler()

{

echo "Example function\n";

}

register_tick_function( $x = 1;

if ($x > 0) {

$x += 2;

print($x);

}

?>

Answer

Correct Answer:

Example function

Example function

Example function

3Example function  



Note: This Question is unanswered, help us to find answer for this one

26.

What will be the output of the following PHP 7.1 code?

$example = true;

example_y();

if ($example){

function example_x()

{

echo "PHP\n";

}

}

if ($example)example_x();

function example_y()

{

echo "Example\n";

}

?>


Answer

Correct Answer:

Example

PHP



Note: This Question is unanswered, help us to find answer for this one

27.

In the following PHP 7.1 code snippet, commenting or removing which code line will result in the successful compilation of the code?

1.

2. namespace X\Y\Z;

3. class Exception extends \Exception {}

4. $x = new Exception()


5. $y = new \Exception('hi');

6. $z = new ArrayObject;

7. ?>

Answer

Correct Answer:

Line 6 


Note: This Question is unanswered, help us to find answer for this one

28. Which of the following statements is NOT correct about PHP 7.1 variables?

Answer

Correct Answer: Variable names are case-insensitive.

Note: This Question is unanswered, help us to find answer for this one

29. In PHP 7.1, which of the following options can be used as a default value for parameters that are declared as iterable?

Answer

Correct Answer: Null
Array

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

30.

What will be the output of the following PHP 7.1 code?

$o = (object) array( var_dump(isset($o-> {'5'}));

var_dump(key($o));

?>

Answer

Correct Answer:

bool(false)

int(5)



Note: This Question is unanswered, help us to find answer for this one

31.

Find the output of the following PHP 7.1 code.

var_dump(round(35/6));

?>


Answer

Correct Answer:

float (6)


Note: This Question is unanswered, help us to find answer for this one

32. Which of the following is the correct magical PHP constant that is used to determine the current line number of a file?

Answer

Correct Answer: _ _LINE_ _

Note: This Question is unanswered, help us to find answer for this one

33.

In PHP 7.1, which of the following statements is/are correct about generators?

i) With generators, less code is required to be written as compared to implementing an iterator class.

ii) The same generator can be iterated over multiple times.


Answer

Correct Answer:

Only option i). 


Note: This Question is unanswered, help us to find answer for this one

34. As an alternative to an existing list() syntax in PHP 7.1, which of the following options can be used to de-structure arrays for assignments (including within foreach)?

Answer

Correct Answer: ([])

Note: This Question is unanswered, help us to find answer for this one

35.

Find the output of the following code.

trait Example {

public function myFunction() {

echo }

}

class ABC {

public function myFunction() {

use Example;

echo 'Hello PHP!';

}

}

$obj = new ABC();

$obj->myFunction();

?>

Answer

Correct Answer:

The code will give a compilation error.


Note: This Question is unanswered, help us to find answer for this one

36.

Which of the following code snippets will print the output, Example?

Answer

Correct Answer:

namespace Example;

echo '?>


namespace Example;

echo '?>


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

37. While working in PHP 7.1, in which of the following types of syntax, a string is delimited with operator, <<
Answer

Correct Answer: In both a and b.

Note: This Question is unanswered, help us to find answer for this one

38.

What will be the output of the following PHP 7.1 code?

echo  echo 'This is an example: \n a newline';

?>

Answer

Correct Answer:

Mark told Nina that

Mark told Nina that This is an example:

a newline


Mark told Nina that a newline


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

39.

Find the output of the following PHP 7.1 code.

function multiple(int $x, int $y)

{ return $x * $y;

}

var_dump(multiple(3, 2));

var_dump(multiple(2.5, 5.5));

?>


Answer

Correct Answer:

int(6)

int(10)



Note: This Question is unanswered, help us to find answer for this one

40. In relation to PHP 7.1 method overloading, which of the following methods is triggered when inaccessible methods are invoked in an object context?

Answer

Correct Answer: __call()

Note: This Question is unanswered, help us to find answer for this one

41.

Find the output of the following PHP 7.1 program.

function double($test)

{

return $test*2;

}

$bar = $ar = 5;

$s = $ar++;

$exp = $dp = ++$bar;

$foo = double($dp++);

$gh = double(++$exp);

$res = $gh += 10;

print_r($res)

?>


Answer

Correct Answer:

24


Note: This Question is unanswered, help us to find answer for this one

42. What is the scope of a constant in PHP 7.1 ?

Answer

Correct Answer: Global

Note: This Question is unanswered, help us to find answer for this one

43.

Find the output of the following PHP 7.1 code snippet.

class Example {

public const ONE = private const TWO = 'two';

}

echo Example::ONE, PHP_EOL;

echo Example::TWO, PHP_EOL;

?>

Answer

Correct Answer:

two

Fatal error



Note: This Question is unanswered, help us to find answer for this one

44.

Which of the given code statements should replace ??? in the following PHP 7.1 code snippet, so that 7, 6, 5, 4, is printed as output?

function &example() {

$var = 8;

while ($var > 4) {

yield $var;

}

}

???

}

?>


Answer

Correct Answer:

foreach (example() as &$num) {

echo (--$num).', ';



Note: This Question is unanswered, help us to find answer for this one

45.

Which of the given statements is correct about the following PHP 7.1 code?

class exp {

public $b = <<< b

EXAMPLE;

}

?>

'>