math 275

10
1 | Page Faculty Of Computer Studies M275 Web Development using PHP and MySQL Midterm Examination- Solution Fall 2012-2013 Date November 21, 2012 ( 90) Minutes Time llowed: Number of Exam Pages: 10 Instructions: This exam consists of 4 parts [ Total is 60 Marks ] 1. Part A [ Total marks available 6 ] consists of a set of multiple choice questions all of which must be answered 2. Part B [ Total marks available 12 ] The student should answer any two of the questions 3. Part C [Total marks available 12 ] The student should answer any three of the questions 4. Part D [ Total marks available 30 ] The student should answer all questions.

Upload: nahian-aziz

Post on 14-Apr-2016

216 views

Category:

Documents


0 download

DESCRIPTION

Solution set

TRANSCRIPT

Page 1: Math 275

1 | P a g e

Faculty Of Computer Studies

M275

Web Development using PHP and MySQL

Midterm Examination- Solution Fall – 2012-2013

Date November 21, 2012 ( 90) Minutes Time llowed: Number of Exam Pages: 10

Instructions:

This exam consists of 4 parts [ Total is 60 Marks ]

1. Part A [ Total marks available 6 ] consists of a set of multiple choice questions all of which must

be answered

2. Part B [ Total marks available 12 ] The student should answer any two of the questions

3. Part C [Total marks available 12 ] The student should answer any three of the questions

4. Part D [ Total marks available 30 ] The student should answer all questions.

Page 2: Math 275

2 | P a g e

PART A [ 6 Marks ] : Choose the correct answer [ 1 Mark for each question ] :

1) PHP comments for a single line have the following syntax:

a) /* comments /*

b) #comment

c) // comment

d) :: comment

e) A&B

2) Which of the following functions are used by PHP to find out what type a variable is?

a) gettype()

b) is_double()

c) get_type()

d) is_date()

e) A&B

3) ________ enables you to incorporate files into your PHP documents.

a) include()

b) is_executable().

c) filesize()

d) None .

4) What library do you need in order to process images?

a) c-client library

b) GIF/PNG library

c) Image Library

d) GD library

5) What is the correct way to create a function in PHP?

a) function myFunction()

b) new_function myFunction()

c) create myFunction()

6) A _constructor in PHP concept is a special kind of…

a) Class

b) Method

c) Object

Page 3: Math 275

3 | P a g e

d) Variable

Part 2 [ 12 Marks ] : Answer ONLY 2 questions [ 6 Marks for each ] :

1) a. What does a DNS term stand for? Define and explain why DNS is being used ?

b. Explain how The ServerName directive enables you to define the name the server will report in

any self-referencing URLs on Linux/Unix ? [ 6 Marks ]

Answer :

a. The DNS : The Domain Name System (DNS) is a hierarchical distributed naming system

for computers, services, or any resource connected to the Internet or a private network.

The DNS system is used to associate IP addresses with domain names. The value of ServerName is

returned when the server generates a URL. If you are using a certain domain name, you must make

sure that it is included in your DNS system and will be available to clients visiting your site.

b. The directive accepts a DNS name and an optional port, separated by a colon. Make sure that

ServerName has a valid value. Otherwise, the server will not function properly; for example, it will

issue incorrect redirects.

On Linux/Unix platforms, you can use the User and Group directives to specify which user and

group IDs the server will run as. The nobody user is a good choice for most platforms. However,

there are problems in the HP-UX platform with this

user ID, so you must create and use a different user ID, such as www.

Note : This question is related to configuring the Apache ( installation chapters )

2) Explain the use of the character sequence <? in PHP and how it communicates with XML

parser ?. [6 marks ]

Answer :

The character sequence <? tells an XML parser to expect a processing instruction

and is therefore frequently included in XML documents. If you include XML in your

script and have short tags enabled, the PHP engine is likely to confuse XML processing

instructions and PHP start tags. Disable short tags if you intend to incorporate

XML in your document.

Note : This question is related to configuring the PHP ( installation chapters )

3) Take the following scenario of a relationship between students & classes relationship then

answer the questions after :

A student has an ID and a name. A class has an ID and a name. A student usually takes more than

one class at a time, and a class always contains more than one student, as you can see in the

following figure :

Page 4: Math 275

4 | P a g e

Explain how to make the theoretical relation to be many-to-many relationship? [6 Marks]

Answer :

Because many-to-many relationships are difficult to represent in an efficient database design, we need to create an intermediate table, one that sits between the two tables and essentially maps them together.

Note : This questions covers : Learning the Database

Design Process . Embeding MySql in PHP is covered in the same week of MTA, so it is not included

in the MTA.

4) What do superGlobals mean in PHP. Give five examples and explain the use of each .

[6 marks ]

Answer :

Superglobals — Superglobals are built-in variables that are always

available in all scopes

Description

Several predefined variables in PHP are "superglobals", which means they

are available in all scopes throughout a script. There is no need to do

global $variable; to access them within functions or methods.

Page 5: Math 275

5 | P a g e

Examples :

$_GET contains any variables provided to a script through the GET method.

. $_POST contains any variables provided to a script through the POST

method.

. $_COOKIE contains any variables provided to a script through a cookie.

. $_FILES contains any variables provided to a script through file uploads.

. $_SERVER contains information such as headers, file paths, and script

locations.

. $_ENV contains any variables provided to a script as part of the server

environment.

. $_REQUEST contains any variables provided to a script via any user input

mechanism.

. $_SESSION contains any variables that are currently registered in a session.

PART 3 [ Total 12 Marks ] : Answer Only 3 Questions [ 4 marks for each ] :

5) Consider the following code of PHP that defines two variables :

<?php

$GoodPassword = 'asdfg';

$password = 'asdf';

?>

Write a function that compares the two passwords and verify the match with a printed out message

to the user .

Answer :

if ($password == $GoodPassword){

print "<b>Password verified!</b>\n";

}

?>

6) Write a PHP script that defines an Array of string "2000-01-12" , and then breaks this string into

another array .

<?php

$start_date = "2000-01-12";

$date_array = explode ("-",$start_date);

?>

7) The following PHP script defines an array and initializes it as follows :

<?

$meals = array('A' => 1,

'B' => 4.95,

Page 6: Math 275

6 | P a g e

'C' => 3.00,

'D' => 6.50,

'E' => 0);

Complete the script so that it is able to locate the element with specific value ( 6.50)

Answer :

<?

$meals = array('A' => 1,

'B' => 4.95,

'C' => 3.00,

'D' => 6.50,

'E' => 0);

$dish = array_search(6.50, $meals);

if ($dish) {

print "$dish costs \$6.50";

}

?>

h)

8) State the output of the following PHP script :

<html>

<body>

<?php

$i=1;

do

{

$i++;

echo "The number is " . $i . "<br />";

}

while ($i<=5);

?>

</body>

</html>

Answer

The number is 2

The number is 3

The number is 4

The number is 5

The number is 6

Page 7: Math 275

7 | P a g e

Part 4 [ Total 30 Marks ] : Answer All questions

9) The simplest way to send an email with PHP is to send a text email.

a. Write a PHP script that first declares the variables ($to, $subject, $message, $from, $headers),

then use these variables in the mail() function to send an e-mail. [ 5 marks]

Answer :

<?php

$to = "[email protected]";

$subject = "Test mail";

$message = "Hello! This is a simple email message.";

$from = "[email protected]";

$headers = "From:" . $from;

mail($to,$subject,$message,$headers);

echo "Mail Sent.";

?>

b. Based on the previous part , write a PHP script that creates a feedback-form on a website. The

example below sends a text message to a specified e-mail address that works according to the

following specification [ 5 Marks ] :

First, check if the email input field is filled out

If it is not set (like when the page is first visited); output the HTML form

If it is set (after the form is filled out); send the email from the form

When submit is pressed after the form is filled out, the page reloads, sees that the email input

is set, and sends the email

Answer :

<html>

<body>

<?php

if (isset($_REQUEST['email']))

//if "email" is filled out, send email

{

//send email

$email = $_REQUEST['email'] ;

$subject = $_REQUEST['subject'] ;

$message = $_REQUEST['message'] ;

mail("[email protected]", $subject,

$message, "From:" . $email);

echo "Thank you for using our mail form";

}

else

//if "email" is not filled out, display the form

Page 8: Math 275

8 | P a g e

{

echo "<form method='post' action='mailform.php'>

Email: <input name='email' type='text' /><br />

Subject: <input name='subject' type='text' /><br />

Message:<br />

<textarea name='message' rows='15' cols='40'>

</textarea><br />

<input type='submit' />

</form>";

}

?>

</body>

</html>

c. Do you think the above script is secured ? Explain. [ 2 Marks]

Answer :

No , as it doesn't validate user input to make it more secure.

10) Write a recursive function in PHP that calculates and prints the factorial of a given number [ 5

Marks]

Hint : The equation of a factorial number can be represented like this: n! = n * ((n - 1)!) That is, the

factorial of any given number is equal to that number multiplied by the factorial of the number one

lower. For example, 4! = 4*3!.

Answer

<?php

function factorial($number) {

if ($number == 0) return 1;

return $number * factorial($number - 1);

}

print factorial(6);

?>

11) Answer the following [ 12 marks ]

Page 9: Math 275

9 | P a g e

a. Create a base class in PHP can be used to define an URL address , called Sites, with two

properties ("site" with private attribute, and "category" with public attribute), constructor, and a

public method, getPag(). The __construct() receives one argument and use it to set the value of

"$site" property. The getPag() method defines a URL address, with the two properties and its

argument, and return it. [ 5 Marks]

b. Create a subclass derived from Sites with a __construct() that has no instruction inside , and

another method, "getLink()" which returns a link (HTML <a> tag) with the URL defined in the base

class. The getLink() method uses the "category" property and the "getPag()" method declared in

parent class, as if it were defined in the subclass. [ 5 marks]

c. Explain how the _constructor in subclass works. [2 Marks]

<?php

// Sites class

class Sites {

// private and public properties

private $site;

public $category = 'php-mysql';

// Constructor

public function __construct($site) {

// if the $site argument is string type, with more than 3 characters,

assigns its value to $site property

// else, display a message and ends the script

if(is_string($site) && strlen($site)>3) $this->site = $site;

else exit('Invalid value for $site');

}

// getPag() - public

public function getPag($pag) {

// return a url address, with the two properies and its argument

$url = $this->site. '/'. $this->category. '/'. $pag;

return $url;

}

}

?>

<?php

include('class.Sites.php'); // Include parent class (Sites)

// child class, LinkSites, derived from Sites

class LinkSites extends Sites {

public function getLink($pag) {

// output an HTML link, using getPag() and "category" declared in parent

class

echo '<a href="http://'. $this->getPag($pag). '" title="'. $this->category.

'">Link</a><br />';

}

}

?>

We use the following script to test the child class (LinkSites). <?php

Page 10: Math 275

11 | P a g e

include('class.LinkSites.php'); // Include LinkSites subclass

// object instance of the LinkSites

$objLink = new LinkSites('http://coursesweb.net');

// calls getLink() (defined in child class)

$objLink->getLink('php-oop-inheritance-class-extends');

// change the value of the "category" (which is in the parent class)

$objLink->category = 'ajax';

// calls getPag() (which is in the parent class)

echo $objLink->getPag('xmlhttprequest-object');

?>

<?php

include('class.Sites.php'); // Include parent class (Site)

// child class, PagSites, derived from Sites

class PagSites extends Sites {

public $domain = 'marplo.net'; // Proprietate

// override the parent constructor

function __construct() {

// with no instructions, does nothing

}

// override getPag()

public function getPag($pag) {

// gets the result returned by the getPag() from the parent class

$url = parent::getPag($pag);

// add to $url the value of "$domain"

$url = $this->domain. $url;

return 'Page: '. $url;

}

}

?>

The __construct() declared in the subclass removes the effect of the constructor

from the parent class. Because here it is defined without parameter, when an

object instance of this subclass is created it's no need to add an argument.