Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP

Kerala State Board New Syllabus Plus Two Computer Science Chapter Wise Previous Questions and Answers Chapter 10 Server Side Scripting Using PHP.

Kerala Plus Two Computer Science Chapter Wise Previous Questions and Answers Chapter 10 Server Side Scripting Using PHP

Question 1.
PHP is [March – 2016]
(a) freeware
(b) proletary
(c) both
(d) none (1)
Answer:
(a) freeware

Question 2
a) Compare Indexed and associative arrays in PHP. [March – 2016] (2)
b) Write a PHP program to display prime numbers below 50. (3)
OR
Write a PHP program to display the perfect numbers below 100. (3)
Answer:
a) In PHP array is a collection of key and value pairs. Unlike C++, in PHP we can use either non-negative integers or strings as keys.

Different types of arrays in PHP

  • Indexed arrays
  • Associate arrays
  • Multi dimensional arrays.

Indexed arrays-: The elements in an array can be distinguished as first or second or third etc. by the indices and the index of the first element is zero. In PHP the function array() is used to create an array.
Syntax: $array_name=array(valuel ,value2 );
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 47
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 48
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 49

Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 1
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 2

Question 3.
An array in PHP is given as follows: [May – 2016]
Sfamily = array (“Father”=>”Ajay”, “Son”=>”Arjun”, “Daughter”=>” “Archana”);
Name the type of this array. Write a for each loop that prints all the array elements. (2)
Answer:
This array is called associative array <? PHP
$family = array (“Father” = > “Ajay”, “Son” => “Arjun”, “Daughter” = > “Archana”);
foreach($family as $key =>$name) echo “$key – $name”. “, “;
?>

Question 4
A web server that supports PHP on any operating system is _____________ . [May – 2016] (1)
Answer:
Apache / LAMP/ WAMP

Question 5.
Discuss about special data types used in PHP. [May – 2016] (2)
Answer:
Special data types

  • Null: Special data type with value NULL. This is the default value to a variable, if we do not set a value to it.
    $m=null;
  • Array: Array is a collection of elements. With the same name we can store many elements.
    Three types of array –

    • Indexed array:
    • Associated array:
    • Multidimensional array:
  • Object: Similar in C++ and must be declared explicitly. At first a class is declared and then objects can be created using the command ‘new’.
  • Resources: It is not an actual data type rather it is a special variable that hold references to file handler, data base object, etc.

Question 6.
Create an HTML form in PHP showing the difference of the GET and POST method. [May – 2016]
OR
Write a function in PHP to find the factorial of a (3)
Answer:
The difference between GET and POST method is given below.

Get method  Post method
1) Faster  1) Slower
2) Tosendsmallvolume of data.  2) To send large volume of data
3) Up to 2000 characters  3) No limit
4) Less secure  4) More secure
5) Data visible during submission  5) Data not visible during submission
6) Page link can be book marked  6) Page link can never be book marked.

OR
Step 1
Take a note pad and type the following and save it as fact_form.html in C:\Abyss Web Server\htdocs
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 3

Step2
Take another notepad and type the following and save it as fact.php in C:\Abyss Web Server\htdocs
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 4

Step 3
Open Internet Explorer and type the following in the address bar
http://localhost/fact_form.html.

Plus Two Computer Science Chapter Wise Practice Questions and Answers

Question 1.
Write a PHP program to display the multiplication table of 5.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 5

Question 2.
Write a PHP program to display all palindrome numbers between 100 and 200.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 6

Question 3.
Write a PHP program to display all Armstrong numbers below 1000.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 7

Question 4.
Display the following table using PHP and HTML. Each cell should display the row number and column number of the cell. You are supposed to use the PHP loop statement to generate the table.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 8
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 9
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 10

Question 5.
Write a program to find the biggest of three numbers.
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 11
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 12

Question 6.
Create a registration form which contains the fields Name, Roll No, General and a submit button. Write a PHP script which accepts these details and display it back on the browser when the user clicks the submit button.
Answer:
Step 1
Take a notepad and type the following
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 13

Step 2
Take another notepad and type the following
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 14
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 15

Step 3.
Run the first file.

Question 7.
Write a PHP script for the following. Design a form to accept a string. Write a function to count the total number of vowels (a, e, i, o, u) from the string.
Answer:
Step 1
Take a notepad and type the following
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 16

Step 2
Take another notepad and type the following
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 17
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 18
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 19

Question 8.
Create a table in MySQL and insert a few records. Write a PHP script script to display the inserted records on a browser.
Answer:
Step 1
Take MySQL and create a table as follows
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 20

