Q1) Explain the role of JavaScript in web development.
JavaScript is a versatile programming language that plays a crucial role in web development. It
enables interactive and dynamic behavior on web pages. Key roles include:
1. Adding Interactivity: Handles user interactions like clicks, form submissions, and mouse events.
2. DOM Manipulation: Dynamically updates and manipulates the Document Object Model (DOM) to
change content or styles without reloading the page.
3. Client-Side Validation: Validates user input in forms to ensure data integrity.
4. Asynchronous Operations: Fetches data from servers using AJAX or APIs without refreshing the
page.
5. Animations and Effects: Creates engaging animations, transitions, and effects.
6. Browser Compatibility: Runs seamlessly across modern browsers to enhance user experience.
Q2) How does JavaScript interact with HTML and CSS?
JavaScript interacts with HTML and CSS to create dynamic and interactive web pages:
1. HTML Integration: JavaScript accesses and modifies HTML elements via the DOM, such as
adding, removing, or changing elements and attributes.
2. CSS Styling: It dynamically changes styles, classes, and CSS properties to create visual effects
or responsive designs.
3. Event Handling: Listens to user events (e.g., clicks, hovers) and updates HTML or CSS
accordingly.
4. Dynamic Content: Injects or updates HTML content, like adding new text, images, or components
on the fly.
Q3) Explain the concept of variable scope in JavaScript.
Variable scope defines where a variable is accessible in the code:
1. Global Scope: Variables declared outside any function or block are accessible throughout the
program.
2. Function Scope: Variables declared inside a function (var) are only accessible within that function.
3. Block Scope: Variables declared with let and const are only accessible within the block they are
defined in.
4. Lexical Scope: Inner functions can access variables of their outer functions due to closures.
Q4) What is the difference between var, let, and const in JavaScript?
| Feature | var | let | const |
|-------------------|----------------------------|------------------------|-------------------------|
| Scope | Function scope | Block scope | Block scope |
| Re-declaration | Allowed | Not allowed | Not allowed |
| Re-assignment | Allowed | Allowed | Not allowed |
| Hoisting | Hoisted with undefined | Hoisted but uninitialized | Hoisted but uninitialized |
| Use case | Rarely used (legacy) | Used for mutable values | Used for constants |
Q5) What are the different types of form controls available in HTML? Explain in detail.
HTML provides various form controls for collecting user input:
1. Text Input (<input type="text">): For single-line text input.
2. Password (<input type="password">): Hides entered text for passwords.
3. Radio Buttons (<input type="radio">): Allows one selection from multiple options.
4. Checkbox (<input type="checkbox">): Enables selecting multiple options.
5. Dropdown (<select>): Provides a list of options in a dropdown menu.
6. Textarea (<textarea>): For multi-line text input.
7. Button (<button> or <input type="submit">): For submitting forms or triggering actions.
8. File Upload (<input type="file">): Uploads files.
9. Range Slider (<input type="range">): Selects a value within a range.
10. Date/Time Pickers (<input type="date">, <input type="time">): Collects date or time input.
Q6) Explain how to validate form input using JavaScript.
Form validation ensures data correctness before submission:
1. Client-Side Checks:
- Use JavaScript to check for empty fields, valid formats, and length.
- Example:
const nameField = [Link]('name');
if ([Link]() === '') {
alert('Name is required!');
2. Regex for Patterns:
Validate emails, phone numbers, or custom patterns using regular expressions.
const email = [Link]('email').value;
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if () {
alert('Invalid email!');
3. Custom Validations:
Provide tailored messages or styling for invalid input fields.
Q7) What are the different types of statements in JavaScript?
JavaScript has various types of statements:
1. Declaration Statements: var, let, const.
2. Control Flow Statements: if, else, switch, try-catch.
3. Loops: for, while, do-while, for-in, for-of.
4. Expression Statements: a = 5, x + y.
5. Function Declaration: function myFunction() { }.
6. Return Statement: return value;.
Q8) What are object literals, and how are they used in JavaScript? Explain in detail.
Object literals are a concise way to create objects using {key: value} syntax.
Example:
const person = {
name: 'John',
age: 30,
greet: function() {
[Link](`Hello, my name is ${[Link]}`);
};
- Properties: Store values (name: 'John').
- Methods: Define functions within the object (greet()).
Usage:
- To store grouped data.
- To represent real-world entities.
Q9) Explain the difference between function declarations and function expressions.
| Feature | Function Declaration | Function Expression |
|------------------------|--------------------------------------|---------------------------------------|
| Syntax | function name() { } | const func = function() { }; |
| Hoisting | Fully hoisted | Not hoisted |
| Anonymous Functions | Not anonymous | Can be anonymous or named |
| Use Case | For reusable code declared upfront | For inline or conditional logic |
Q10) Explain the IP address and its purpose in networking.
An IP address is a unique identifier assigned to devices on a network, enabling communication:
1. Purpose: Identifies devices, ensuring correct routing of data.
2. Types: IPv4 (32-bit) and IPv6 (128-bit).
3. Structure:
- IPv4: [Link].
- IPv6: 2001:0db8:85a3:0000:0000:8a2e:0370:7334.
Q11) Differentiate IPv4 and IPv6.
| Feature | IPv4 | IPv6 |
|---------------|-------------------------------|--------------------------------|
| Address Size | 32-bit | 128-bit |
| Format | Decimal (e.g., [Link]) | Hexadecimal (e.g., fe80::1) |
| Address Space | ~4.3 billion addresses | Virtually unlimited |
| Header Size | 20 bytes | 40 bytes |
| Security | Optional (IPSec) | Built-in |
Q12) What is the purpose of a network mask? Explain in detail.
A network mask (subnet mask) defines which portion of an IP address represents the network and
which represents the host:
- Example:
IP: [Link], Mask: [Link].
- Network: [Link].
- Host: [Link].
- Purpose:
- Divides large networks into smaller subnets.
- Improves routing efficiency.
- Reduces network congestion.
Q13) Explain how to retrieve the hostname and IP address using InetAddress.
In Java, the InetAddress class provides methods for networking:
import [Link];
public class NetworkInfo {
public static void main(String[] args) throws Exception {
InetAddress address = [Link]();
[Link]("Hostname: " + [Link]());
[Link]("IP Address: " + [Link]());
- Methods:
- getHostName(): Retrieves the hostname.
- getHostAddress(): Retrieves the IP address.