parajulirajesh.com.np

Rajesh Parajuli

JavaScript

+2 javascript Questions with answers

  1. What is javaScript ? Demonstrate simple javaScript example.

JavaScript is a scripting language used for creating dynamic and interactive web content.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<h2>JavaScript Example</h2>
<script>
document.write(“hello javascript”); //display on screen
alert(“hello javascript”); //display like a popup(box) message
console.log.(“hello javascript); //display on console screen after inspect the code
</script>
</body>
</html>

  1. Write a function to add any two numbers in javaScript.
<!DOCTYPE html>
<html>
<head>
    <title>JavaScript addition program</title>
</head>
<body>
    <script>
function addNumbers() {
    let a=10;
let b=20;
let sum=a+b;
document.write(“sum is: “+sum); // Output: 15
}
addNumbers(); 
 
    </script>
</body>
</html>

 

  1. Develop a javaScript program to swap/exchange the values of any two variables.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript swap program</title>
</head>
<body>
<script>
let x = 5, y = 10;
[x, y] = [y, x]; // Swaps values
document.write(x,” “+y); // Output: 10 5
</script>
</body>
</html>

  1. Write a javaScript function that checks if a number is even or odd and print the result.
<!DOCTYPE html>
<html>
<head>
    <title>JavaScript evenodd program</title>
</head>
<body>
    <script>
      function evenodd(){
      let n=parseInt(prompt(“enter number: “));
      if(n%2==0)
  {
  document.write(“the entered number is even: “+n);
  }
      else
{
document.write(“the entered number is odd: “+n);
}
}
evenodd();
    </script>
</body>
</html>
  1. Write a program to find the largest number among three numbers in javascript.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript greatestnumber program</title>
</head>
<body>
<script>
function findLargest() {
let a=5;
let b=10;
let c=2;
document.write(Math.max(a, b, c)); // Output: 10
}
findLargest();
</script>
</body>
</html>

  1. How do you add an event handler in javaScript? Give an example.

Events in JavaScript allow you to interact with user actions like clicking a button, hovering over an element, or pressing a key. You can handle events using inline methods using onclick attribute and event listeners method.

• Inline method for event handling:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<h2>JavaScript Example</h2>
<button onclick=”myfun()”>Click Me</button>
<script>
function myfun() {
alert(“Hello, JavaScript!”);
}
</script>
</body>
</html>

addEventListener() method:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<h2>JavaScript Example</h2>
<button id=”mybtn”>Click Me</button>
<script>
function myfun() {
alert(“Hello, JavaScript!”);
}
document.getElementById(“mybtn”).addEventListener(“click”, myfun);
</script>
</body>
</html>

 

  1. Write a javaScript code to calculate factorial of a given number.
<!DOCTYPE html>
<html>
<head>
    <title>JavaScript factorial</title>
</head>
<body>
   <script>
function factorial(n)
    {
    if(n==0 || n==1)
  return 1;
  else
    return n*factorial(n-1);
}
    let x=parseInt(prompt(“enter number: “));
document.write(“factorial of a number is: “+factroial(x));
   </script>
</body>
</html>
  1. Write a function to enter two numbers and find the sum of two numbers in javaScript

<!DOCTYPE html>
<html>
<head>
<title>JavaScript userinput add using function</title>
</head>
<body>
<script>
function add() {
let num1 = parseInt(prompt(“Enter first number:”));
let num2 = parseInt(prompt(“Enter second number:”));
let sum=num1+num2;
document.write(“Sum is: ” +sum);
}
add();
</script>
</body>
</html>

  1. Difference between server side and client side scripting.

Server Side

Client Side

Runs on the server

Runs in the Browsers

PHP,PYTHON, Node.js are server side languages.

JavaScript, HTML, CSS are client side scripting languages.

Server side scripting are slower in speed.

Clint side are faster in speed.

Server side scripting are more secure.

Client side scripting are less secure.

 

  1. Explain different datatypes used in javaScript.

Datatypes refers to the type of data. There are various types of datatypes in javaScript.

  • Number: let num = 10;
  • String: let text = “Hello”;
  • Boolean: let isTrue = true;
  • Array: let arr = [1, 2, 3];
  • Object: let person = {name: “John”, age: 30};
  • Undefined: let x;
  • Null: let y = null;

 

  1. What are the uses of javaScript in web development.
  • Makes websites interactive – JavaScript enables dynamic content updates, enhancing user experience.
  • Validates user input – It checks form inputs for errors before submission.
  • Creates animations – JavaScript can animate elements like sliders, pop-ups, and transitions.
  • Handles events like clicks – It responds to user actions like clicks, scrolls, and keypresses.
  • Works with APIs – JavaScript fetches and processes data from web services for real-time updates. And manymore.

 

  1. What is Jquery? Write down its uses.

JQuery is a fast, lightweight, and easy-to-use JavaScript library that simplifies DOM manipulation, event handling, animations, and AJAX interactions.

Uses of jQuery:

  1. Manipulation – Modify HTML elements with simple syntax.
  2. Simplified Event Handling – Easily handle clicks, hovers, and keyboard events.
  3. Animations & Effects – Create smooth animations like fade, slide, and hide/show.
  4. Cross-Browser Compatibility – Works consistently across different browsers.
  5. Reduces Code Length – Perform tasks in fewer lines compared to plain JavaScript.
  6. Form Validation – Validate user inputs efficiently. And many more.

 

  1. Write a javascript to display 1 to 10 using while and do-while loop.