Step 2
Take a notepad and type the following and run.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 21
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 22
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 23
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 24

Question 9.
Create a table in MySQL and insert a few records. Write a PHP script to accept a value using a form, search the value in the database and display the record on a browser.
Answer:
Step 1.
Take a notepad and type the following. Save it as practiceQ9.html.

<html>
<head>
<title>
form
</title>
</head>
<body bgcolor=”cyan”>
<form method - ’post” action=”practiceQ9.php”>
Enter the Item code:
<input type=”text” name=”itemcode”><br>
<inputtype=’’submit” value=”submit”>
</form>
</body>
</html>

Step 2.
Take another notepad and type the following and save it as practiceQ9.php.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 26
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 27
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 28

Step 3.
Run the file practice9.html
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 29
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 30

Plus Two Computer Science Chapter Wise Assess Questions and Answers

Question 1
Which of the following functions returns 1 when the output is successful?
a) echo ()
b) print ()
c) both
d) None
Answer:
b) print()

Question 2.
Which of the following printing constructs accepts multiple parameters?
a) echo
b) print
c) printf
d) All of the above
Answer:
a) echo

Question 3
Which one of the following should not be used while sending password or other sensitive information?
a) Get
b) Post
c) Request
d) Next
Answer:
a) Get

Question 4
Which of the following functions returns the number of characters in a string variable?
a) count($variable)
b) ien($variable)
c) strcount($variable)
d) strlen(Svariable)
Answer:
d) strlen($variable)

Question 5
Which of the following variables is not a predefined variable?
a) $GET
b) $ASK
c) SREQUEST
d) $POST
Answer:
b) $ASK

Question 6
When $_GET variable is used to collect data, the data is visible to
a) none
b) only you
c) everyone
d) selected few
Answer:
c) everyone.

Question 7
When $_POST variable is used to collect data, the data is visible to
a) none
b) only you
c) everyone
d) selected few
Answer:
a) none

Question 8
Which variable is used to collect form data sent with both the GET and POST methods?
a) %Both
b) $_Both
c) $Request
d) $_Request
Answer:
d) $_Request

Question 9.
Which one of the following parameters is not used mysql_connect()?
a) database host
b) user ID
c) password
d) database name
Answer:
d) database name

Question 10
Study the following steps and determine the correct order
i) Open a connection to MySQL server
ii) Execute the SQL, query
iii) Fetch the data from query
iv) Select database
v) Close Connections
a) 1, 4, 2, 3, 5
b) 4, 1, 2, 3, 5
c) 1, 5, 4, 2, 1
d) 4, 1, 3, 2, 5
Answer:
a) 1, 4, 2, 3, 5.

Question 11.
What is PHP?
Answer:
Sip Formerly it is known as Personal Home Pages. Nowadays PHP stands for PHP Hypertext Preprocessor. Normally, it runs on a web server.

Question 12.
What is the difference between PHP and JavaScript?
Answer:
Client-side scripts(JavaScript) are run faster but it has some limitations. The limitations are we can view the source code hence it is less secure. On the other hand PHP is executed on the server and the result is sent back to the client(browser) so it is impossible to view the source code.

Benefits of using PHP
Client-side scripts(JavaScript) are run faster but it has some limitations. The limitations are we can view the source code hence it is less secure. On the other hand PHP is executed on the server and the result is sent back to the client(browser) so it is impossible to view the source code.

Benefits of PHP are given below

  • As PHP can be easily embedded into HTML moreover its easiness to convert a static website into a dynamic one.
  • It is compatible with almost all databases
  • It is platform-independent can run on almost all OS (Windows, Linux, MAC)
  • It is simple and easy to learn
  • It uses very small amount of system resources to run and faster.
  • PHP provides security to prevent malicious attacks.
  • Open-source community supports PHP
  • It is a free s/w by all means hence can be used to develop all types of web sites.

Question 13.
What are the steps involved in running a PHP?
Answer:
Take a note pad and type the code, PHP code should begin with <?php and end with ?>. Save this file in the root directory of the web server with extension php.

Step 1
Take a notepad and type the following and save it as first.php on C:\Abyss Web Server\htdocs.
<?php
echo”My first PHP web page”;
?>

Step 2
Start the web server if it is off

Step 3
Type as “http://localhost/first.php” in the address bar.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 31

Question 14
What is the use of “echo” in PHP?
Answer:
echo is an output statement, echo is used to produce multiple outputs. Parenthesis is optional.
eg:
echo “ first output”, “second output”;
or
echo (“ first output”, “second output”);

