0% found this document useful (0 votes)
68 views4 pages

$a 5 $B 3 $B &$a $B $a $B They Are Both 25

This document contains a midterm exam for a COMP 170 course. It has 22 multiple choice and coding questions covering topics like variable scope, data types, arrays, regular expressions, functions, classes, and file I/O in PHP. The exam is worth a total of 40 marks and includes a reference page listing common PHP functions.

Uploaded by

Nahian Aziz
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)
68 views4 pages

$a 5 $B 3 $B &$a $B $a $B They Are Both 25

This document contains a midterm exam for a COMP 170 course. It has 22 multiple choice and coding questions covering topics like variable scope, data types, arrays, regular expressions, functions, classes, and file I/O in PHP. The exam is worth a total of 40 marks and includes a reference page listing common PHP functions.

Uploaded by

Nahian Aziz
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/ 4

COMP 170 - Midterm Exam

February 21, 2008


page 1 of 4

Name: _________________________

Coding questions are at the end. Function prototypes are on the last page. The number of marks allocated per
question appears in parentheses: total = 40.
1.

(2) Variable scope:


(a) Where are variables that are created inside of a function known?
(b) Where are variables that are created inside of a PHP script (but are not inside of a function) known?
a) Inside of the function from where they are declared, but not outside of the function
b) From where they are declared to the end of the PHP script, but not inside of functions.

2.

(1) What are the final values of $a and $b?


$a
$b
$b
$b

=
=
=
=

5;
3;
&$a;
$a * $b;

They are both 25

3.

(1) When should the === operator be used?


To check if two variables have the same value and the same type

4.

(1) What is the purpose of the isset(mixed var) function?


To check if a variable has been assigned a value (or has not been unset)

5.

(1) Compare when a for loop should be used vs. when a while loop should be used.
A for loop is used when you know the starting value and the number of iterations.
A while loop is used when the terminating condition is computed inside of the loop.

6.

(1) When inside of a loop, what is the difference between using break and continue?
break exits the loop and execution continues on the line following the loop
continue immediately starts the next iteration of the loop (but the $i++ in a for loop is executed!)

7.

(1) When should you use the flock() function?


To prevent corruption hen a file can be opened by multiple reader/writer processes. The file should be
locked with an exclusive lock when writing to a shared file.

8.

(1) Describe one distinct advantage and one distinct disadvantage of using associative arrays vs. using
indexed arrays.
associative arrays can be more intuitive since elements are accessed by name, e.g., $_POST[address]
associative arrays require more overhead to access than indexed arrays

COMP 170 - Midterm Exam


9.

February 21, 2008


page 2 of 4

Name: _________________________

(2) What is the formatted output of the following code?


$prices = array ( array (Cabbage => 0.27, Turnip => 0.75),
array (Banana => 0.58, Oranges => 1.23));
for ($k=0; $k < count($prices); $k++) {
foreach ($prices[k] as $key => $value) {
echo $key.'=>'.$value.'<br />';
}
echo "<br>";
Cabbage => 0.27
}
Turnip => 0.75
(blank line)
Banana => 0.58
Oranges => 1.23

10. (1) What is the purpose of the ksort() function?


To sort an associative array by keys
11. (2) Write a regular expression that matches any valid surname (allow hyphens, spaces, and periods).
^[A-Za-z\ \.-]+$
12. (2) Write a regular expression that matches any alphabetic string having at least two vowels.
^[A-Za-z]*[AEIOUaeiou][A-Za-z]*[AEIOUaeiou][A-Za-z]*$
13. (2) What are two practical techniques for reusing code in PHP?
Writing functions
Using classes with inheritance
14. (1) What is function overriding?
Providing a function with the same name as a function in the parent class
15. (1) Java has private, public, and protected. What access modifiers does PHP have?
PHP 4: none
PHP 5: private, public, protected
16. (2) Give a verbal example of some classes where polymorphism is used.
A parent class Shape that keeps track of the position, size, and colour.
Classes Triangle and Ellipse that inherit Shape and handle other functionality, such as drawing

17. (1) Is it a good idea for a subclass to declare an attribute having the same name as an attribute inside of the
class that was inherited? Why or why not?
No. It breaks the use of the attribute in the parent class and is inappropriate for object-oriented
programming

COMP 170 - Midterm Exam

February 21, 2008


page 3 of 4

Name: _________________________

