Friday, February 28, 2025

3. Web Technology II [Most Important Questions and Solutions]

IMPORTANT OLD QUESTION OF WEB TECHNOLOGY II

JavaScript (Client-side scripting):

2081 GIE Set A Write a JavaScript code snippet, along with HTML, that displays the multiplication table of an entered number.

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Multiplication Table</title>

</head>

<body>

    <script>

        function multiplication()

        {

         var n;

         n = parseInt(prompt("Enter a number:"));

         document.write("Multiplication Table of " + n)

         for (var i = 1; i <= 10; i++)

         {

            document.write(n + " × " + i + " = " + (n * i) + "<br>");

         }

        }

        multiplication();

    </script>

</body>

</html>

 

Example Output:

Enter a number: 5

Multiplication Table of 5

5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
5 × 10 = 50

 

2081 Q.No. 11 Write a JavaScript function that checks if a number is even or odd and prints the result. [1+4]

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Even or Odd Checker</title>

</head>

<body>

    <script>

        function checkEvenOdd()

        {

            var n;

            n = parseInt(prompt("Enter a number:"));

            if (n % 2 === 0)

            {

                document.write("The number is an Even number");

            }

            else

            {

                document.write("The number is an Odd number");

            }

        }

        checkEvenOdd();

    </script>

</body>

</html>

 

Example Output:

Enter a number: 5

The number is an Odd number

 

2081 GIE Set B/2080 GIE Set A Q.No. 11 Write a program to find the largest number among three numbers in JavaScript. [5]

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Largest Number Finder</title>

</head>

<body>

    <script>

        function findLargest()

        {

            var a, b, c;

            a = parseInt(prompt("Enter first number:"));

            b = parseInt(prompt("Enter second number:"));

            c = parseInt(prompt("Enter third number:"));

            if (a >= b && a >= c)

            {

                document.write("The largest number is: " + a);

            }

            else if (b >= a && b >= c)

            {

                document.write("The largest number is: " + b);

            }  

            else

            {

                document.write("The largest number is: " + c);

            }

        }

        findLargest();

    </script>

</body>

</html>

 

Example Output:

Enter first number: 50

Enter second number: 60

Enter third number: 40

The largest number is: 60


2080 GIE Set B Q.No. 11 How do you add an event handler in JavaScript? Give an example. [5]

Ans:

In JavaScript, an event handler is a function that gets executed when a specific event occurs, such as a mouse click, a key press, or a form submission. We can add event handlers using either of the following methods:

a) Using the onclick attribute (Inline event handling)

b) Using addEventListener() (External event handling)

A simple example of an event handler using the onclick attribute in JavaScript is as follows:

<!DOCTYPE html>

<html>

<head>

    <title>Hello World</title>

</head>

<body>

<button onclick="showMessage()">Click me</button> 

    <script>

        function showMessage()

        {

            document.write("Hello, World!");

        }

 </script>

</body>

</html> 

Explanation:

The showMessage() function uses document.write() to directly write the text "Hello, World!" to the page.

(Click me) button triggers the showMessage() function when clicked

 

2080/2079 Set A Q.No. 11 Write a program to find the factorial of any given number using JavaScript. [5]

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Factorial of a Number</title>

</head>

<body>

    <script>

        function calculateFactorial() 

        {

            var n, fact = 1;

            n = parseInt(prompt("Enter a number:"));

            if (isNaN(n) || n < 0) 

            {

                document.write("Invalid input! Please enter a non-negative integer.");

                return;

            }

            for (var i = 1; i <= n; i++) {

                fact = fact * i;

            }

            document.write("The factorial of " + n + " is: " + fact);

        }

        calculateFactorial();

    </script>

</body>

</html>

Example Output:

Enter a number: 5

The factorial of 5 is: 120

 

2078 NEB Model/2079 GIE Set A Q.No. 11 Write a function to enter two numbers and find sum of two numbers in JavaScript.    [5]

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Sum of Two Numbers</title>

</head>