Question 15
What is the use of ‘print’ in PHP?
Answer:
print is an output statement, print is used to produce single outputs. It tales only one parameter and it is slower. It Returns TRUE or 1 on successful print and FALSE otherwise:
print “only one output”;
or
print (“only one output”);

Question 16.
What are the differences between “echo” and “print” statements?
Answer:
The difference between echo and print

echo print
take more than one parameter only one parameter
Does not return any value Returns TRUE or 1 on successful print and FALSE otherwise
Faster slower

Question 17.
Why is PHP superior to client-side languages such as JavaScript?
Answer:
Benefits of using PHP
Client-side scripts(JavaScript) are run faster but it has some limitations. The limitations are we can view the source code hence it is less secure. On the other hand PHP is executed on the server and the result is sent back to the client (browser) so it is impossible to view the source code.

Benefits of PHP are given below

  • As PHP can be easily embedded into HTML moreover its easiness to convert a static website into dynamic one.
  • It is compatible with almost all databases
  • It is platform-independent can run on almost all OS (Windows, Linux, MAC)
  • It is simple and easy to learn
  • It uses very small amount of system resources to run and faster.
  • PHP provides security to prevent malicious attacks.
  • Open-source community supports PHP
  • It is a free s/w by all means hence can be used to develop all types of web sites.

Question 18.
Which character is used to terminate a PHP statement? Name one situation where omitting this termination character does not produce an error.
Answer:
In PHP each and every statement must be end with semicolon. There is an exception for the last statement. For the last statement of PHP code semicolon is not compulsory.

Question 19.
What is an escape sequence? Name three commonly used escape sequences.
Answer:
Escape sequences
It is a character preceded by a back slash(\)

Operator Definition Example Output
\” To print “ echo TPHPY”’ “PHP”
\’ To print ‘ echo”XYian” X’ian
\n New line echo “BVM \nHSS” BVM HSS
\t To print tab echo”BVM\tHSS” BVM HSS
\r Print carriage return
\$ To print S echo “100\$” 100$
\\ To print\ echo’’\\n is used as new line” \n is used as new line

Question 20.
What will be the output of the following PHP scripts?
a) <?php
echo “Today looks \nbright and sunny”;
?>

b) <?php
echo “ours not to question why;”;
echo “ours but to do and die”;
?>

c) <?php
$x = 8 ;
$y = 8 ;
echo (sx = = sy) ;
?>
Answer:
a) Today looks bright and sunny
b)
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 32
C) 1

Question 21.
Find the error in each of the following PHP scripts:
a) <?php
/* print a line of output /
echo ‘hello’
?>

b) <?php
echo ‘<p align=”right”>Copyright Me, 2008</p>’;
?>

c) <?php
echo‘Line 1;
?>
Answer:
a) There is an error
Un terminated comment line The correct code is as follows
<?php
/‘print a line of output*/
echo’hello’
?>

b) no error.
c) String must be enclosed in single or double quotes. The correct code is as follows.
<?php
echo’Line 1‘;
?>

Question 22.
Which are the different types of loops in PHP?
Answer:
Loops in PHP

a) while loop
It is an entry controlled loop The syntax is given below
Loop variable initialised

while(expression)
{
Body of the loop;
Update loop variable;
}

Here the loop variable must be initialised out side the while loop. Then the expression is evaluated, if it is true then only the body of the loop will be executed and the loop variable must be updated inside the body. The body of the loop will be executed until the expression becomes false.

<?php
$i=10;
while($i>0)
{
echo”$i\t”;
$i - ;
}
?>

b) do-while loop
This loop will execute at least once. The body of the loop will be executed until the expression becomes false,

do
{
Body of the loop;
Update loop variable;
}while(expression);
Eg.
<?php
$i=10;
do
{
echo”$i\t”;
Si- -;
}while(Si>0);
?>

c) for loop
The syntax of for loop is given below For(initialisation; testing; updation)

{
Body of the for loop;
}
Eg.
<!DOCTYPE HTML>
<html lang=”en”>
<head>
<title>
We are learning PHP
</title>
</head>
<body bgcolor=”cyan”>
<?php
for($i=10;$i>0;$i- -)
echo”$i\t”;
?>
</body>
</html>

d) foreach loops(Extension of for loop)
The number of elements in an array is not known in advance foreach loop is used.

Two types
a) for ($array_variable_name as $value)
No need to specify the starting index and ending index, the array pointer is moved one by one.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 35