18. (1) PHP is a weakly-typed language.


Write a short code example having an error that is due to weak typing.
$price = 1.83;
echo "Price is $prices";
// Spelling mistake caused a different variable to be used.
19. (2) Write the PHP code that will forward all of the variables that were posted to the current web page to the
next web page.
foreach ($_POST as $name => $value) {
echo "<input type=hidden name=$name value=$value>";
}

20. (2) Write an appropriate short example of a function that uses pass-by-reference.
function increment (&$a) {
++$a;
}
21. (2) Write a function product (x, y) that returns the product of two numbers. If either argument is not a valid
number, return false.
function product ($x, $y) {
if (!is_numeric($x) || !is_numeric($y) return false;
return $x * $y;
}

22. (4) Write the PHP code to open the file inventory.txt (containing 3 tab-separated items per line), read the
input line-by-line, and display the entire contents of this file on the web page. You do not need to write the
error-checking code for opening the file. The output format is unimportant.
@ $fp = fopen ("inventory.txt", "r");
// Normally, check $fp for NULL
while (count($item = fgetcsv($fp, 200, "\t")) == 3) {
echo "$item[0] $item[1] $item[2]<br>\n";
}

string gettype(mixed var);


bool settype (mixed var, string type);
bool is_array($v)
bool is_double(mixed var),
bool is_float(mixed var),
bool is_real(mixed var)
bool is_long(mixed var),
bool is_int(mixed var),
bool is_integer(mixed var)
bool is_string(mixed var)
bool is_object(mixed var)
bool isset(mixed var);
void unset(mixed var);
bool empty(mixed var);
int intval(mixed var);
float doubleval(mixed var);
string strval(mixed var);
int fopen(string name, string mode, [int includePath]);
int fwrite ( int fp, string string [, int length])
fclose(int fp);
feof(int fp)
string fgets(int fp, int length);
string fgetss(int fp, int length, string [allowed_tags]);
array fgetcsv ( int fp, int length
[, string delimiter [, string enclosure]])
int readfile(string filename, int [includePath]);
array file(int fp);
int file_get_contents(string fName, int [includePath]);
char fgetc(int fp);
string fread(int fp, int length);
bool file_exists(string name);
int filesize(string name);
unlink(string name);
rewind (int fp);
ftell(int fp);
int fseek ( int fp, int offset [, int whence])
bool flock(int fp, int operation [, int &wouldblock])
list( $product, $price ) = each( $prices );
reset($prices);
sort($prices);
asort($aprices);
ksort($aprices);
rsort($array)
arsort($array)
krsort($array)
usort($products, 'compareFunctionName');
uasort($aproducts, 'compareFunctionName');
shuffle($prices);
array_revers($array);
count($array);
array explode(string separator, string s [, int limit])

current( $array_name )
each( $array_name )
reset( $array_name )
end( $array_name )
next( $array_name )
prev( $array_name )
int array_walk(array arr, string func, [mixed userdata])
int array_count_values($array)
extract(array arry [, int extract_type] [, string prefix] )
bool mail(string to, string subject, string message,
string [additional_headers
[, string additional_parameters]]);
chop(string s)
ltrim(string s)
trim(string s)
nl2br(string s)
string sprintf (string format [, mixed args...])
void printf (string format [, mixed args...])
strtoupper(string s)
strtolower(string s)
ucfirst(string s)
ucwords(string s)
string AddSlashes(string s)
string StripSlashes(string s)
string implode(string delim, string, s)
string strtok(array a, string delim);
string substr(string string, int start[, int length] );
int strcmp(string str1, string str2);
int strcasecmp(string str1, string str2);
int strnatcmp(string str1, string str2);
int strnatcasecmp(string str1, string str2);
int strlen(string s);
string strstr(string haystack, string needle);
string stristr(string haystack, string needle);
string strchr(string haystack, string needle);
int strpos(string haystack, string needle, int [offset] );
int strrpos(string haystack, string needle, int [offset] );
mixed str_replace(mixed needle, mixed new_needle,
mixed haystack);
string substr_replace(string string, string replacement,
int start, int [length] )
int ereg(string pattern, string search, array [matches]);
int eregi(string pattern, string search, array [matches]);
string ereg_replace(string pattern, string replacement,
string search);
string eregi_replace(string pattern, string replacement,
string search);
array split(string pattern, string search, int [max]);
require(string filename);
include(string filename);

You might also like