Use while loop:
<html>
<head>
<title>javascript</title>
</head>
<body>
<script>
let i = 1;
while (i <= 10) {
document.write(i);
document.write(“<br>”);
i++;
}
</script>
</body>
</html>

Use Do-while loop:
<html>
<head>
<title>javascript</title>
</head>
<body>
<script>
let i = 1;
do
{
document.write(i);
document.write(“<br>”);
i++;
}while (i <= 10);
</script>
</body>
</html>

OOP (Object Oriented Programming)

Object Oriented programming is a programming paradigm based on the concepts of objects which can contain data and methods. Many of programming language supports the concepts of object oriented Programming. C++, Java, Python are the example programming language of OOP.

Features of Object Oriented Programming are:

  • Class
  • Object
  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction

1. Class: Class is a user-defined data type. Class is a blueprint for creating an objects. Class contains data members and member functions.

Syntax:
class className
{
access specifier:
data_type data_members;
data_type member_functions;
};

Class Example:
class student
{
public:
int rollno;
string name;
void show();
};

2.Objects: Object is an instance of a class.
Syntax:
className objectName;

Example:
student st;

3.Polymorphism: Polymorphism means “many forms.” It allows methods to perform differently based on the object that calls them.  Example: Function Overloading, Function Overriding

Types of Polymorphism:

  1. Compile-time Polymorphism
  2. Runtime Polymorphism

4.Inheritance:  The process of creating new class derived class by acquiring the properties of base class is known as inheritance. Inheritance allows a new class (derived class / child class) to acquire the properties and methods of an existing class (base class / parent class / super class).

Types of Inheritance:

  1. Single Inheritance
  2. Multi-level Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

Single Inheritance: A Derived class inherits the properties and methods of one base class.

Syntax:
class A
{
access specifier:
data_type data_members of A;
data_type member_functions of A;
};
class B: public A
{
access specifier:
data_type data_members of B;
data_type member_functions of B;
};

Multilevel Inheritance: A process of deriving a class from another derived class.
Syntax:
class A
{
data_members of class A;
member_functions of class A;
};
class B: access specifier A
{
Data_members of class B;
member_functions of class B;
};

Example:
class A
{
public:
int A_id;
string A_name;
void show();
};
class B: public A
{
public:
int B_id;
string B_name;
void display();
};

Multiple Inheritance: A derived class inherits from more than one base classes. This allows the derived class to have access to members of multiple base classes.
Syntax:
class A
{
data_members of class A;
member_functions of class A;
};
class B: {
Data_members of class B;
member_functions of class B;
};
class C: accessSpecifier A, accessSpecifier B{
Data_members of class B;
member_functions of class B;
};

Example:
class A
{
public:
int A_id;
string A_name;
void show();
};
class B
{
public:
int B_id;
string B_name;
void display();
};
class C: public A, public B
{
public:
int B_id;
string B_name;
void display();
};

Hierarchical Inheritance: Hierarchical Inheritance is a type of inheritance where multiple derived classes inherit from a single base class. It has tree like structure.

Syntax:
class A
{
data_members of class A;
member_functions of class A;
};
class B: accessSpecifier A
{
Data_members of class B;
member_functions of class B;
};
class C: accessSpecifier A
{
Data_members of class C;
member_functions of class C;
};

Example:
class A
{
public:
int A_id;
string A_name;
void show();
};
class B: public A
{
public:
int B_id;
string B_name;
void display();
};
class C: public A
{
public:
int B_id;
string B_name;
void display();
};

5.Encapsulation: Encapsulation in C++ means binding the data (variables) and the functions (methods) that work on the data together in a single unit. Class is the best example of encapsulation.

6.Abstraction: Abstraction in C++ is a fundamental concept in Object-Oriented Programming (OOP) that focuses on hiding implementation details and exposing only the essential features of an object.

Advantages of Object Oriented Programming

  1. Reusability: Code can be reused through inheritance, saving time and effort.
  2. Modularity: Programs are divided into smaller, manageable parts called objects.
  3. Flexibility: You can use polymorphism to write flexible and easily extendable code.
  4. Data Security: Encapsulation protects data by restricting direct access.
  5. Scalability: OOP makes it easy to add new features without affecting existing code.
  6. Real-world Mapping: It models programs based on real-world entities, making them intuitive.
  7. Improved Productivity: Clear structure and reusable code make development faster.
  8. Easy Maintenance: Code is organized and modular, making debugging and updates simpler.

Disadvantages of OOP Object Oriented Programming

  1. Complexity: Designing and understanding object relationships can be challenging.
  2. Performance Overhead: OOP uses more memory and processing power compared to procedural programming.
  3. Not Suitable for All Problems: Simple tasks may become unnecessarily complex with OOP.
  4. Learning Steep Curve: Concepts like inheritance, polymorphism, and encapsulation can be difficult for beginners.
  5. Code Duplication for Simple Projects: OOP might lead to more boilerplate code in small programs.
  6. Maintenance Issues with Large Projects: If not designed well, managing object interactions in big systems can become problematic.
  7. Overhead in Debugging: Tracking bugs across multiple objects and classes can be time-consuming.

Exercises (Exam Oriented)

Software project management

A software project is the step-by-step process of creating a software in a well defined, systematic and scientific manner. It starts with understanding the user needs (requirement gathering) and includes designing, building, testing, and maintaining the software, carried out according to the execution methodologies in a specified period of time to achieve intended software product.

Scroll to Top

Rajesh Parajuli.

Services: