Sie sind auf Seite 1von 23

Important Questions for Mid-1

Short Questions

1) What are the differences between Get and Post methods?

Ans.

1) GET method passes request parameter in URL String while POST method passes
request parameter in request body.
2) GET request can only pass limited amount of data while POST method can pass
large amount of data to server.
3) GET request can be bookmarked and cached unlike POST requests.
4) GET is mostly used for view purpose (e.g. SQL SELECT) while POST is mainly use
for update purpose (e.g. SQL INSERT or UPDATE).

2) What is the necessary of CSS? Explain types?

Ans.

Cascading Style Sheet(CSS) is used to set the style in web pages which contain HTML
elements. It sets the background color, font-size, font-family, color, … etc property of
elements in a web pages.
There are three types of CSS which are given below:
 Inline CSS
 Internal or Embedded CSS
 External CSS

3) What are the advantages of XML when compared to HTML?

Advantages of XML over HTML

 By defining own markup language, can code documents more precisely


 Reflects structure and semantics of documents --> better searching and
navigation
 Tagging/content separate from display
 Allows single document to be used many ways

4 Write the differences between DOM parser and SAX parsers?

Ans.
Here are few high-level differences between DOM parser and SAX Parser in Java:

1) DOM parser loads whole XML document in memory while SAX only loads a small
part of the XML file in memory.

2) DOM parser is faster than SAX because it access whole XML document in memory.

3) SAX parser in Java is better suitable for large XML file than DOM Parser because it
doesn't require much memory.

4) DOM parser works on Document Object Model while SAX is an event based XML
parser.

5) What is PHP? Explain predefined string functions in PHP?

Ans.

PHP is a server scripting language, and a powerful tool for making dynamic and
interactive Web pages.

PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's


ASP.

PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open


source scripting language PHP scripts are executed on the server PHP is free to
download and use

strtolower Used to convert all string characters to lower echo strtolower( outputs benjamin
case letters 'Benjamin');
strtoupper Used to convert all string characters to upper echo outputs GEORGE W
case letters strtoupper('george w BUSH
bush');
Strlen The string length function is used to count the echo strlen('united 24
number of character in a string. Spaces in states of america');
between characters are also counted
Substr Used to return part of the string. It accepts three $my_var = 'This is a This is a re...
(3) basic parameters. The first one is the string really long sentence
to be shortened, the second parameter is the that I wish to cut
position of the starting point, and the third short';echo
parameter is the number of characters to be substr($my_var,0,
returned. 12).'...';
str_replace Used to locate and replace specified string echo str_replace that laptop is very
values in a given string. The function accepts ('the', 'that', 'the expensive
three arguments. The first argument is the text laptop is very
to be replaced, the second argument is the expensive');
replacement text and the third argument is the
text that is analyzed.

6) How to Define Constant Variable in Php? Give an Example?

To create a constant, use the define() function.

Syntax
define(name, value, case-insensitive)

Parameters:

 name: Specifies the name of the constant


 value: Specifies the value of the constant
 case-insensitive: Specifies whether the constant name should be case-
insensitive. Default is false

<?php
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
?>

7) Features of XML and Difference between Attributes and Entities?

 Excellent for handling data with a complex structure or atypical data


 Data described using markup language
 Text data description
 Human- and computer-friendly format
 Handles data in a tree structure having one-and only one-root element
 Excellent for long-term data storage and data reusability

Attributes
Attributes give the information about the elements. They can be specified only in the
element start tag and their values are enclosed strictly in double quotation-mark. This is
unlike HTML where attributes could be in single, double or without quotations.

Syntax: < tag attribute = “value” >description < /tag >

Example:
< problem size=“huge” cause=“unknown” solution=“run away” >

If elements are the “nouns” of XML, then attributes are its “adjectives”.An Element can
have zero, one or more attributes. Also an attribute name can only appear once within
an element

Bad: < Test name=“John” name=“Doe” / >


Good: < Test first=“John” last=“Doe” / >

Entities
Entity references are placeholders for other values that are otherwise reserved in the
language or that maybe misinterpreted. For example the less than (< ) and the greater
than ( > ) symbols are reserved for demarking the tags. If the entity description itself
contains one of these symbols the data would be misinterpreted. To avoid such a
scenario Entities are used. The ampersand (&) symbol is reserved to indicate start of an
entity.

The various predefined entities are as follows

< LESS THAN

> GREATER THAN

& AMPERSAND

" QUOTATIONS

&apos APOSTROPHEE

8) What are the basic rules to write XML document?

