Вопросы: PHP Test online

1. Choose the selection that best matches the following statements:

PHP is a _____ scripting language based on the ____ engine. It is primarily used to develop dynamic _____ content, although it can be used to generate ____ documents (among others) as well.

2. Which of the following tags is not a valid way to begin and end a PHP code block?

3. Which of the following is not valid PHP code?

4. What is displayed when the following script is executed?

<?php
define(myvalue, "10");
$myarray[10] = "Dog";
$myarray[] = "Human";
$myarray[ myvalue ] = "Cat";
$myarray["Dog"] = "Cat";
print "The value is: ";
print $myarray[myvalue]." ";
?>

5. What is the difference between print() and echo()?

6. What is the output of the following script?

<?php
$a = 10;
$b = 20;
$c = 4;
$d = 8;
$e = 1.0;
$f = $c + $d * 2;
$g = $f % 20;
$h = $b - $a + $c + 2;
$i = $h << $c;
$j = $i * $e;
print $j;
?>

7. Which values should be assigned to the variables $a, $b and $c in order for the following script to display the string Hello, World!?

<?php
$string = "Hello, World!";
$a = ?;
$b = ?;
$c = ?;
if($a) {
if($b && !$c) {
echo "Goodbye Cruel World!";
} else if(!$b && !$c) {
echo "Nothing here";
}
} else {
if(!$b){
if(!$a && (!$b && $c)) {
echo "Hello, World!";
} else {
echo "Goodbye World!";
}
} else {
echo "Not quite.";
}
}
?>

8. What will the following script output?

<?php
$array = 0123456789ABCDEFG ;
$s = ;
for ($i = 1; $i < 50; $i++) {
$s .= $array[rand(0,strlen ($array) - 1)];
}
echo $s;
?>

9. Which language construct can best represent the following series of if conditionals?

<?php
if($a == a ) {
somefunction();
} else if ($a == b ) {
anotherfunction();
} else if ($a == c ) {
dosomething();
} else {
donothing();
}
?>

10. What is the best way to iterate through the $myarray array, assuming you want to modify the value of each element as you do?

<?php
$myarray = array ("My String",
"Another String",
"Hi, Mom!");
?>

11. Consider the following segment of code:

<?php
define("STOP_AT", 1024);
$result= array();
/* Missing code */
{
$result[] = $idx;
}
print_r($result);
?>

What should go in the marked segment to produce the following array output?

Array
{
[0] => 1
[1] => 2
[2] => 4
[3] => 8
[4] => 16
[5] => 32
[6] => 64
[7] => 128
[8] => 256
[9] => 512
}

12. Choose the appropriate function declaration for the user-defined function is_leap(). Assume that, if not otherwise defined, the is_leap function uses the year 2000 as a default value:

<?php
/* Function declaration here */
{
$is_leap = (!($year %4) && (($year % 100) ||
!($year % 400)));
return $is_leap;
}
var_dump(is_leap(1987)); /* Displays false */
var_dump(is_leap()); /* Displays true */
?>

13. What is the value displayed when the following is executed? Assume that the code was executed using the following URL:

testscript.php?c=25

<?php
function process($c, $d = 25)
{
global $e;
$retval = $c + $d - $_GET[ c ] - $e;
return $retval;
}
$e = 10;
echo process(5);
?>

14. Consider the following script:

<?php
function myfunction($a, $b = true)
{
if($a && !$b) {
echo "Hello, World! ";
}
}
$s = array(0 => "my",
1 => "call",
2 => $function ,
3 => ,
4 => "function",
5 => $a ,
6 => $b ,
7 => a ,
8 => b ,
9 => );
$a = true;
$b = false;
/* Group A */
$name = $s[?].$s[?].$s[?].$s[?].$s[?].$s[?];
/* Group B */
$name(${$s[?]}, ${$s[?]});
?>

Each ? in the above script represents an integer index against the $s array. In order to display the Hello, World! string when executed, what must the missing integer indexes be?

