0% found this document useful (0 votes)
14 views

Javascript Oct 2022 in Sem Solved

Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Javascript Oct 2022 in Sem Solved

Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Oct-22/BE/Insem-84

B.E. (Electronics & Electronics & Telecommunication Engineering)


JAVA SCRIPT (Elective - III)
(2019 Pattern) (404184 C) (Semester - VII)
[Max. Marks : 30]
Q1) a) Explain core features of JavaScript. [5]
Answer:
JavaScript is a versatile and widely-used programming language with several core features:
1. Dynamic Typing: Variables in JavaScript do not need a predefined type. The type is
determined at runtime.
2. Object-Oriented: JavaScript supports object-oriented programming concepts, including
inheritance, encapsulation, and polymorphism.
3. First-Class Functions: Functions are first-class citizens in JavaScript, meaning they can be
assigned to variables, passed as arguments, and returned from other functions.
4. Prototype-Based Inheritance: JavaScript uses prototypal inheritance, where objects inherit
properties and methods from other objects.
5. Event-Driven: JavaScript can handle events such as user actions, making it essential for
interactive web applications.
6. Asynchronous Programming: JavaScript supports asynchronous programming through
callbacks, promises, and async/await, allowing non-blocking operations.

Q1) b) Explain different script tags that can be used in JavaScript. [5]
Answer:
In HTML, the `<script>` tag is used to include JavaScript. Different ways to use the `<script>`
tag include:
1. **Inline Script**: JavaScript code written directly within the `<script>` tags.
```html
<script>
alert("Hello, World!");
</script>
```
2. **External Script**: JavaScript code included from an external file.
```html
<script src="script.js"></script>
```
3. **Async Attribute**: Loads the script asynchronously without blocking the HTML parsing.
```html
<script src="script.js" async></script>
```
4. **Defer Attribute**: Loads the script after the HTML parsing is complete.
```html
<script src="script.js" defer></script>
```

Q1) c) What are input-output functions in JavaScript? [5]


Answer:
Input-output functions in JavaScript facilitate interaction with users:
1. **alert()**: Displays a message to the user in a dialog box.
```javascript
alert("This is an alert message!");
```
2. **prompt()**: Asks the user for input and returns the input as a string.
```javascript
let name = prompt("What is your name?");
```
3. **confirm()**: Asks the user for confirmation and returns `true` if the user clicks "OK" or
`false` if the user clicks "Cancel".
```javascript
let result = confirm("Are you sure?");
```
4. **console.log()**: Outputs a message to the browser's console.
```javascript
console.log("This is a log message.");
```

Q2) a) Justify & explain how to add JavaScript code in an XHTML Document. [5]
Answer:
JavaScript can be added to an XHTML document using the `<script>` tag within the XHTML
structure. XHTML requires strict adherence to XML syntax rules. Example:
```xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML with JavaScript</title>
<script type="text/javascript">
function showAlert() {
alert("Hello, XHTML!");
}
</script>
</head>
<body>
<button onclick="showAlert()">Click Me</button>
</body>
</html>
```
XHTML documents must be well-formed, meaning all tags are properly closed, and attribute
values are quoted.
Q2) b) Elaborate Concept of Linked Scripts with an example. [5]
Answer:
Linked scripts refer to including external JavaScript files in an HTML document. This practice
promotes code reusability and separation of concerns. Example:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Linked Scripts Example</title>
<script src="script.js"></script>
</head>
<body>
<button onclick="showAlert()">Click Me</button>
</body>
</html>
```
`script.js`:
```javascript
function showAlert() {
alert("Hello from an external file!");
}

Q2) c) Analyze different flow control statements in JavaScript (JS). Explain any one with
an example. [5]
Answer:
JavaScript has several flow control statements, including:
1. **if-else**: Conditional statements to execute code blocks based on conditions.
2. **switch**: Evaluates an expression and executes code blocks based on matching case values.
3. **for**: Loop that iterates a specified number of times.
4. **while**: Loop that continues as long as a specified condition is true.
5. **do-while**: Loop that executes at least once before checking the condition.
6. **break**: Exits a loop or switch statement.
7. **continue**: Skips the current iteration of a loop and proceeds to the next iteration.

