0% found this document useful (0 votes)
29 views3 pages

PHP Mid Term Completed

Mid Term for Winter 2024

Uploaded by

iemw0508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

PHP Mid Term Completed

Mid Term for Winter 2024

Uploaded by

iemw0508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP 1006 Midterm Fall 2023

1) (10 marks) Write a function named abs_val that returns the absolute value of an int
passed to it by value. You cannot use the abs() function library.

<?php

function abs_val($num) {
if ($num >= 0) { // if greater than or equal to 0 returns the number, if it's less
//than zero returns the negative of the number, making it positive and returning it
return $num;
} else {
return -$num;
}
}

// How it works
$number = -5;
echo "The absolute value of $number is " . abs_val($number);

?>

2) (10 marks) Write a rangeSum function which

 Prompt the user for two positive integers a and b. (asks for input)

 Write a separate function, rangeSum to compute the sum of the consecutive


integers from a to b inclusive (that includes both a and b).
 The rangeSum function should only calculate the sum. It should print a, b and sum.
 Example: a = 5, b = 7. Output is a is 5, b is 7, sum is 18.
<?php

$positiveOne = readline("Enter a first Positive Integer: ");


$positiveTwo = readline("\nEnter a second Positive Integer: ");
function rangeSum($a,$b){
echo "a = $a\n";
echo "b = $b\n";
echo "a + b = ";
echo ($a + $b);
}

rangeSum($positiveOne, $positiveTwo);

?>

3) Short answers and theoretical questions (2 marks each)


a) What initial value should be assigned to b at line 6 so that the output of this program is 10?

<?php
$sum=0; $n=1;
$b;

b=5;
while(n<b)
{
$sum += $n;
$n += 1;
}
echo $sum;

?>
3b) What is the function prototype to declare a reference to the int
"numPlayers"?

<?php
function functionName(&$numPlayers) { //int $numPlayers as a parameter
// Function body
}
?>

3c) What is the purpose of a reference in PhP and when would you use it?
(Describe with an example)

References in PHP let you access the same content of a variable by a different
name. Ex:

<?php
$greeting = "hello";
$ref1 =& $greeting;
echo "$ref1\n"; // Outputs: 'hello' referencing $greeting
echo $greeting;
?>

3d) What is the difference between an Array and Arraylist?

Arrays can store primitive data types and objects, whereas ArrayLists can
contain only object elements. With arrays you can’t use generics while ArrayList
allows use of generics to ensure type safety.

3e) What type of programming language is PhP. What is the difference between
a server-side language and a client-side language?

PHP is a server-side scripting language that is open sourced and used by


many developers for web development; it is also a general-purpose
language so it can be used for an array of different projects.

The difference between client-side languages and server-side languages


are that the client-side code is run on the device that is accessing the site;
ie. the javascript code the site is using runs on the phone that’s accessing
the website. The server-side is the opposite, the scripting from the server
side is run on the back-end server, it’s done this way to give dynamic
content to web pages for the client device.

You might also like