There are nine basic rules for building good XML:

1. All XML must have a root element.


2. All tags must be closed.
3. All tags must be properly nested.
4. Tag names have strict limits.
5. Tag names are case sensitive.
6. Tag names cannot contain spaces.
7. Attribute values must appear within quotes ("").
8. White space is preserved.
9. HTML tags should be avoided (optional).

9) How to find current date and time? & How to convert one date format into another in
PHP?
<?php

echo "Today's date is :";


$today = date("d/m/Y");
echo $today;

?>

<?php
echo "Today's date in various formats:" . "\n";

echo date("d/m/Y") . "\n";


echo date("d-m-Y") . "\n";
echo date("d.m.Y") . "\n";
echo date("d.M.Y/D");
?>

10) require_once(), require(), include(). What is difference between them?

require(): If it cannot find the file that is referenced, the application will throw an error
and stop the execution. include(): If it cannot find the referenced file it will throw a
warning, but the application WILL CONTINUE execution. (sorry for the caps, they are to
emphasize) require_once & include_once work in the same way as include() and
require() with the difference that they are slower, since the loading of a file is prevented
more than once. This prevents the functions and variables from being rewritten and you
have an error associated with it. Normally these versions are used only when they are
necessary

11) What are simple and complex data types in XML Schema?

Simple Type User can independently define. This type is used when a
restriction is placed on an embedded simple type to create
and use a new type.

Complex User can independently define. This type is used when the
Type type has a child element or attribute.

A simple type is a type that only contains text data when expressed according to XML
1.0. This type can be used with element declarations and attribute declarations. The
embedded simple type is provided for in XML Schema Part 2. A restriction may be
placed on an embedded simple type to create a new, unique simple type.
On the other hand, a complex data type is a type that has a child element or attribute
structure when expressed according to XML 1.0. An element declaration may be used
with this type. There are no predefined complex type data types, so the user will
always define their own.

12) Define DTD? Write Advantages and Disadvantages of DTD?

XML Document Type Declaration, commonly known as DTD, is a way to describe


precisely the XML language. DTDs check the validity of structure and vocabulary of an
XML document against the grammatical rules of the appropriate XML language.

DTD can be classified on its declaration basis in the XML document, such as −

 Internal DTD

 External DTD

When a DTD is declared within the file it is called Internal DTD and if it is declared in a
separate file it is called External DTD.

Advantages of using DTD


 Documentation − You can define your own format for the XML files. Looking at
this document a user/developer can understand the structure of the data.

 Validation − It gives a way to check the validity of XML files by checking whether
the elements appear in the right order, mandatory elements and attributes are in
place, the elements and attributes have not been inserted in an incorrect way,
and so on.

Disadvantages of using DTD


 It does not support the namespaces. Namespace is a mechanism by which
element and attribute names can be assigned to groups. However, in a DTD
namespaces have to be defined within the DTD, which violates the purpose of
using namespaces.

 It supports only the text string data type.

 It is not object oriented. Hence, the concept of inheritance cannot be applied on


the DTDs.
 Limited possibilities to express the cardinality for elements.

Long Questions

1. Write a PHP program to implement simple calculator?

Ans.

<!DOCTYPE html>

<html>

<head>

<title>Manual 10</title>

</head>

<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">

Enter first number:

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

<br><br>

Enter second number:

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

<br><br>

Select operation:

<br><br>

<input type="radio" name="r1" value="add">Addition

<br><br>

<input type="radio" name="r1" value="subtract">Subtraction

<br><br>

<input type="radio" name="r1" value="multiply">Multiplication


<br><br>

<input type="radio" name="r1" value="divide">Division

<br><br>

<input type="radio" name="r1" value="modulo">Modulus

<br><br>

<input type="submit" name="submit">

<br><br>

Output:

<?php

$num1=$_POST['number1'];

$num2=$_POST['number2'];

if($_SERVER["REQUEST_METHOD"]="POST")

if($_POST['r1']=='add')

echo $num1+$num2;

else if($_POST['r1']=='subtract')

echo $num1-$num2;

else if($_POST['r1']=='multiply')

echo $num1*$num2;

}
else if($_POST['r1']=='divide')

echo $num1/$num2;