**Example: if-else**
```javascript
let num = 10;
if (num > 0) {
console.log("Positive number");
} else if (num < 0) {
console.log("Negative number");
} else {
console.log("Zero");
}

Q3) a) Write a JavaScript program to extract the values at the specified indexes from a
specified array. [5]
Answer:
```javascript
function extractValues(arr, indexes) {
let result = [];
for (let i = 0; i < indexes.length; i++) {
result.push(arr[indexes[i]]);
}
return result;
}
let array = [10, 20, 30, 40, 50];
let indexes = [1, 3, 4];
let values = extractValues(array, indexes);
console.log(values); // Output: [20, 40, 50]

Q3) b) Elaborate the type conversions used in JavaScript. [5]


Answer:
Type conversions in JavaScript are the process of converting a value from one type to another.
There are two types of type conversions:
1. **Implicit Conversion (Type Coercion)**: JavaScript automatically converts types when
necessary.
```javascript
let result = '5' + 5; // '55' (string)
result = '5' * 2; // 10 (number)
```
2. **Explicit Conversion**: Manually converting types using functions or operators.
- **String Conversion**: `String(value)`, `value.toString()`
```javascript
let num = 10;
let str = String(num); // "10"
```
- **Number Conversion**: `Number(value)`, `parseInt(value)`, `parseFloat(value)`
```javascript
let str = "20";
let num = Number(str); // 20
```
- **Boolean Conversion**: `Boolean(value)`
```javascript
let str = "hello";
let bool = Boolean(str); // true
Q3) c) Which are the termination statements in JavaScript? [5]
Answer:
Termination statements in JavaScript include:
1. **break**: Exits a loop or switch statement.
```javascript
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i); // Output: 0 1 2 3 4
}
```
2. **continue**: Skips the current iteration of a loop and proceeds to the next iteration.
```javascript
for (let i = 0; i < 10; i++) {
if (i === 5) continue;
console.log(i); // Output: 0 1 2 3 4 6 7 8 9
}
```
3. **return**: Exits a function and optionally returns a value.
```javascript
function sum(a, b) {
return a + b;
}
console.log(sum(5, 10)); // Output: 15
```
Q4) a) Write a JavaScript program to extract the values at the specified indexes from a
specified array. [5]
Answer:
This question is the same as Q3 a). Refer to the answer provided for Q3 a).

Q4) b) Create an object in JavaScript and add different properties to that object
dynamically. [5]
Answer:
```javascript
let person = {}; // Create an empty object
person.name = "John"; // Add property dynamically
person.age = 30;
person.sayHello = function() {
console.log("Hello, my name is " + this.name);
};

console.log(person); // Output: { name: 'John', age: 30, sayHello: [Function (anonymous)] }


person.sayHello(); // Output: Hello, my name is John

Q4) c) What is the typeof operator in JavaScript? List out values returned by typeof
operator. [5]
Answer:
The `typeof` operator in JavaScript returns a string indicating the type of the operand. Values
returned by `typeof` include:
1. `"undefined"`:

If the variable is undefined.


2. `"boolean"`: If the variable is a boolean.
3. `"number"`: If the variable is a number.
4. `"string"`: If the variable is a string.
5. `"object"`: If the variable is an object (including `null` and arrays).
6. `"function"`: If the variable is a function.
7. `"symbol"`: If the variable is a symbol.
8. `"bigint"`: If the variable is a bigint.

**Example:**
```javascript
console.log(typeof undefined); // "undefined"
console.log(typeof true); // "boolean"
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof null); // "object"
console.log(typeof function(){}); // "function"
console.log(typeof Symbol()); // "symbol"
console.log(typeof 10n); // "bigint"

You might also like