CAT OF PHP
COURSE TITLE: DEVELOP WEB APPLICATION USING PHP
COURSE CODE: SFPWP302
CLASS: YEAR 1(A, B, C, D)
NUMBER OF TEACHING HOURS/WEEK: 2
INSTRUCTOR: Olivier HABUMUGISHA
Date: 1/03/2024
Duration: 2Hours
Maximum Marks: 40
INSTRUCTIONS:
-Attempt all questions.
- Make sure the handwriting is readable and Underline after each question.
1
QUESTION ONE: Choose the correct answer(s). [10Marks]
1. What is the difference between Indexed array and Associative array in PHP?
A. Index array has numeric index while associative array has named keys
B. Index array has numeric index while associative array has named keys
and numeric index both
C. Index array is one-dimensional array with numeric index while
associative array is two-dimensional array with numeric index
D. Index array has numeric index while associative array has one or more
arrays
Answer: A) Index array has numeric index while associative array has
named keys
2. Which PHP function(s) is/are used to compare arrays and returns the
differences?
A. array_diff()
B. array_diff_assoc()
C. array_diff_key()
D. All of the above
Answer: D) All of the above
3. Which PHP function is used to sort multi-dimensional arrays?
A. array_sort()
B. sort()
C. multisort()
D. array_multisort()
Answer: D) array_multisort()
4. What is the use of PHP sort() function?
A. Sorts an indexed array
B. Sorts an associative array
C. Sorts a multi-dimensional array
D. Sorts any kind of array
Answer: A) Sorts an indexed array
2
5. Which PHP function is used to sort an indexed array in descending order?
A. sort_reverse()
B. reverse_sort()
C. revsort()
D. rsort()
Answer: D) rsort()
6. Which PHP function is used to return the current element in an array?
A. get()
B. start()
C. current()
D. cur()
Answer: C) current()
7. Which PHP function is used to check if a specified value exists in an array?
A. in_array()
B. check_array()
C. exist()
D. None of the above
Answer: A) in_array()
8. Which PHP function is used to sort an associative array in descending
order, according to the key?
A. sort()
B. rsort()
C. ksort()
D. krsort()
Answer: D) krsort()
9. Which PHP global variable is used to collect data after submitting an HTML
form?
A. $_GET
B. $_REQUEST
C. $_POST
D. $_ENV
3
Answer: B) $_REQUEST
10. Which PHP function is used to open a file?
A. fopen()
B. open()
C. open_file()
D. PHP_open()
Answer: A) fopen()
QUESTION TWO: [4Marks]
What will be the output of the following PHP code.
Answer:
QUESTION THREE: [4Marks]
Write a PHP script to display the following patterns.
4
Answers:
<?php
$n = 5;
for ($i = 1; $i <= $n; ++$i) {
for ($j = 1; $j <= $i; ++$j) {
echo ' * ';
}
echo '<br>';
}
for ($i = $n; $i >= 1; --$i) {
for ($j = 1; $j <= $i; ++$j) {
echo ' * ';
}
echo '<br> ';
}
?>
QUESTION FOUR: [6Marks]
Write a PHP script that creates the following table.
5
Answers:
<html>
<body>
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
<!-- cell 270px wide (8 columns x 60px) -->
<?php
for($row=1;$row<=10;$row++)
{
echo "<tr>";
for($col=1;$col<=10;$col++)
{
echo "<td>" .($col * $row). "</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>
QUESTION Five: [8Marks]
Write a PHP Script to record 10 marks of the students in array then compute it
average and display first five lowest marks of students.
Hint:
Answers:
<?php
$marks= array(78, 60, 62, 68, 71, 68, 73, 85, 66, 64);
$tot_mark = 0;
$array_length = count($marks);
Foreach($marks as $temp)
{
$tot_mark += $temp;
6
}
$avg_marks = $tot_mark/$array_length;
echo "Average marks= ".$avg_marks."<br>";
sort($marks);
echo " List of five lowest marks of student :";
for ($i=0; $i< 5; $i++)
{
echo $marks [$i].", ";
}
?>
QUESTION SIX: [8Marks]
Write a PHP Program for finding the biggest number in an array without using
any array functions. Hint: $numbers = array (12,23,45,20,5,6,34,17,9,56,999);
Answers:
<?php
$numbers = array(12,23,45,20,5,6,34,17,9,56,999);
$length = count($numbers);
$max = $numbers[0];
for($i=1;$i<$length;$i++)
{
if($numbers[$i]>$max)
{
$max=$numbers[$i];
}
}
echo "The biggest number is ".$max;
?>
---------------------Good Luck------------------------------------------------------------