<body>

    <script>

        function calculateSum()

        {

            var a, b, s;

            a = parseInt(prompt("Enter first number:"));

            b = parseInt(prompt("Enter second number:"));

            s = a + b;

            document.write("The sum is: " + s);

        }

        calculateSum(); 

    </script>

</body>

</html>

 

Example Output:

Enter first number: 8

Enter second number: 7

The sum is: 15

 

2079 GIE Set B Q.No. 15 What are the uses of Java Script in web page development? [3]

Ans:

JavaScript is one of the most important client-side programming languages in web development. It plays a crucial role in creating dynamic, interactive, and user-friendly websites. Some popular uses of JavaScript in webpage development are as follows:

1.       Creating Interactive Elements: JavaScript allows developers to create interactive elements like dropdown menus, sliders, pop-ups, and form validations that improve user engagement.

2.       Asynchronous Operations (AJAX): JavaScript enables asynchronous communication with servers, allowing websites to update content without requiring a full page reload.

3.       Client-Side Scripting: JavaScript runs on the user’s browser rather than the server, reducing load time and improving website performance.

4.       Mobile and Web App Development: JavaScript is used in mobile app development through frameworks like React Native and in progressive web apps (PWAs).

5.       Security Features: JavaScript helps in real-time form validation and authentication before data is sent to the server.

 

2079 NEB Model Q.No. 11 Develop a program in JavaScript to exchange/swap the values of any two variables.  [5]

Ans:

<!DOCTYPE html>

<html>

<head>

<title> To Swap values </title>

</head>

<body>

<script>

var a, b, temp;

a = parseInt(prompt("Enter value of 1st variable:"));

b = parseInt(prompt("Enter value of 2nd variable:"));

temp=a;

a=b;

b=temp;

document.write("The value of 1st variable after swap:"+a);

document.write("The value of 2nd variable after swap:"+b);

</script>

</body>

</html>

 

Example Output:

Enter value of 1st variable: 6

Enter value of 2nd variable: 4

The value of 1st variable after swap: 4

The value of 2nd variable after swap: 6

 

Asmita Set Q.No 11 OR Write a JavaScript to print the largest and smallest numbers among 10 elements of an array.    [5]

Ans:

<!DOCTYPE html>

<html>

<head>

  <title>Largest and Smallest in Array</title>

</head>

<body>

  <script>

    var numbers = [];

 

    // Get 10 numbers from the user

    for (var i = 0; i < 10; i++)

    {

      numbers[i] = parseInt(prompt("Enter number " + (i + 1) + ":"));

    }

 

    // Initialize largest and smallest with the first element

    var largest = numbers[0];

    var smallest = numbers[0];

 

    // Loop through the array to find largest and smallest

    for (var i = 1; i < 10; i++)

    {

      if (numbers[i] > largest)  

      {

        largest = numbers[i];

      }

      if (numbers[i] < smallest)

      {

        smallest = numbers[i];

      }

    }

 

    // Display the results AFTER the loop finishes

    document.write("Largest number: " + largest);

    document.write("Smallest number: " + smallest);

  </script>

</body>

</html>

 

PHP (Server-side scripting):

2081 Q.No. 11 OR What is the purpose of the mysqli_connect() function in PHP? Describe its usage and parameters. [2+3]

Ans:

The mysqli_connect() function in PHP is essential for establishing a connection between a PHP script and a MySQL database server. This connection is the foundation for any PHP application that needs to interact with a MySQL database, allowing it to perform operations like querying, inserting, updating, and deleting data.

Purpose, usage, and parameters of mysqli_connect() function in PHP are as follows:

Purpose:

Ø To create a link between a PHP script and a MySQL database server.

Ø To enable the PHP script to send and receive data from the MySQL database.

Usage:

An example to demonstrate usage of mysqli_connect() function in PHP is as follows:

<?php

// Step 1: Database configuration

$servername = "localhost";

$username = "root";

$password = "";

$database = "studentDB";

 

//Step2: Database connection

$con = new mysqli($servername, $username, $password, $database);

 

