Past Papers, UOS Main Campus, Web System and Technologies

Web System & Technologies Past Paper 2018 – BSCS 6th Term UOS (CS-3548)

ProgramBSCS
Term6th
Year2018
Course codeCS-3548
AnswersSolved ✓

University of Sargodha BS Computer Science 6th term examination 2018, Web System & Technologies (CS-3548) paper (80 marks, 2:30 hours) – the PHP and MySQL paper. The paper is retyped by Shining Study from a student’s copy (marks, names and roll numbers removed); all answers are written by Shining Study for revision. The top line of the header is cut in the copy we have, so the year (2018) is taken from the same set of papers it came with. Solved answers for all 16 PHP short questions and the subjective questions: creating a MySQL user, EXPLAIN, connecting PHP to MySQL, htmlentities(), createElement/appendChild and SELECT DISTINCT vs GROUP BY. PHP and SQL examples were tested.

Solved answers

Q1 – Short answers (16 × 2)

i. $variable = 1 vs $variable == 1. = is the assignment operator (puts 1 into $variable); == is the comparison operator (checks whether $variable equals 1 and returns TRUE or FALSE).

ii. Spaces in variable names? No. PHP variable names can contain letters, digits and underscores only, and must start with a letter or underscore after $. Use $user_name or $userName instead.

iii. ++$j vs $j++. ++$j (pre-increment) adds 1 first and then returns the new value; $j++ (post-increment) returns the current value and then adds 1. With $j = 5: echo ++$j prints 6; then echo $j++ prints 6 and $j becomes 7.

iv. Are && and and interchangeable? They do the same logical AND, but and has lower precedence than =. So $a = true and false; sets $a to true, while $a = true && false; sets $a to false. Use brackets or && to be safe.

v. Multiline echo or assignment. Use the heredoc syntax <<<_END … _END; (or a normal string with line breaks):

$text = <<<_END
Line one
Line two
_END;
echo $text;

vi. Variable accessible to all parts of a program. Declare it as global inside functions (global $count;) or use the $GLOBALS array ($GLOBALS[‘count’]). A constant made with define() is also visible everywhere.

vii. Underlying values of TRUE and FALSE. TRUE has the value 1 and FALSE is empty/0. echo TRUE prints 1 and echo FALSE prints an empty string (NULL/“”).

viii. Three conditional statement types. if (with else/elseif), switch, and the ternary operator ?: .

ix. Skip the current loop iteration. continue; – e.g. for($i=1;$i<=5;$i++){ if($i==3) continue; echo $i; } prints 1245.

x. Initialising code when an object is created. Write a constructor: a method named __construct() inside the class. It runs automatically on new ClassName().

xi. Number of elements in an array. count($array) (or its alias sizeof()); count($array, COUNT_RECURSIVE) counts nested elements too.

xii. Purpose of explode(). It splits a string into an array using a separator: explode(” “, “Shining Study Past Papers”) gives [“Shining”, “Study”, “Past”, “Papers”].

xiii. Unix timestamp for 7:11 AM on May 2, 2016. mktime(7, 11, 0, 5, 2, 2016); (order: hour, minute, second, month, day, year). The value depends on the time zone: in UTC it is 1462173060; in Pakistan (Asia/Karachi) it is 1462155060.

xiv. Function that runs system commands. exec() (also system(), shell_exec(), passthru() or backticks).

xv. Delete the file file.txt. unlink(‘file.txt’);

xvi. Multidimensional array. An array whose elements are arrays: $m = array(array(1,2,3), array(‘a’=>’x’, ‘b’=>’y’)); or $m = [[1,2,3],[4,5,6]]; Access with $m[0][1].

Q2 – Creating a MySQL user

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'newpass';
GRANT ALL PRIVILEGES ON newdatabase.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

In older MySQL (5.x, as in many textbooks) one statement does both: GRANT ALL ON newdatabase.* TO ‘newuser’@’localhost’ IDENTIFIED BY ‘newpass’;. newdatabase.* means all tables of that database; localhost means the user can connect only from the same machine. Check with SHOW GRANTS FOR ‘newuser’@’localhost’;

Q3 – Examining how a query will work: EXPLAIN

MySQL provides the EXPLAIN command (also DESCRIBE/EXPLAIN ANALYZE in newer versions). Put it before a SELECT to see the execution plan without changing data:

EXPLAIN SELECT * FROM students WHERE roll_no = 25;

It shows, for each table: select_type, table, type (join/access type: ALL = full scan, ref, eq_ref, const), possible_keys and key (indexes that can be / are used), key_len, ref, rows (estimated rows to examine) and Extra (e.g. Using where, Using filesort, Using temporary). From this you can decide to add indexes or rewrite slow queries.

Q4 – Standard PHP function for connecting to MySQL

Older PHP used mysql_connect() (removed in PHP 7). Today the standard ways are mysqli_connect() / new mysqli() and PDO:

<?php
$conn = mysqli_connect('localhost', 'newuser', 'newpass', 'newdatabase');
if (!$conn) {
    die('Connection failed: ' . mysqli_connect_error());
}
echo 'Connected successfully';
$result = mysqli_query($conn, 'SELECT name FROM students');
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . '<br>';
}
mysqli_close($conn);
?>

PDO version: $pdo = new PDO(‘mysql:host=localhost;dbname=newdatabase’, ‘newuser’, ‘newpass’); – works with many databases and supports prepared statements.

Q5 – Showing HTML without it being interpreted

htmlentities() (or htmlspecialchars()) converts special characters into HTML entities, so the browser shows the code as text instead of running it:

echo htmlentities('<b>Hi</b> & bye');
// output sent to browser: &lt;b&gt;Hi&lt;/b&gt; &amp; bye
// browser displays: <b>Hi</b> & bye

This is also the main protection against XSS (cross-site scripting) when printing user input.

Q6 – Creating and appending DOM elements in JavaScript

document.createElement() creates a new element and appendChild() (or append()) adds it to the DOM:

<ul id="list"></ul>
<script>
  var li = document.createElement('li');      // create
  li.textContent = 'New item';                // add text (or createTextNode)
  document.getElementById('list').appendChild(li);  // append to the DOM
</script>

Related: insertBefore() to add at a position, removeChild() to delete, document.createTextNode() for a text node.

Q7 – SELECT DISTINCT vs GROUP BY

SELECT DISTINCTGROUP BY
Removes duplicate rows from the result.Groups rows with the same value into one group.
Used only to show unique values.Used mainly with aggregate functions: COUNT, SUM, AVG, MIN, MAX.
Cannot calculate per-group totals.Gives one row per group with totals/counts.
No HAVING clause.Groups can be filtered with HAVING.
Result order not guaranteed.Older MySQL sorted by the grouped column (not guaranteed in MySQL 8; use ORDER BY).

Example table orders(city, amount) with rows (Lahore 100), (Lahore 200), (Sargodha 50), (Karachi 70), (Sargodha 30):

SELECT DISTINCT city FROM orders;
-- Karachi, Lahore, Sargodha

SELECT city, COUNT(*), SUM(amount) FROM orders GROUP BY city;
-- Karachi 1 70 | Lahore 2 300 | Sargodha 2 80

SELECT city, SUM(amount) FROM orders GROUP BY city HAVING SUM(amount) > 100;
-- Lahore 300

So for just listing unique values both give the same rows, but only GROUP BY can summarise each group.

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)
Have another year of Web System and Technologies?

Upload a photo in 10 seconds — no form to fill. We clean it, solve it and credit you.

Missing years for this subject
202420232022

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.