You are on page 1of 9

BIT 2207

W1-2-60-1-6
JOMO KENYATTA UNIVERSITY
OF
AGRICULTURE AND TECHNOLOGY
University Examinations 2016/2017
THIRD YEAR FIRST SEMESTER EXAMINATION FOR THE DEGREE
OF BACHELOR OF SCIENCE IN INFORMATION TECHNOLOGY
BIT 2207: WEB DESIGN AND DEVELOPMENT II
DATE: DECEMBER 2016 TIME: 2 HOURS
INSTRUCTIONS: ANSWER QUESTION ONE AND ANY OTHER TWO
QUESTIONS

Question One (30 Marks)

a) State the function of each of the following three languages all web developers must
learn: (6 Marks)

(i) HTML

(ii) CSS

(iii) Javascript

b) Explain the term javascript function. (2 Marks)

c) With suitable examples, show where a javascript is positioned within a HTML page.
(4 Marks)

d) State four (4) methods of displaying data in javascript. (4 Marks)

e) Explain the functions of PHP. (4 Marks)

f) Write the basic PHP syntax. (2 Marks)

g) Given a form has three fields:


FirstName, Last Name and Age. Write a non-validated PHP script that can post the
three form content to a database. (6 Marks)
1
BIT 2207

h) Differentiate between GET and POST methods. (2 Marks)

Question Two (20 Marks)

a) Write a PHP script to check if a string contains specific string. (4 Marks)

Sample string: ‘The quick brown fox jumps over the lazy dog’.
Check whether the said string contains the sting ‘jumps’.

b) Explain the behaviour of the html(specialchars) function. (3 Marks)

c) Describe the actions of the $_SERVER [“PHP_SELF”]. (3 Marks)

d) Write a PHP script that selects the id, firstname and lastname columns from the
students table and displays it on the page as follows: (10 Marks)
id: 1 – Name: John Kamau
id: 2 – Name: Mary Mwajuma
id: 3_ Name: Julie Odogo

Question Three (20 Marks)

a) Explain the following PHP terms: (6 Marks)

(i) Polymorphism

(ii) Data Abstraction

(iii) Encapsulation

b) Below is a PHP code extract which defines a class of type Books:

< ? Php
Class Books {
/*Member variables*/
Var & price;
Var & title;

/*Member functions*/
function set Price ($ Par) {
$ this - > price = $ par;
}
function get Price ( ){
echo & this - > price. “<br/>”;
}
function set Title (& Par) {
& this - > title = & par ;

2
BIT 2207

}
function get Title ( ) {
echo & this - > tittle . “<br/>”;
}
}
?>

(i) Explain five (5) parts of the code. (5 Marks)

(ii) Demonstrate how to create three books Maths, Physics and Chemistry
using the new operations. (3 Marks)

(iii) Show how to set title and prices of the three books by calling member
functions. (6 Marks)

Question Four (20 Marks)

a) The javascript function in the program has an error find out the error and fix it.
(6 Marks)

< ! DOCTYPE html>


< html >
< body >

<P id = “demo” > < / P>

<script>
func MyFunc {
document . getElementById (“clemo”)
innerHTML = “Hello World!” ;
}
MyFunction ( )
</script>
</body>
</html>

b) Write a function in javascript to display the product of 5 * 5 (4 Marks)

c) (i) State the function of a switch statement. (2 Marks)

(ii) Write the syntax for the switch statement. (2 Marks)

(iii) The getDay ( ) method returns the weekday as a number between 0 and 6.
(Sunday = 0, Monday = 1, Tuesday = 2 ……) use the weekday number to
calculate weekday name: (use switch statement). (6 Marks)

3
BIT 2207

Question Five (20 Marks)

a) Explain the term object in javascript. (2 Marks)

b) State three (3) ways to create a new object. (3 Marks)

c) Write a javascript function to convert degrees Celsius to degrees Fahrenheit. The


output should be presented as shown below: (15 Marks)

Insert a number into one of the input fields

below:
Degrees Celsius

equals

Degrees Fahrenheit

4
BIT 2207

Functions of the languages: (i) HTML: Used for structuring the content on web pages. (ii)
CSS: Used for styling and layout of web pages. (iii) Javascript: Used for adding
interactivity and dynamic features to web pages.
b) Javascript function: A JavaScript function is a block of reusable code designed to perform a
particular task.
c) JavaScript within an HTML page:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
// Your JavaScript code here
</script>
</head>
<body>
<!-- Your HTML content here -->
</body>
</html>