// Step 3: Check connection 

if ($con->connect_error) {

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

} else {

    echo "Connected successfully!";

}

?>

Parameters:

$servername – The hostname or IP of the database server (e.g., "localhost" or "127.0.0.1").

$username – The MySQL database username (e.g., "root").

$password – The MySQL database password (e.g., "" for empty or "your_password").

$database – The name of the database (e.g., "studentDB").

$port(optional) – The port number for the database connection (default: 3306).

$socket(optional) – The socket or named pipe used for the connection. 


2080 GIE Set A Q.No. 11 OR How do you connect a MySQL (Client-Side scripting) database with PHP (Server-Side Scripting)? Explain with an example. [5]

2080 GIE Set B Q.No. 11 OR Explain the database connection PHP function for MySQL. [5]

2079 GIE Set A Q.No. 11 OR Explain the database connection method in PHP.    [5]

2079 GIE Set B Q.No. 15 How do you connect the PHP and My SQL database? Explain with connection class.   [5]

2079 NEB Model Q.No. 11 OR How can you connect a database with PHP? Demonstrate with an example.   [5]

Ans:

Database connectivity refers to the process of connecting a server-side script (like PHP) to a database (like MySQL) to store, retrieve, update, and delete data. The new mysqli() or mysqli_connect() function in PHP is essential for establishing a connection between a PHP script and a MySQL database server. Syntax for Database Connection is as follows:

$con = new mysqli($servername, $username, $password, $database);

OR

$con = mysqli_connect($servername, $username, $password, $database);

We can connect a MySQL (Client-Side scripting) database with PHP (Server-Side Scripting) by following these steps as shown in example below:

<?php

// Step 1: Database configuration

$servername = "localhost";

$username = "root";

$password = "";

$database = "studentDB";

 

//Step2: Database connection

$con = new mysqli($servername, $username, $password, $database);

 

// Step 3: Check connection 

if ($con->connect_error) {

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

} else {

    echo "Connected successfully!";

}

?>

Explanation:

  1. Create a Connection – Uses the new mysqli() object-oriented method to establish a connection.
  2. Check Connection – Uses $con->connect_error to check if there was a connection error.
  3. Error Handling – If the connection fails, it stops execution with die() and outputs the error.
  4. Success Message – If the connection is successful, it prints "Connected successfully!".

 

2080 Q.No. 11 Define syntax for database connectivity. Write a server-side scripting code to insert data into the table student having fields (first name, last name, mark and email). Assume that server name= “localhost”, username=”root”, password=” “, database name=”student DB”.

2081 PABSON Q.No. 11 Write a PHP program to insert data in a database.

Ans:  

Syntax for Database Connectivity:

$con = new mysqli($servername, $username, $password, $database);

OR

$con = mysqli_connect($servername, $username, $password, $database);

 

A server-side scripting code to insert data into the table student having fields (first name, last name, mark and email) is as follows:

<?php

// Step 1: Database configuration

$servername = "localhost";

$username = "root";

$password = "";

$database = "studentDB";

 

//Step2: Database connection

$con = new mysqli($servername, $username, $password, $database);

 

// Step 3: Check connection

if ($con->connect_error)

{

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

} else {

    echo "Connected successfully!";

}

 

// Step 4: Prepare the SQL statement for insertion

$sql = "INSERT INTO student (first_name, last_name, mark, email)

             VALUES ('Madhav', 'Sapkota', 30, 'madhav@gmail.com')";

 

// Step 5: Execute the query and check if data is inserted

if ($con->query($sql) === TRUE)

{

    echo "Data inserted successfully";

} else {

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

}

// Step 5: Close connection

$con->close();

?>

 

2081 GIE Set A Q.No. 11 OR Develop a PHP code to add a record like (601, Raju, M, 12-06-2002, Kathmandu) in the table named info_stud having structure (reg_no integer, name character 25, gender character 1, dob character 10, address character 25) of the database named "School".

Ans:

A PHP code to insert the record (601, Raju, M, 12-06-2002, Kathmandu) into the table info_stud in the database "School" is as follows:

<?php

// Step 1: Database configuration

$servername = "localhost";

$username = "root";

$password = "";

$database = "School";

 

// Step 2: Database connection

$con = new mysqli($servername, $username, $password, $database);

 

// Step 3: Check connection

if ($con->connect_error)

{

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

} else {

    echo "Connected successfully!";

}

 

// Step 4: Prepare the SQL statement for insertion

$sql = "INSERT INTO info_stud (reg_no, name, gender, dob, address)

             VALUES (601, 'Raju', 'M', '12-06-2002', 'Kathmandu')";

 

// Step 5: Execute the query and check if data is inserted

if ($con->query($sql) === TRUE)

{

    echo "Record added successfully!";

} else {

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

}

 

// Step 6: Close connection

$con->close();

?>

 

2079 Set A Q.No. 11 OR How do you fetch data from a database in PHP and display it in form? Describe. [5]

Ans:

To fetch data from a database in PHP and display it in form, follow these steps as shown in example below:

<!DOCTYPE html>

<html>

<head>

    <title>Fetch Data and Display in Form</title>

</head>

<body>

<?php

//Step 1: Database configuration

$servername = "localhost";

$username = "root";

$password = "";

$database = "test_db";

 

// Step 2: Database connection

$con = new mysqli($servername, $username, $password, $database);

 

// Step 3: Check connection

if ($con->connect_error) {

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

}

 

// Step 4: Fetch data when ID is provided (Example: user with ID=1)

$id = 1;

$sql = "SELECT * FROM users WHERE id = $id";

$result = $con->query($sql);

 

//Step 5: Check if data exists

if ($result->num_rows > 0) {

    $row = $result->fetch_assoc();

} else {

    echo "No record found!";

    exit();

}

?>

<!--Step 6: Display Data in Form -->

<form method="post">

    Name: <input type="text" name="name" value="<?php echo $row['name']; ?>">

    Age: <input type="number" name="age" value="<?php echo $row['age']; ?>">

    <input type="submit" name="update" value="Update">

</form>

</body>

</html>

 

2081 GIE Set A Q.No. 11 OR Write a PHP program to swap two numbers.   [5]

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Swap Two Numbers</title>

</head>

<body>

<form method="post">

  Enter First Number: <input type="number" name="a" required>

  Enter Second Number: <input type="number" name="b" required>

  <input type="submit" name="swap" value="Swap">

</form>

<?php

if(isset($_POST['swap']))

{

    $a = $_POST['a'];

    $b = $_POST['b'];

    $temp = $a;

    $a = $b;

    $b = $temp;

    echo "The value of 1st variable after swap:" . $a;

    echo "The value of 2nd variable after swap:" . $b;

}

?>

</body>

</html>


Write a PHP program to find sum of two numbers.

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Sum of Two Numbers</title>

</head>

<body>

<form method="post">

  Enter First Number: <input type="number" name="a" required>

  Enter Second Number: <input type="number" name="b" required>

  <input type="submit" name="sum" value="Calculate Sum">

</form>

<?php

if(isset($_POST['sum']))

{

    $a = $_POST['a'];

    $b = $_POST['b'];

    $sum = $a + $b;

    echo "The sum of the two numbers is:" . $sum;

}

?>

</body>

</html>  

 

Write a PHP program to display the largest among three numbers.

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Largest Among Three Numbers</title>

</head>

<body>

<form method="post">

  Enter First Number: <input type="number" name="a" required>

  Enter Second Number: <input type="number" name="b" required>

  Enter Third Number: <input type="number" name="c" required>

  <input type="submit" name="largest" value="Find Largest">

</form>

<?php

if(isset($_POST['largest']))

{

    $a = $_POST['a'];

    $b = $_POST['b'];

    $c = $_POST['c'];

    if ($a > $b && $a > $c)

    {

        echo "The largest number is:" . $a;

    }

    elseif ($b > $c)

    {

        echo "The largest number is:" . $b;

    }

    else

    {

        echo "The largest number is:" . $c;

    }

}