15. Run-time inclusion of a PHP script is performed using the ________ construct, while compile-time inclusion of PHP scripts is performed using the _______ construct.

16. Under what circumstance is it impossible to assign a default value to a parameter while declaring a function?

17. The ____ operator returns True if either of its operands can be evaluated as True, but not both.

18. How does the identity operator === compare two values?

19. Which of the following expressions multiply the value of the integer variable $a by 4?

20. How can a script come to a clean termination?

1. What is the construct used to define the blueprint of an object called?

2. At the end of the execution of the following script, which values will be stored in the $a->my_value array?

<?php
class my_class
{
var $my_value = array();
function my_class ($value)
{
$this->my_value[] = $value;
}
function set_value ($value)
{
$this->$my_value= $value;
}
}
$a = new my_class ( a );
$a->my_value[] = b ;
$a->set_value ( c );
$a->my_class( d );
?>

3. How can you write a class so that some of its properties cannot be accessed from outside its methods?

4. Which object-oriented pattern would you use to implement a class that must be instantiated only once for the entire lifespan of a script?

5. A class can be built as an extension of other classes using a process known as inheritance. In PHP, how many parents can a child class inherit from?

6. What OOP construct unavailable in PHP does the following script approximate?

<?php
class my_class
{
function my_funct ($my_param)
{
user_error ("Please define me", E_ERROR);
}
function b()
{
return 10;
}
}
?>

7. Assume that a class called testclass is defined. What must the name of its constructor method be?

8. How can a class override the default serialization mechanism for its objects?

9. In PHP 4, which object-oriented constructs from the following list are not available?
• Abstract classes
• Final classes
• Public, private, protected (PPP) methods
• Interfaces

10. How would you call the mymethod method of a class within the class itself?

11. What will the following script output?

<?php
class my_class
{
var $my_var;
function _my_class ($value)
{
$this->my_var = $value;
}
}
$a = new my_class (10);
echo $a->my_var;
?>

12. What will the following script output?

<?php
class my_class
{
var $value;
}
$a = new my_class;
$a->my_value = 5;
$b = $a;
$b->my_value = 10;
echo $a->my_value;
?>

13. Consider the following script. What will it output?

<?php
$global_obj = null;
class my_class
{
var $value;
function my_class()
{
global $global_obj;
$global_obj = &$this;
}
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
?>

14. Consider the following segment of PHP code. When it is executed, the string returned by the $eight_tenths->to_string method is 8 / 10 instead of the expected 4 / 5. Why?

<?php
class fraction {
var $numerator;
var $denominator;
function fraction($n, $d) {
$this->set_numerator($n);
$this->set_denominator($d);
}
function set_numerator($num){
$this->numerator= (int)$num;
}
function set_denominator($num) {
$this->denominator = (int)$num;
}
function to_string() {
return "{$this->numerator}
/ {$this->denominator}";
}
}
function gcd($a, $b) {
return ($b > 0) ? gcd($b, $a % $b) : $a;
}
function reduce_fraction($fraction) {
$gcd= gcd($fraction->numerator,
$fraction->denominator);
$fraction->numerator /= $gcd;
$fraction->denominator /= $gcd;
}
$eight_tenths = new fraction(8,10);
/* Reduce the fraction */
reduce_fraction($eight_tenths);
var_dump($eight_tenths->to_string());
?>

15. What does the following PHP code segment do?

<?php
require_once("myclass.php");
myclass::mymethod();
?>

16. Do static class variables exist in PHP?

17. What will the following script output?

<?php
class a
{
function a ($x = 1)
{
$this->myvar= $x;
}
}
class b extends a
{
var $myvar;
function b ($x = 2)
{
$this->myvar= $x;
parent::a();
}
}
$obj = new b;
echo $obj->myvar;
?>

18. How can you load classes on demand as they are required by the interpreter?

19. _____________________ are used to provide high-quality solutions to a recurrent design problem using object-oriented programming.

20. What will the following script output?

<?php
class a
{
function a()
{
echo Parent called ;
}
}
class b
{
function b()
{
}
}
$c = new b();
?>

  • 1