else if($_POST['r1']=='modulo')

echo $num1%$num2;

?>

</form>

</body>

</html>

2. Write a PHP program to upload files onto the server.

Ans.

Q. WAP to upload a file in server & get the name, temporary name, size and extension
of that file.

Sol:

HTML CODE

<html>

<body>

<form method="POST" enctype="multipart/form-data">

<input type="file" name="file"><br><br>


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

</form>

PHP CODE

<?php

$name = $_FILES["file"]["name"];

$size = $_FILES["file"]["size"];

$ext = end((explode(".", $name)));

$tmp_name = $_FILES["file"]["tmp_name"];

//to get uploaded file name

echo "name of file is".$name;

//to get extension of uploaded file

echo "Extension of file is".$ext;

if (isset($name)) {

if(!empty($name))

//locate file uploading folder path

$location = "uploads/";

echo "<br> Uploaded Successfully!";


// to get the name of temporary file in server

if(move_uploaded_file($tmp_name, $location.$name)) {

echo "<br> temporary name of file is".$tmp_name;

echo "<br> Uploaded!";

// Check file size

if ($_FILES["file"]["size"]) {

echo "<br> your file size is ".$size;

} else{

echo "Please choose a file";

?>

</body>

</html>

3. Write the schema for the following xml data using DTD Language and XML
Schema Language

<empdetails>
<emp id=”1”>

<name>kiran</name>

<designation>professor</designation>

<salary>50000</salary>

</emp>

<emp id=”2”>

<name>shyam</name>

<designation>Associate professor</designation>

<salary>45000</salary>

</emp>

</empdetails>

Ans.

<xs:schema>

<xs:complexType=”empType”>

<xs:sequence>

<xs:element name=”name” type=”xs:string”/>

<xs:element name=”designation” type=”xs:string”/>

<xs:element name=”salary” type=”xs:integer”/>

</xs:sequence>

<xs:attribute name=”id” type=”xs:integer”/>

</xs:complexType>

<xs:complexType=”empdetailsType”>

<xs:element name=”emp” type=”empType” maxOccurs=”unbounded”/>

</xs:complexType>
<xs:element name=”empdetails” type=”empdetailsType”>

</xs:schema>

<!ELEMENT empdetails (emp*) >

<!ELEMENT emp (name,designation,salary)>

<!ATTLIST emp id CDATA #REQUIRED >

<!ELEMENT name (#PCDATA)>

<!ELEMENT designation (#PCDATA)>

<!ELEMENT salary (#PCDATA)>

4) Create an XML Document for storing books information such as isbn number, title,
author, publisher and price.

<xml version=”1.0”

<books>

<book>

<isbn></isbn>

<title></title>

<author></author>

<publisher></publisher>

<price></price>

</book>

</books>

5) Explain the following in detail

a) Cookies b) Session Tracking


a)

A cookie is often used to identify a user.

What is a Cookie?

A cookie is often used to identify a user. A cookie is a small file that the server embeds
on the user's computer. Each time the same computer requests a page with a browser,
it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Create Cookies With PHP

A cookie is created with the setcookie() function.

Syntax
setcookie(name, value, expire, path, domain, secure, httponly);

Only the name parameter is required. All other parameters are optional.

PHP Create/Retrieve a Cookie

The following example creates a cookie named "user" with the value "John Doe". The
cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is available
in entire website (otherwise, select the directory you prefer).

We then retrieve the value of the cookie "user" (using the global variable $_COOKIE).

Modify a Cookie Value

To modify a cookie, just set (again) the cookie using the setcookie() function:

Delete a Cookie

To delete a cookie, use the setcookie() function with an expiration date in the past:

b)
A session is a way to store information (in variables) to be used across multiple pages.

Unlike a cookie, the information is not stored on the users computer.

What is a PHP Session?

When you work with an application, you open it, do some changes, and then you close
it. This is much like a Session. The computer knows who you are. It knows when you
start the application and when you end. But on the internet there is one problem: the
web server does not know who you are or what you do, because the HTTP address
doesn't maintain state.

Session variables solve this problem by storing user information to be used across
multiple pages (e.g. username, favorite color, etc). By default, session variables last
until the user closes the browser.

So; Session variables hold information about one single user, and are available to all
pages in one application.

Tip: If you need a permanent storage, you may want to store the data in a database.

Start a PHP Session

A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

Get PHP Session Variable Values

