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

Solved Question Bank of CSS 2

Css question bank

Uploaded by

shrutiarya029
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Solved Question Bank of CSS 2

Css question bank

Uploaded by

shrutiarya029
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Solved Question bank of CSS

1. **Break and Continue Statements**

**Usage**:
- **`break`**: Exits the nearest loop (`for`, `while`, `do-while`) or `switch`
statement.
- **`continue`**: Skips the rest of the current loop iteration and proceeds to
the next iteration.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Break and Continue Example</title>
</head>
<body>
<script>
document.write("Using break:<br>");
for (let i = 1; i <= 5; i++) {
if (i === 3) break; // Exit loop when i is 3
document.write(i + " ");
}

document.write("<br>Using continue:<br>");
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // Skip the rest of this iteration when i is 3
document.write(i + " ");
}
</script>
</body>
</html>
```
2. **Client-Side vs. Server-Side Scripting**

- **Client-Side Scripting**: Runs in the user's browser. Example: JavaScript


for dynamic content.
- **Server-Side Scripting**: Runs on the web server. Example: PHP for
dynamic content generation.

**Client-Side Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Client-Side Scripting Example</title>
</head>
<body>
<button onclick="document.write('Hello from client-side script!')">Click
Me</button>
</body>
</html>
```

**Server-Side Example** (PHP):

```php
<!-- hello.php -->
<!DOCTYPE html>
<html>
<head>
<title>Server-Side Scripting Example</title>
</head>
<body>
<?php
echo "Hello from server-side script!";
?>
</body>
</html>
3. **Function Definition Expression**

A function expression defines a function as part of an expression, often


assigned to a variable.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Function Expression Example</title>
</head>
<body>
<script>
const greet = function() {
document.write("Hello from a function expression!");
};
greet();
</script>
</body>
</html>
```

---

4. **Switch Case**

The `switch` statement executes different code blocks based on the value of
a variable.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Switch Case Example</title>
</head>
<body>
<script>
let dayNumber = 2;

switch (dayNumber) {
case 1:
document.write("Monday<br>");
break;
case 2:
document.write("Tuesday<br>");
break;
default:
document.write("Invalid day number<br>");
}
</script>
</body>
</html>
```

---

5. **Object in JavaScript**

An object is a collection of key-value pairs.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Object Example</title>
</head>
<body>
<script>
const person = {
firstName: 'John',
lastName: 'Doe',
greet() {
return `Hello, ${this.firstName} ${this.lastName}`;
}
};

document.write(person.greet());
</script>
</body>
</html>
```

---

6. **Getters and Setters**

Getters and setters allow custom behavior when getting or setting property
values.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Getters and Setters Example</title>
</head>
<body>
<script>
const person = {
firstName: '',
lastName: '',

get fullName() {
return `${this.firstName} ${this.lastName}`;
},

set fullName(name) {
const parts = name.split(' ');
this.firstName = parts[0] || '';
this.lastName = parts[1] || '';
}
};

person.fullName = 'Jane Doe';


document.write('Full Name: ' + person.fullName);
</script>
</body>
</html>
```

---

7. **Select Element of Form Tag**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Select Element Example</title>
</head>
<body>
<form>
<label for="fruit">Choose a fruit:</label>
<select id="fruit" name="fruit">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
<button type="button" onclick="showSelectedFruit()">Show Selected
Fruit</button>
</form>

<script>
function showSelectedFruit() {
const fruit = document.getElementById('fruit').value;
document.write('Selected fruit: ' + fruit);
}
</script>
</body>
</html>
```

---

8. **String Methods**

1. **`charCodeAt()`**: Returns the Unicode value of a character at a


specified index.
2. **`fromCharCode()`**: Converts Unicode values to characters.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>String Methods Example</title>
</head>
<body>
<script>
const str = 'Hello';
document.write("Unicode value of 'H': " + str.charCodeAt(0) + "<br>");
document.write("Character from Unicode 72: " +
String.fromCharCode(72));
</script>
</body>
</html>
```

---

9. **`splice()` Method**

**Usage**: Adds/removes items from an array.

**Syntax**:

```javascript
array.splice(start, deleteCount, item1, item2, ...);
```

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Splice Method Example</title>
</head>
<body>
<script>
let arr = [1, 2, 3, 4, 5];
arr.splice(2, 1, 6, 7); // Starts at index 2, removes 1 item, adds 6 and 7

document.write('Array after splice: ' + arr);


</script>
</body>
</html>
```

---

10. **Convert Character to Unicode**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Character to Unicode Example</title>
</head>
<body>
<script>
const char = 'A';
document.write("Unicode of '" + char + "': " + char.charCodeAt(0));
</script>
</body>
</html>
```

---

11. **Armstrong Number**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Armstrong Numbers Example</title>
</head>
<body>
<script>
for (let num = 1; num <= 100; num++) {
let sum = 0;
let temp = num;
while (temp > 0) {
let digit = temp % 10;
sum += digit * digit * digit;
temp = Math.floor(temp / 10);
}
if (sum === num) {
document.write(num + " is an Armstrong number<br>");
}
}
</script>
</body>
</html>
```