b) foreach($array_variable_name as key=>$value) Here the value of the current array element is assigned to $value and the key is assigned to $key.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 36

Question 23
How do we declare an array in php?
Answer:
Arrays in PHP
In PHP array is a collection of key and value pairs. Unlike C++, in PHP we can use either non-negative integers or strings as keys.

Different types of arrays in PHP

  • Indexed arrays
  • Associate arrays
  • Multi dimensional arrays.

Indexed arrays: The elements in an array can be distinguished as first or second or third etc. by the indices and the index of the first element is zero. In PHP the function array() is used to create an array.

Syntax: $array_name=array(value1 ,value2,……….);
OR
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 37

2 Associative arrays
Arräys with named keys and string indices are called associative arrays.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 38
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 39

Question 24
What is the importance of “action” attribute in an HTML, form?
Answer:
The action attribute in an HTML form specifies the file name the data to be submitted. When the submit button in the form is clicked the data is submitted to the file specified in the action attribute.

Question 25
How can we extract the string “hotmail.com” from a string “scert@hotmail.com”?
Answer:
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 40
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 41

Question 26.
What will be the output of the folloiwng code?
$a = 10;
if($a> 5cr< 15)
echo ‘true’;
else
echo ‘false’;
Answer:
An error occurred the correct code and output is as follows

<?php
$a=10;
if($a>5or$a<15)
echo 'true’;
else
echo’false’;
?>
The out put is true.

Question 27.
What will be the output of the following code? (1)

function fn ($var)
{
$var = $var - ($var/10*5);
return $var;
}
echo fn (100);

Answer:
The output is 50.

Question 28.
How do super global arrays help programming in PHP?
Answer:
This is a special variable available to all programs.

Superglobal Purpose
GLOBALS Global scope variables available.
$_SERVER Information about the server
$_GET Data passed using the HTTP GET method
$_POST Data passed using the HTTP POST method
$_REQUEST Data passed via HTTP request
$_FILES Data passed by an HTML file input
S_SESSION Current session data specific to the user
S_COOKIE Data stored on the browser.

Question 29
What is the use of $ SERVER?
Answer:
PHP $_SERVER
This returns the paths and script locations.

Code Description
$_SERVER [‘PHP_SELF’] Returns the currently executing file name
$_SERVER [‘SERVER_NAME’] Returns the host server name
$ SERVER [‘SCRIPT_NAME’] same as $_SERVER[‘PHP_SELF]

Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 42
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 43
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 44

Question 30
What are the difference between GET and POST methods in form submitting? (5)
Answer:
The difference between GET and POST method is given below.

Get method Post method
1. Faster 1. Slower
2. To send small volume of data. 2 To send large volume of data
3. Up to 2000 characters 3. No limit
4. Less secure 4. More secure
5. Data visible during submission 5. Data not visible during submission
6. Page link can be book marked 6. Page link can never be book marked.

Question 31
While submitting a form, the URL displayed on the address barwas http://localhost:8080 check2.php? exam=March2014&web=www.dhsekerala.gov.in Can you identify which method was used here?
Answer:
The method used was GET method because it displayed all the data during data submission.

Question 32
While trying to open a dynamic webpage from a . bookmark saved earlier, the webpage did not show properly. What could be the reason for this?
Answer:
The reason for this is that the method used for data submission is POST method. It is invisible to others that is the variables are not displayed in the URL hence it is not possible to bookmark the page.

Question 33
How do we create a MySQL connection?
Answer:
Establishing connection to MySQL database To access data from the data base, the PHP program first establish a connection to the data base server(MySQL) and then specify the data base to use.

Consider the following
1) Open a connection to MySQL.
2) Secify the data base we want to open.
3) Retrieve data from or insert data in to dat base.
4) Close the connection.

Question 34
How do we select a MySQL database from PHP?
Answer:
MySQL server may contain many data bases. mysqi_select_db() is a function used to select pa, ocular data base. It return true if it finds the data base otherwise false.
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 45
Plus Two Computer Science Chapter Wise Previous Questions Chapter 10 Server Side Scripting Using PHP 46

Question 35
How do we execute an SQL query? How to fetch its result?
Answer:
Two steps
a) Execute the SQL query on the data base using the function mysql_query()
Syntax:
$result_set=mysql7_query(query, connection);
b) I nsert a row using the function mysql_fetch_array()
$result_set=mysql_query(query, connection);
$fetched_row=mysql_fetch_array($result_set);