Next, we create another page called "demo_session2.php". From this page, we will
access the session information we set on the first page ("demo_session1.php").

Notice that session variables are not passed individually to each new page, instead they
are retrieved from the session we open at the beginning of each page (session_start()).

Destroy a PHP Session

To remove all global session variables and destroy the session,


use session_unset() and session_destroy():
6) Explain the following in detail

a) Super Global Variables in PHP.

b) How to redirect a request from one PHP file to another PHP file with an example.

7) Write the schema for the following xml data using DTD Language and XML Schema
Language

<studentdetails>

<student rollno=”1”>

<name>kiran</name>

<college>KMIT</college>

<branch>CSE</branch>

</student>

<student rollno=”2”>

<name>shyam</name>

<college>CBIT</college>

<branch>ECE</branch>

</student>

</studentdetails>

8) Explain in detail about XML Schema Language.

9) Write a PHP program which takes username and password as input from html file
.Checks whether the user is a valid user or not with the data in the database. If the user
is valid user, it should redirect to welcome page else to failure page.

Ex1.html

<html>

<body>

<form action="ex1.php" method="post">

ENTER USER NAME: <input type="text" name="t1"/><br>


ENTER PASSWORD:<input type="password" name="t2"/><br>

<input type="submit" value="login"/>

</form>

</body>

</html>

<?php

session_start();

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "WTDB";

// Create connection

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

// Check connection

if($conn->connect_error)

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

// checks for existance of user with matching password


$stmt = $conn->prepare("SELECT * FROM log WHERE username=? and
password=?");

$stmt->bind_param("ss", $first, $last);

$first =trim($_POST['t1']);

$last = trim($_POST['t2']);

$_SESSION["suser"] = $first;

$stmt->execute();

$stmt->bind_result($u,$p);

if($stmt->fetch()==true)

$validuser=true;

else

$validuser=false;

$stmt->close();

//checks for the existance of the user

$stmt = $conn->prepare("SELECT username FROM log WHERE username=?");

$stmt->bind_param("s", $first);

$stmt->execute();

$stmt->bind_result($u1);

if($stmt->fetch()==true)

$userexists=true;

else

$userexists=false;

$stmt->close();

$conn->close();
if($validuser==true)

header("location:welcome.php");

else if($userexists==true)

header("location:pwdpage.php");

else

header("location:register.php");

?>

Ex1.php

<!DOCTYPE html>

<html>

<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

User Name: <input type="text" name="t1"><br>

Password: <input type="password" name="t2"><br>

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

</form>

Register.php

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")

{
// collect value of input field

$u = $_REQUEST['t1'];

$p = $_REQUEST['t2'];

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "WTDB";

// Create connection

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

// Check connection

if ($conn->connect_error)

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

// prepare and bind

$stmt = $conn->prepare("INSERT INTO log (username, password) VALUES (?, ?)");

$stmt->bind_param("ss", $u, $p);

if($stmt->execute()==true)

echo "new user registered succesfully";


else

echo "could not register";

$stmt->close();

$conn->close();

?>

</body>

</html>

Welcome.php

<?php

session_start();

echo "Hello,".$_SESSION["suser"];

?>

Pwdpage.php

<?php

echo "<b>incorrect password..Try again </b>";

?>

10) Explain the following in detail

a) Associative arrays in PHP b) Predefined Array functions in PHP


11) Write the schema for the following xml data using DTD Language and XML Schema
Language

<contacts>

<person>

<name>peter</name>

<mobileno>99999999</mobileno>

</person>

<person>

<name>john</name>

<mobileno>88888888</mobileno>

</person>

</contacts>

<xs:schema>

<xs:complexType=”personType”>

<xs:element name=”name” type=”xs:string”/>

<xs:element name=”mobileno” type=”xs:integer”/>

</xs:complexType>

<xs:complexType=”contactsType”>

<xs:element name=”persons” type=”personType” maxOccurs=”unbounded”/>

</xs:complexType>

<xs:element name=”contacts” type=”contactsType”/>

</xs:schema>
<!ELEMENT contacts (person*)>

<!ELEMENT person(name,mobileno)>

<!ELEMENT name (#PCDATA)>

<!ELEMENT mobileno (#PCDATA)>

12) Explain in detail the limitations of DTD Language and the strengths of XML Schema
Language.

Das könnte Ihnen auch gefallen