?>

</body>

</html>


Write a PHP program to check whether the given number is even or odd.

Ans:

<!DOCTYPE html>

<html>

<head>

    <title>Even or Odd</title>

</head>

<body>

<form method="post">

  Enter a Number: <input type="number" name="num" required>

  <input type="submit" name="check" value="Check">

</form>

<?php

if(isset($_POST['check']))

{

    $num = $_POST['num'];

    if ($num % 2 == 0)

    {

        echo "The number is even";

    }

    else

    {

        echo "The number is odd";

    }

}

?>

</body>

</html>


MULTIPLE CHOICE QUESTIONS:

1. 2081 GIE Set A What is a scripting language that runs for the browser of the user's Computer?

A) Object-oriented programming

B) Server-side scripting language

C) Client-side scripting Language

D) Structure programming Language

2. 2081 GIE Set A Predict the correct output of the following PHP code.

                       <?php $ x=30; $y = 30;

                       if ($x>$y) echo "CDC, Nepal.";

                       else echo "NEB, Nepal."; ?>

A) 30 is not greater than 30.                                                              

B) CDC, Nepal                          

C) NEB, Nepal.                                         

D) No output.

3. 2080 GIE Set A Analyse the expected output of the provided in JavaScript snippet.

  <script> var p=120, Q=O; document. Write (P/Q); </script>

A) Garbage value                          B) 0                           C) 120                               D) Infinity is pointed

4. 2081 GIE Set B What type of language is PHP?

a) Client-side scripting

b) Server-side scripting

c) User-side scripting

d) Machine level

5. 2081 GIE Set B Which JavaScript method is used to write HTML output?

a) console.log()

b) document.output()

c) document.write()

d) document.writeHTML()

6. 2081 GIE Set B Which sign is used to declare variables in PHP?

a) @           b) *            c) ^                 d) $

7. 2081 What is the correct syntax for a 'for-loop' in JavaScript?
a. for (var i = 0; i < 5; i++)
b. for (i = 0; i < 5; i++)
c. for (var i < 5; i++)
d. for (var i = 5; i++)

8. 2081 Which PHP function is commonly used to execute SQL queries on a database connection established using MySQL extension?
a. mysqli_query()
b. pdo_query()
c. mysql_connect()
d. pgsql_query()

9. 2080 GIE Set A What function do you need to call to ask the user of the program to enter text in JavaScript?
a. readLine             b. readln               c. text                d. println

10. 2080 GIE Set B Which JavaScript function is used to take text input?
a. alert()
b. prompt()
c. confirm()
d. console.log()

11. 2080 GIE Set B Which of the following PHP functions is used to connect to a MySQL database?
a. mysql_connect()
b. mysqli_connect()
c. pdo_connect()
d. db_connect()

12. 2080 Which of the given signs is used as a shortcut for jQuery?
a. The * sign
b. The & sign
c. The $ sign
d. The @ sign

13. 2080 Which jQuery method is used to hide selected elements?
a. hide()        b. hide(on)      c. invisible()       d. display(none)

14. 2079 GIE Set A Which method is used to return today’s date and time in PHP?
a. Today date() method
b. Today’s date() method
c. Date() method
d. Today’s date and time() method

16. 2079 GIE Set B What would be the value of "z" in the given JavaScript code?

var x = 5;

var y = 2;

var z = x % y;

a. 0                         b. 2                        c. 1                      d. 2.5

17. 2079 GIE Set B Which is the correct opening and closing tag for PHP scripts?
a. { }          b. <?php ?>          c. ( )                  d. <??>

18. 2079 Set A/2079 GIE Set A What is the correct syntax for referring to an external JavaScript script?
a. <script src="myscript.js"></script>
b. <script href="myscript.js"></script>
c. <js href="myscript.js"></js>
d. <js src="myscript.js"></js>

19. 2079 Set A Which of the following is the correct way of defining a variable in PHP?
a. $variable name = value;
b. $variable_name = value;
c. $variable.name = value;
d. $variable name as value;

20. 2079 NEB Model Which of the following keywords are used to declare a variable in

JavaScript?

A) int or var

B) float or let

C) var or let

D) char or var

21. 2079 NEB Model Which of the following commands is executed in PHP to concatenate the variables $x with $y?

A) $x + $y

B) $x=$y

C) concat ($x,$y)

D) $x.$y

22. ……. Extracts a section of a string and returns a new string

a. set() method      b. slice() method      c. split() method   d. None

20. JavaScript is ...
a. Procedural-oriented language
b. Object-oriented programming language
c. Firmware
d. None of the above

23. Data types in JavaScript are ...
a. Primary and secondary
b. Primitive and non-primitive
c. Local and universal
d. None of the above

24. PHP stands for ...
a. Hypertext Pre-processor
b. Program hyper processor
c. Process hyper program
d. None of the above

25. concat() is ...
a. Integer method
b. String method
c. Image method
d. None of the above

26. ... is an identifier whose value may change during the execution of the program.
a. Constant             b. Function      c. Variable     d. None of the above

27. What is a scripting language that runs for the browser of the user's Computer?

A) Object-oriented programming

B) Server-side scripting language

C) Client-side scripting Language

D) Structure programming Language

28. Predict output

<?

$fruits=array("mango","apple","pear","peach");

$fruits = array_reverse($fruits);

echo($fruits[2]);

?>
a. mango          b. peach        c. pear        d. apple

29. Which of the following is the correct syntax to display "Stay Safe" in an alert box using JavaScript?

a) alert-box("Stay Safe");

b) confirm("Stay Safe");

c) msgbox("Stay safe");

d) alert("Stay Safe");

30. Which of the following is a ternary operator in JS?

a) -         b) ?:         c) +              d) :

31. Which of the following can’t be done with client-side JavaScript?

a) Validating a form

b) Sending a form’s content by email

c) Storing the form’s contents to a database file on the server

d) None of the above

32. Which of the following language is not web scripting language?

 a. HTML           b. CSS              c. JavaScript                d. C Language

33. Which of the following statement is not true?

    a. HTML is standard language for development webpages

    b. HTML helps to embed text, graphics, audio, video and animation on  
         webpage

    c. HTML is also used to store database in websites

    d. HTML also defines a link to traverse from one page to another

34. An example of client-side scripting language is ....

 a. PHP               b. JSP             c. ASP           d. JavaScript

35. JavaScript was originally developed by ......

 a. Google      b. Microsoft      c. Sun microsystem      d. Netscape

36. What are the basic three types of data in JavaScript?

  a. Numbers, Strings and Boolean

  b. Numbers Strings and Bytes

  c. Integer, Character and Boolean

  d. Numbers, Strings and Array

37. Greater than equal to >= is an example of

 a. Assignement    b. Comparaison     c. Logical     d. Conditional

38. Which of the following statement is not a type of looping statement?

 a. do while         b. for         c. switch case           d. while

39. Which of the following is not an example of object?

  a. Array       b. String        c. Image         d. Number

40. Which of the following statement is not true?

  a. PHP is open-source scripting language

  b. PHP is client site scripting language

  c. PHP can be integrated with database like MYSQL, Oracle, Sybase and  
      MS SQL Server

  d. PHP syntax is same as C language

41. What are the two method of form access?

  a. get/post       b. give/take      c. input/output           d. get/set

44. Which Program Copies The Databases From One Server To Another?

 a. Mysqlcopydb      b. Mysqldbcopy     c. Mysqlflushdb   d. Mysqldbflush

45. Which among the following have the maximum bytes?

  a. varchar     b. char     c. Both varchar and char      d. Text Type

46. The "father" Of MYSQL......

  a. Bill Joy     b. Michael Widenius     c. Bill Gates      d. Stephanie Wall

47. Read all MCQ from Asmita Textbook

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home