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;
-
128
-
42
-
256
-
342
- $f = 4 + (8 * 2) = 20. 2. $g = 20 % 20 = 0. 3. $h = (20 - 10) + 4 + 2 = 16. 4. $i = 16 << 4. 16 is 00010000 binary; shifting left 4 yields 256 (100000000). 5. $j = 256 * 1.0 = 256.
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 << $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.