0% found this document useful (0 votes)
26 views11 pages

WebTech Question Paper-2

Uploaded by

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

WebTech Question Paper-2

Uploaded by

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

Question Paper-2

1 Marks Questions
a) Which tag is used to set the text in Superscript format?
The <sup> tag is used to set the text in superscript format in HTML.

b) Explain the use of <style>

The <style> tag is used in HTML to define internal styles for an HTML document. It
contains CSS (Cascading Style Sheets) code to specify the appearance and layout
of elements on the page.

c) What is the difference between echo() and print() function?


Both echo() and print() are used to output data in PHP. The main difference is that
echo can take multiple parameters, while print can only take one parameter.

Additionally, print always returns 1, so it can be used in expressions.

d) Which construct is used to define an array?


The array() construct is used to define an array in PHP.

e) How to create a directory in PHP?


The mkdir() function is used to create a directory in PHP. For example:

<?php
mkdir("new_directory");
?>

f) Explain any two directory functions.

opendir() : Opens a directory handle to read its contents.

readdir() : Reads the next entry from the directory handle.

Question Paper-2 1
g) What is a DSN?
DSN stands for Data Source Name. It is a string that specifies the connection
information and parameters needed to connect to a database.

h) Which protocols are used to retrieve mail from the server?


POP3 (Post Office Protocol 3) and IMAP (Internet Message Access Protocol) are
used to retrieve mail from the server.

i) How to convert an object to an array?


The get_object_vars() function can be used to convert an object to an array in PHP.
For example:

<?php
$object = new stdClass();
$object->name = "John";
$array = get_object_vars($object);
print_r($array);
?>

j) Explain SMTP Protocol.


SMTP (Simple Mail Transfer Protocol) is a protocol used to send emails. It defines
the rules and conventions for email communication between servers. SMTP is
responsible for transferring emails from the sender's server to the recipient's server.

2 Marks Questions

a) Differentiate between single quoted string and double quoted string.

Single Quoted String:

Variables and escape sequences are not expanded.

Example: $string = 'This is a single-quoted string.';

Double Quoted String:

Question Paper-2 2
Variables and escape sequences are expanded.

Example: $name = 'John'; $string = "Hello, $name!";

b) How External CSS is used?


External CSS is used by linking an external CSS file to an HTML document. The
<link> tag is used in the <head> section of the HTML document to reference the

external CSS file.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, init
<link rel="stylesheet" href="styles.css">
<title>External CSS Example</title>
</head>
<body>
<!-- Content of the HTML document -->
</body>
</html>

c) Write any two functions to decompose a string with a suitable example.

explode() Function:

Splits a string into an array of substrings based on a specified delimiter.

<?php
$string = "apple,orange,banana";
$fruits = explode(",", $string);
print_r($fruits);
?>

str_split() Function:

Converts a string into an array of characters.

Question Paper-2 3
<?php
$string = "Hello";
$characters = str_split($string);
print_r($characters);
?>

d) How to find out the position of the first occurrence of a substring in a string?
The strpos() function is used to find the position of the first occurrence of a substring
in a string.

<?php
$string = "Hello, World!";
$position = strpos($string, "World");
echo "Position of 'World': $position";
?>

e) What is the purpose of the array_splice() function?

The array_splice() function is used to change the content of an array by removing or


replacing existing elements and/or adding new elements. It modifies the original
array and returns an array of the removed elements.

<?php
$colors = array("red", "green", "blue");
$removed = array_splice($colors, 1, 1, "yellow");
print_r($colors); // Output: Array ( [0] => red [1] => yell
print_r($removed); // Output: Array ( [0] => green )
?>

4 Marks Questions
a) Discuss the Scope of a Variable in PHP with an example

Variable Scope in PHP:

Question Paper-2 4
The scope of a variable refers to the context in which it is defined and can be
accessed. PHP has three main variable scopes: local, global, and static.

Example:

<?php
// Global scope variable
$globalVar = "I am global";

function exampleFunction() {
// Local scope variable
$localVar = "I am local";
echo $localVar; // Accessible only within the function

// Accessing global variable within the function


global $globalVar;
echo $globalVar;
}

exampleFunction();

// Trying to access localVar outside the function will resu


// echo $localVar;

// Accessing globalVar outside the function


echo $globalVar;
?>

In this example, $globalVar is a global variable accessible throughout the script,


while $localVar is a local variable accessible only within the exampleFunction() .

b) Explain prepare() and execute() commands in database handling

prepare() Method:

Used to prepare an SQL statement for execution. It helps prevent SQL


injection by allowing parameterized queries.

<?php
$stmt = $conn->prepare("INSERT INTO employees (name, s

Question Paper-2 5
$stmt->bind_param("si", $name, $salary);
$name = "John";
$salary = 50000;
$stmt->execute();
?>

execute() Method:

Executes a prepared SQL statement. If the statement includes


parameters, you need to bind values to those parameters before calling
execute() .

<?php
$stmt = $conn->prepare("SELECT name, salary FROM emplo
$stmt->bind_param("i", $employeeId);
$employeeId = 1;
$stmt->execute();
$stmt->bind_result($name, $salary);
$stmt->fetch();
echo "Name: $name, Salary: $salary";
?>

c) Explain the functions used for reading and writing characters in files
Functions for Reading Characters:

fread() : Reads a specified number of bytes from a file.

<?php
$file = fopen("example.txt", "r");
$content = fread($file, filesize("example.txt"));
fclose($file);
echo $content;
?>

fgets() : Reads a line from a file.

<?php
$file = fopen("example.txt", "r");

Question Paper-2 6
while (!feof($file)) {
$line = fgets($file);
echo $line;
}
fclose($file);
?>

Functions for Writing Characters:

fwrite() : Writes a specified number of bytes to a file.

<?php
$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);
?>

file_put_contents() : Writes data to a file (simpler alternative).

<?php
file_put_contents("example.txt", "Hello, World!");
?>

4 Marks Questions
a) Design HTML form that will accept user input of user name, Address, provide
buttons to submit the input as well as to refresh it.
HTML Form:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, init
<title>User Input Form</title>
</head>
<body>

Question Paper-2 7
<form action="process_form.php" method="post">
<label for="username">User Name:</label>
<input type="text" id="username" name="username" re

<label for="address">Address:</label>
<textarea id="address" name="address" rows="4" requ

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


<input type="reset" value="Refresh">
</form>
</body>
</html>

Note: The form submits data to a PHP script named process_form.php .

b) Write PHP Script to accept an associative array and sort it in descending order.
Display the sorted array to the user.

PHP Script (process_array.php):

<?php
// Accept associative array
$userInfo = array(
"username" => "JohnDoe",
"address" => "123 Main Street",
// Add more fields as needed
);

// Sort the associative array in descending order


arsort($userInfo);

// Display the sorted array to the user


foreach ($userInfo as $key => $value) {
echo "$key: $value <br>";
}
?>

Question Paper-2 8
c) Accept directory name from the user. Write a PHP program to change the
current directory to the accepted directory name and count the number of files
and directories in it.

PHP Script (change_directory.php):

<?php
// Accept directory name from the user (assuming it's submi
$directoryName = $_POST['directory'];

// Change the current directory


chdir($directoryName);

// Count the number of files and directories


$files = glob("*");
$numFiles = count($files);
$numDirectories = count(array_filter($files, 'is_dir'));

// Display the results


echo "Current Directory: " . getcwd() . "<br>";
echo "Number of Files: $numFiles<br>";
echo "Number of Directories: $numDirectories";
?>

Note: Ensure proper validation and security measures are implemented when
accepting user input, especially directory names, to prevent potential security
issues.

3 Marks Questions
a) Explain terms HTTP request and HTTP response.

HTTP Request:
An HTTP request is a message sent by a client to request a specific action from a
server. It consists of:

Request Line: Specifies the HTTP method (e.g., GET, POST) and the
resource (URL) being requested.

Question Paper-2 9
Headers: Additional information about the request (e.g., user-agent,
content-type).

Body: Data sent with the request, often used in POST requests.
HTTP Response:

An HTTP response is a message sent by a server in response to an HTTP


request. It includes:

Status Line: Indicates the status of the response (e.g., 200 OK, 404 Not
Found).

Headers: Additional information about the response.

Body: Data sent by the server as the response content.

Example:

HTTP Request:

GET /index.html HTTP/1.1


Host: example.com
User-Agent: Mozilla/5.0

HTTP Response:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 123
<html>
<!-- Response content -->
</html>

b) Explain the concept of missing parameters to a function with a suitable


example.
Concept:
In programming, when calling a function that expects certain parameters, missing
parameters refer to not providing the required arguments for the function to
operate correctly.
Example:

Question Paper-2 10
<?php
// Function that expects two parameters
function addNumbers($num1, $num2) {
return $num1 + $num2;
}

// Calling the function with missing parameters


$result = addNumbers(5);
echo "Result: $result";
?>

Explanation:

The addNumbers function expects two parameters ( $num1 and $num2 ), but when
calling it, only one parameter (5) is provided. This results in a missing parameter.
In this example, PHP will issue a warning or notice about the missing parameter,
and the result will be based on default values or null if not specified.

It's essential to provide the correct number of parameters to functions to ensure


they work as intended.

Question Paper-2 11

You might also like