0% found this document useful (0 votes)
2K views17 pages

Summer 23 22619

model answer of 22619 summer 23

Uploaded by

dingraritika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views17 pages

Summer 23 22619

model answer of 22619 summer 23

Uploaded by

dingraritika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

### Question 1: Attempt any FIVE of the following (10 Marks)

#### (a) State the advantages of PHP (any four).

**Marks: 2**

1. **Open Source**: PHP is free to use and distribute, making it accessible to a wide range of users.

2. **Cross-Platform**: PHP runs on various platforms, including Windows, Linux, and macOS.

3. **Database Integration**: PHP easily integrates with databases like MySQL, PostgreSQL, and
Oracle.

4. **Easy Learning Curve**: PHP is simple to learn and use, especially for those familiar with HTML.

#### (b) State the use of strlen() & strrev().

**Marks: 2**

- **strlen()**: This function returns the length of a given string.

Syntax: strlen($string);

- **strrev()**: This function reverses a given string.

Syntax: new_string = strrev($old_string);

#### (c) Define introspection.

**Marks: 2**

Introspection is the ability of a program to examine an object's characteristics, such as its name,
parent class (if any), properties, and methods. With introspection, we can write code that operates
on any class or object. We don't need to know which methods or properties are defined when we
write code; instead, we can discover that information at runtime, which makes it possible for us to
write generic debuggers, serializers, profilers, etc.

#### (d) Enlist the attributes of cookies.

**Marks: 2**

1. **Name**: The name of the cookie.

2. **Value**: The value stored in the cookie.

3. **Expiry**: The time when the cookie expires.

4. **Path**: The path on the server where the cookie is valid.

#### (e) Write syntax of constructing PHP webpage with MySQL.

**Marks: 2**

```php

<?php

$servername = "localhost";
$username = "username";

$password = "password";

$dbname = "database";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

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

$conn->close();

?>

```

#### (f) Define GET & POST methods.

**Marks: 2**

- **GET Method**: Sends data via the URL. Limited by URL length and less secure as data is visible in
the URL.

- **POST Method**: Sends data via the HTTP request body. More secure and allows for larger
amounts of data.

#### (g) State the use of “$” sign in PHP.

**Marks: 2**

The `$` sign is used to declare variables in PHP. It is a symbol to signify that what follows is a variable
name.

---

### Question 2: Attempt any THREE of the following (12 Marks)

#### (a) Write a program using do-while loop.

**Marks: 4**

```php

<?php

$i = 1;
do {

echo "The number is: $i <br>";

$i++;

} while ($i <= 5);

?>

```

#### (b) Explain associative and multi-dimensional arrays.

**Marks: 4**

- **Associative Array**: An associative array in PHP is an array where the keys are
named (or strings) rather than numeric. This allows for more meaningful keys
that describe the values they are associated with, making the code more
readable and understandable.

<?php

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

echo "Peter is " . $age['Peter'] . " years old.";

?>-

**Multi-Dimensional Array**: A multi-dimensional array in PHP is an array that


contains one or more arrays. These arrays can be used to represent more
complex data structures, such as tables or matrices.

```php

$cars = array (

array("Volvo",22,18),

array("BMW",15,13),

array("Saab",5,2),

array("Land Rover",17,15)

);

echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";

```

#### (c) Define serialization and explain it with example.

**Marks: 4**

Serialization is the process of converting an object into a string representation that can be easily
stored or transmitted. The opposite process is called deserialization.

Example:
```php

<?php

$myArray = array("Apple", "Banana", "Cherry");

$serializedArray = serialize($myArray);

echo $serializedArray; // Outputs: a:3:{i:0;s:5:"Apple";i:1;s:6:"Banana";i:2;s:6:"Cherry";}

$unserializedArray = unserialize($serializedArray);

print_r($unserializedArray); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry )

?>

```

#### (d) Describe the procedure of sending e-mail.

**Marks: 4**

### Procedure of Sending an Email in PHP

Sending an email in PHP involves several steps. Here's a detailed explanation of each step involved:

1. **Set the Recipient Email Address**:

- The recipient's email address is specified as a string.

- This is the email address to which the email will be sent.

```php

$to = "recipient@example.com";

```

2. **Set the Subject of the Email**:

- The subject of the email is a short description of the email content.

- It gives the recipient an idea of what the email is about.

```php

$subject = "Test email";

```

3. **Set the Message Body**:

- The message body contains the actual content of the email.

- It can be plain text or HTML formatted text if specified in the headers.

```php

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

```
4. **Set the Headers (Optional)**:

- Headers provide additional information about the email, such as the sender's email address, CC,
BCC, and MIME types for HTML emails.

- Common headers include `From`, `Reply-To`, and `Content-Type`.

```php

$headers = "From: sender@example.com";

```

5. **Send the Email**:

- The `mail` function is used to send the email.

- It takes up to four parameters: recipient address, subject, message body, and headers.

```php

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

```

Here is a complete example that combines all the steps to send an email in PHP:

```php

<?php

// Step 1: Set the recipient email address

$to = "recipient@example.com";

// Step 2: Set the subject of the email

$subject = "Test email";

// Step 3: Set the message body

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

// Step 4: Set the headers (optional)

$headers = "From: sender@example.com";

// Step 5: Send the email

if(mail($to, $subject, $message, $headers)) {

echo "Email sent successfully to $to";

} else {

echo "Failed to send email.";

?>
### Question 3: Attempt any THREE of the following (12 Marks)

#### (a) Differentiate between implode and explode functions.

**Marks: 4**

Implode explode
Joins array elements into a single string Splits a string into an array
implode(separator, array): string explode(delimiter, string): array
To create a single string from array To break a string into an array of
elements substrings
Separator string used to join array Delimiter string used to split the input
elements string
Input array containing elements to join Input string to be split
Returns a single concatenated string Returns an array of strings

#### (b) Explain the concept of cloning of an object.

**Marks: 4**

Cloning an object in PHP means creating a copy of an existing object. This is done using the `clone`
keyword. When an object is cloned, the `__clone()` method, if defined, is called to handle the cloning
process.

Example:

```php

<?php

class student {

function getdata($nm,$rn) {

$this->name=$nm;

$this->rollno=$rn;

function display() {

echo "<br>name = ".$this->name;

echo "<Br>rollno = ".$this->rollno;

$s1 = new student();

$s1->getdata("abc",1);

$s1->display();

$s2 = clone $s1;


echo "<br> Cloned object data ";

$s2->display();

?>

```

#### (c) Describe the procedure of validation of web page.

**Marks: 4**

Refer summer 22

```

#### (d) Write update & delete operations on table data.

**Marks: 4**

- **Update Operation**: Update command is used to change / update new value for field in row of
table. It updates the value in row that satisfy the criteria given in query

```php

$conn = new mysqli("localhost", "username", "password", "database");

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

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

echo "Record updated successfully";

} else {

echo "Error updating record: " . $conn->error;

$conn->close();

```

- **Delete Operation**: Delete command is used to delete rows that are no longer required from the
database tables. It deletes the whole row from the table. If the WHERE clause is not used, all records
will be deleted.

```php

$conn = new mysqli("localhost", "username", "password", "database");

$sql = "DELETE FROM MyGuests WHERE id=3";

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

echo "Record deleted successfully";

} else {

echo "Error deleting record: " . $conn->error;


}

$conn->close();

```

---

### Question 4: Attempt any THREE of the following (12 Marks)

#### (a) State user defined function and explain it with example.

**Marks: 4**

A function is a named block of code written in a program to perform some specific tasks. They take
information as parameters, execute a block of statements or perform operations on these
parameters and return the result. A function will be executed by a call to the function. The function
name can be any string that starts with a letter or underscore followed by zero or more letters,
underscores, and digits.

Example:

```php

<?php

function addNumbers($a, $b) {

return $a + $b;

echo addNumbers(5, 10); // Outputs: 15

?>

```

#### (b) Describe inheritance, overloading, overriding & cloning object.

**Marks: 4**

- **Inheritance**: Allows a class to inherit properties and methods from another class.

```php

class ParentClass {

public $name;

function __construct($name) {

$this->name = $name;

}
class ChildClass extends ParentClass {

function getName() {

return $this->name;

$child = new ChildClass("John");

echo $child->getName(); // Outputs: John

```

- **Overloading**: PHP does not support function overloading directly, but it can be simulated using
magic methods like `__call()`.

class shape {

function __call($name_of_function, $arguments) {

// It will match the function name

if($name_of_function == 'area') {

switch (count($arguments)) {

// If there is only one argument

// area of circle

case 1:

return 3.14 * $arguments[0];

// IF two arguments then area is rectangle;

case 2:

return $arguments[0]*$arguments[1];

} } } }

$s = new Shape;

echo($s->area(2));

echo "<br>";

// calling area method for rectangle

echo ($s->area(4, 2));

?>

Output:
9.426

48

- **Overriding**: A child class method overrides a parent class method with the same name.

```php

class ParentClass {

public function display() {

echo "Parent class display method";

class ChildClass extends ParentClass {

public function display() {

echo "Child class display method";

$obj = new ChildClass();

$obj->display(); // Outputs: Child class display method

```

- **Cloning Object**: Creating a copy of an existing object using the `clone` keyword.

```php

<?php

class Example {

public $var;

function __clone() {

$this->var = "Cloned object";

$obj1 = new Example();

$obj1->var = "Original object";

$obj2 = clone $obj1;

echo $obj2->var; // Outputs: Cloned object

?>
```

#### (c) Explain web server role in web development.

**Marks: 4**

A web server's primary role in web development is to handle client requests and serve web pages to
users. It processes incoming network requests over HTTP and other related protocols.

1. **Handling Requests**: Receives HTTP requests from clients (browsers).

2. **Serving Content**: Sends back the requested content (HTML, CSS, JavaScript, images).

3. **Processing Dynamic Content**: Interacts with server-side scripts (PHP, Python, Node.js) to
generate dynamic content.

4. **Security**: Implements security measures like SSL/TLS for encrypted communication.

5. **Session Management**: Manages user sessions and cookies to maintain state.

#### (d) Explain inserting & retrieving the query result operations.

**Marks: 4**

- **Inserting Data**:

```php

$conn = new mysqli("localhost", "username", "password", "database");

$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe',
'john@example.com')";

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

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

$query = "select * from MyGuests";

# Retrieving Values

$result = mysqli_query($con, $query);

foreach($result as $r) {

echo $r['firstname'].' '.$r['lastname’]; } // output: john doe

?>

$conn->close();

#### (e) Create a web page using GUI components.


**Marks: 4**

Example HTML with basic GUI components:

```html

<!DOCTYPE html>

<html>

<body>

<h2>Sample Web Page</h2>

<form action="/submit_form.php" method="post">

<label for="fname">First name:</label><br>

<input type="text" id="fname" name="fname"><br><br>

<label for="lname">Last name:</label><br>

<input type="text" id="lname" name="lname"><br><br>

<label for="gender">Gender:</label><br>

<input type="radio" id="male" name="gender" value="male">

<label for="male">Male</label><br>

<input type="radio" id="female" name="gender" value="female">

<label for="female">Female</label><br><br>

<label for="hobbies">Hobbies:</label><br>

<input type="checkbox" id="hobby1" name="hobby" value="Reading">

<label for="hobby1"> Reading</label><br>

<input type="checkbox" id="hobby2" name="hobby" value="Traveling">

<label for="hobby2"> Traveling</label><br><br>

<label for="country">Country:</label><br>

<select id="country" name="country">

<option value="India">India</option>

<option value="USA">USA</option>

</select><br><br>

<input type="submit" value="Submit">

</form>

</body>

</html>
```

---

### Question 5: Attempt any TWO of the following (12 Marks)

#### (a) Implement any three data types used in PHP with illustration.

**Marks: 6**

1. **String**:

```php

$str = "Hello, World!";

echo $str; // Outputs: Hello, World!

```

2. **Integer**:

```php

$int = 123;

echo $int; // Outputs: 123

```

3. **Array**:

```php

$arr = array("Red", "Green", "Blue");

print_r($arr); // Outputs: Array ( [0] => Red [1] => Green [2] => Blue )

```

#### (b) Write a program to connect PHP with MySQL.

**Marks: 6**

```php

<?php

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

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

echo "Connected successfully";

?>

```

#### (c) Explain the concept of constructor and destructor in detail.

**Marks: 6**

- **Constructor**: A special method called when an object is instantiated. It initializes object


properties.

```php

class MyClass {

public $name;

function __construct($name) {

$this->name = $name;

$obj = new MyClass("John");

echo $obj->name; // Outputs: John

```

- **Destructor**: A special method called when an object is destroyed. It performs cleanup tasks.

```php

class MyClass {

public $name;

function __construct($name) {

$this->name = $name;

function __destruct() {

echo "Destroying " . $this->name;


}

$obj = new MyClass("John");

// When the script ends or $obj is unset, the destructor is called.

```

---

### Question 6: Attempt any TWO of the following (12 Marks)

#### (a) Describe form controls – text box, text area, radio button, check box, list & buttons.

**Marks: 6**

1. **Text Box**: Single-line input field.

```html

<input type="text" name="username">

```

2. **Text Area**: Multi-line input field.

```html

<textarea name="message"></textarea>

``’

3. **Radio Button**: Allows the user to select one option from a set.

```html

<input type="radio" name="gender" value="male"> Male

<input type="radio" name="gender" value="female"> Female

```

4. **

Check Box**: Allows the user to select multiple options.

```html

<input type="checkbox" name="hobby" value="reading"> Reading

<input type="checkbox" name="hobby" value="travelling"> Travelling

```
5. **List (Select Box)**: Dropdown menu for selecting an option.

```html

<select name="country">

<option value="india">India</option>

<option value="usa">USA</option>

</select>

```

6. **Buttons**: Triggers an action when clicked.

```html

<button type="submit">Submit</button>

<button type="reset">Reset</button>

```

#### (b) Write a program to create PDF document in PHP.

**Marks: 6**

Using the `FPDF` library:

```php

<?php

require('fpdf.php');

$pdf = new FPDF();

$pdf->AddPage();

$pdf->SetFont('Arial', 'B', 16);

$pdf->Cell(40, 10, 'Hello World!');

$pdf->Output();

?>

```

Ensure the `fpdf.php` library is available in the working directory.

#### (c) Elaborate the following:

**Marks: 6**

(i) **__call()**: A magic method triggered when invoking inaccessible methods in an object context.

```php

class MagicMethod {
public function __call($name, $arguments) {

echo "Method $name with arguments " . implode(', ', $arguments) . " called.";

$obj = new MagicMethod();

$obj->nonExistentMethod('arg1', 'arg2'); // Outputs: Method nonExistentMethod with arguments


arg1, arg2 called. ```

(ii) **mysqli_connect()**: A function to establish a new connection to the MySQL server.

```php

$conn = mysqli_connect("localhost", "username", "password", "database");

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

echo "Connected successfully";

?>

```

You might also like