d) Methods to display data in Javascript:


1. console.log(): Logs the data to the browser console.
2. alert(): Shows the data in an alert dialog box.
3. document.write(): Writes the data directly to the HTML document.
4. innerHTML: Updates the content of HTML elements.
e) Functions of PHP:
1. Server-side scripting.
2. Dynamic content generation.
3. Database interaction.
4. File handling and manipulation.
f) Basic PHP syntax:
<?php
// PHP code goes here
?>

g) PHP script to post form content to a database:


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password,


$dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

5
BIT 2207

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstName = $_POST['FirstName'];
$lastName = $_POST['LastName'];
$age = $_POST['Age'];

$sql = "INSERT INTO tablename (FirstName, LastName, Age)


VALUES ('$firstName', '$lastName', '$age')";

if ($conn->query($sql) === TRUE) {


echo "Data inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

$conn->close();
?>

h) Difference between GET and POST: GET:


• Data is sent as URL parameters.
• Limited amount of data can be sent.
• Less secure for sensitive data.
POST:
• Data is sent in the body of the HTTP request.
• Can send large amounts of data.
• More secure as data is not visible in the URL.
Question Two
a) PHP script to check if a string contains a specific string:

<?php
$string = 'The quick brown fox jumps over the lazy dog';
$search = 'jumps';

if (strpos($string, $search) !== false) {


echo "String contains the search string.";
} else {
echo "String does not contain the search string.";
}
?>

b) Behaviour of htmlspecialchars function: The htmlspecialchars function converts special


characters to their corresponding HTML entities. This is useful to prevent potential
security vulnerabilities like cross-site scripting (XSS) attacks.
c) Actions of $_SERVER["PHP_SELF"]: $_SERVER["PHP_SELF"] returns the
filename of the currently executing script. It can be used for form actions to ensure the
form submits to the same page.
d) PHP script to display student data:
6
BIT 2207

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password,


$dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM students";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " .
$row["firstname"] . " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>

Question Three
a) Explanation of PHP terms: (i) Polymorphism: Allows objects of different classes to be
treated as objects of a common super class. (ii) Data Abstraction: Hides complex
implementation details and shows only the necessary features. (iii) Encapsulation:
Bundling of data and methods that operate on the data into a single unit.
b) PHP code extract explained: The given code defines a Books class with member variables
price and title and member functions setPrice, getPrice, setTitle, and getTitle.
c) Create three books:
$maths = new Books();
$physics = new Books();
$chemistry = new Books();
$maths->setTitle("Maths Book");
$maths->setPrice(100);

$physics->setTitle("Physics Book");
$physics->setPrice(150);

$chemistry->setTitle("Chemistry Book");
$chemistry->setPrice(120);

7
BIT 2207

Question Four
a) Fixed JavaScript code:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
function myFunc() {
document.getElementById("demo").innerHTML = "Hello
World!";
}
myFunc();
</script>
</body>
</html>

b) JavaScript function to display product:


function displayProduct() {
var product = 5 * 5;
console.log(product);
}
displayProduct();

c) (i) Function of a switch statement: Allows multiple conditions to be tested. (ii) Syntax:
switch(expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
(iii) Calculate weekday name:
var day = new Date().getDay();
var weekday;

switch(day) {
case 0:
weekday = "Sunday";
break;
case 1:
weekday = "Monday";
break;
// ... Continue for other days
}
console.log(weekday);
8
BIT 2207

Question Five
a) Object in JavaScript: An instance of a class, which can have properties and methods.
b) Ways to create a new object:
1. Object literal: var obj = { key: value };
2. Constructor function: function ObjectName() { this.key = value; } var obj = new
ObjectName();
3. Object.create(): var obj = Object.create(proto);
c) JavaScript function to convert Celsius to Fahrenheit:

function celsiusToFahrenheit(celsius) {
var fahrenheit = (celsius * 9/5) + 32;
return fahrenheit;
}

document.getElementById("celsiusInput").addEventListener("inpu
t", function() {
var celsius = parseFloat(this.value);
var fahrenheit = celsiusToFahrenheit(celsius);
document.getElementById("fahrenheitOutput").value =
fahrenheit.toFixed(2);
});

You might also like