0

Basic PHP Quiz 1

Description: Basic PHP Quiz 1
Number of Questions: 10
Created by:
Tags: php
Attempted 0/10 Correct 0 Score 0

What PHP stands for?

php
  1. Hypertext Preprocessor

  2. Pre Hypertext Processor

  3. Pre Hyper Processor

  4. Pre Hypertext Process


Correct Option: A

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

php
  1. It converts them to a common compatible data type and then compares the resulting values

  2. It returns True only if they are both of the same type and value

  3. If the two values are strings, it performs a lexical comparison

  4. It bases its comparison on the C strcmp function exclusively


Correct Option: B

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

php
  1. When the parameter is Boolean

  2. When the function is being declared as a member of a class

  3. When the parameter is being declared as passed by reference

  4. When the function contains only one parameter


Correct Option: C

Variables always start with a ........ in PHP

php
  1. Pond-sign

  2. Yen-sign

  3. Dollar-sign

  4. Euro-sign


Correct Option: C

AI Explanation

To answer this question, you need to understand how variables are declared in PHP.

In PHP, variables always start with a dollar sign ($) followed by the variable name. Therefore, the correct answer is C) Dollar-sign.

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
  1. 25

  2. 10

  3. -5

  4. 0


Correct Option: C

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:

{
$is_leap = (!($year %4) && (($year % 100) ||
!($year % 400)));
return $is_leap;
}

var_dump(is_leap(1987)); /* Displays false / var_dump(is_leap()); / Displays true */

php
  1. function is_leap($year = 2000)

  2. function is_leap($year default 2000)

  3. function is_leap($year)

  4. is_leap($year default 2000)


Correct Option: A

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

$myarray = array ("My String","Another String","Hi, Mom!");

php
  1. Using a for loop

  2. Using a foreach loop

  3. Using a while loop

  4. Using a do..while loop


Correct Option: A

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

if($a == 'a') {
  somefunction();
} else if ($a == 'b') {
  anotherfunction();
} else if ($a == 'c') {
  dosomething();
} else {
  donothing();
}

php
  1. A switch statement without a default case

  2. A recursive function call

  3. A switch statement using a default case

  4. A while statement


Correct Option: C
Explanation:

A series of if…else if code blocks checking for a single condition as above is a perfect place to use a switch statement

What will the following script output?

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

echo $s;

php
  1. A string of 50 random characters

  2. A string of 49 copies of the same character, because the random number generator

  3. A string of 49 random characters

  4. has not been initialized


Correct Option: C

What is the output of the following script?

$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;

php
  1. 128

  2. 42

  3. 256

  4. 342


Correct Option: C

AI Explanation

To find the output of the given script, let's go through each line of code:

$a = 10;
$b = 20;
$c = 4;
$d = 8;
$e = 1.0;

Here, we are assigning values to the variables $a, $b, $c, $d, and $e.

$f = $c + $d * 2;

In this line, $c is added to the result of multiplying $d by 2. $f is assigned the value of 4 + (8 * 2) = 4 + 16 = 20.

$g = $f % 20;

In this line, $g is assigned the remainder when $f is divided by 20. Since $f is equal to 20, the remainder is 0. Therefore, $g is assigned the value of 0.

$h = $b - $a + $c + 2;

In this line, $b is subtracted by $a, and $c is added to the result. Then, 2 is added to the result. $h is assigned the value of 20 - 10 + 4 + 2 = 16.

$i = $h &lt;&lt; $c;

In this line, $h is shifted left by $c bits. Since $c is equal to 4, $h is shifted left by 4 positions. Shifting a binary number left by n positions is equivalent to multiplying it by 2^n. So, $i is assigned the value of $h multiplied by 2^4. $i is assigned the value of 16 * 16 = 256.

$j = $i * $e;

In this line, $i is multiplied by $e. Since $i is equal to 256 and $e is equal to 1.0, $j is assigned the value of 256 * 1.0 = 256.

print $j;

Finally, the value of $j, which is 256, is printed as the output.

Therefore, the correct answer is C) 256.

- Hide questions