---

12. **Greatest Amongst 3 Numbers**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Greatest Number Example</title>
</head>
<body>
<script>
const num1 = 5;
const num2 = 8;
const num3 = 3;

const greatest = Math.max(num1, num2, num3);


document.write("Greatest number is: " + greatest);
</script>
</body>
</html>
```

---

13. **Palindrome Number**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Palindrome Number Example</title>
</head>
<body>
<script>
function isPalindrome(num) {
const str = num.toString();
const reversed = str.split('').reverse().join('');
return str === reversed;
}

const number = 121;


document.write(number + " is " + (isPalindrome(number) ? "a
palindrome" : "not a palindrome"));
</script>
</body>
</html>
```

---

14. **If Statement**

**Usage**: Executes code based on a condition.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>If Statement Example</title>
</head>
<body>
<script>
const age = 18;

if (age >= 18) {


document.write("You are an adult.");
} else {
document.write("You are a minor.");
}
</script>
</body>
</html>
```

---

15. **`concat()` vs `join()`**


- **`concat()`**: Merges two or more arrays.
-

**`join()`**: Joins all elements of an array into a string.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Concat vs Join Example</title>
</head>
<body>
<script>
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const combined = array1.concat(array2); // Merges arrays
document.write("Concatenated array: " + combined + "<br>");

const joined = combined.join('-'); // Joins array elements into a string


document.write("Joined string: " + joined);
</script>
</body>
</html>
```

---

16. **Data Types in JavaScript**

1. **Number**: Numeric values (e.g., 1, 3.14).


2. **String**: Text values (e.g., "Hello").
3. **Boolean**: True or false values.
4. **Object**: Key-value pairs.
5. **Array**: Ordered list of values.
6. **Null**: Represents no value.
7. **Undefined**: Variable declared but not initialized.
8. **Symbol**: Unique identifier.

---

17. **Object in JavaScript** (Repetition from 5)

Refer to the example provided in point 5.

---

18. **Sorting Students and Calculating Average**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Student Sorting Example</title>
</head>
<body>
<script>
const students = [
{ name: 'Amit', marks: 70 },
{ name: 'Sumit', marks: 78 },
{ name: 'Abhishek', marks: 71 }
];

// Sort students by marks


students.sort((a, b) => b.marks - a.marks);

// Calculate average
const totalMarks = students.reduce((sum, student) => sum +
student.marks, 0);
const average = totalMarks / students.length;

// Display sorted students and average


document.write("Students sorted by marks:<br>");
students.forEach(student => {
document.write(student.name + ": " + student.marks + "<br>");
});

document.write("<br>Average marks: " + average);


</script>
</body>
</html>
```

---

19. **Check First Character Uppercase**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Check Uppercase Example</title>
</head>
<body>
<script>
function isFirstCharUppercase(str) {
return str.charAt(0) === str.charAt(0).toUpperCase();
}

document.write("Is 'Hello' starting with uppercase? " +


isFirstCharUppercase('Hello'));
</script>
</body>
</html>
```

---

20. **Merge Arrays and Remove Duplicates**

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Merge Arrays Example</title>
</head>
<body>
<script>
function mergeAndRemoveDuplicates(arr1, arr2) {
const merged = arr1.concat(arr2);
return [...new Set(merged)];
}

const array1 = [1, 2, 3, 4];


const array2 = [3, 4, 5, 6];
const result = mergeAndRemoveDuplicates(array1, array2);

document.write("Merged array without duplicates: " + result);


</script>
</body>
</html>
```

---
21. **String Definition**

A string is a sequence of characters enclosed in quotes.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>String Definition Example</title>
</head>
<body>
<script>
const str = "Hello, World!";
document.write("String: " + str);
</script>
</body>
</html>
```

---

22. **Two String Functions**

**`toUpperCase()`**: Converts string to uppercase.

**`substring(start, end)`**: Extracts a part of the string.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>String Functions Example</title>
</head>
<body>
<script>
const str = "Hello, World!";
document.write("Uppercase: " + str.toUpperCase() + "<br>");
document.write("Substring (0-5): " + str.substring(0, 5));
</script>
</body>
</html>
```

---

23. **Array Concept and Inserting Element**

An array is an ordered collection of elements.

**Example**:

```html
<!DOCTYPE html>
<html>
<head>
<title>Array Example</title>
</head>
<body>
<script>
let arr = [1, 2, 3, 4, 5];
arr.splice(2, 0, 10); // Insert 10 at index 2

document.write("Array after insertion: " + arr);


</script>
</body>
</html>
```

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy