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

CSS Practice Questions

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

CSS Practice Questions

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

1) Differentiate between local and global variable.

Ans :-

Aspect Local Variable Global Variable


Definition Variables declared within a specific Variables declared outside of any
function or block. function or block, or explicitly
attached to the global object
Scope Accessible only within the function or Accessible from anywhere in the code,
block where they are declared. including functions and blocks.
Example let x = 10; inside a function. var globalVar = 'I am global'; declared
usage outside any function.
Declaratio let or const, or var inside functions or var, let, or const declared outside
n blocks. functions or on the global object.
Memory Memory is allocated and released Memory is allocated for the duration of
when the function or block execution the page or script
ends.
Purpose Used to manage temporary data within Used for data or functionality that
specific functions or blocks. needs to be accessed across multiple
functions or scripts.

2) State the use of prompt( ) along with syntax.

Ans :- While primarily used for input, prompt() can also be used to display output by
showing a message in the prompt dialog.

- Example:

prompt("Enter your name:", "Deepak");

3) State the ways to display output in JavaScript.


Ans ;-

Method Description Example code


console.log() Outputs information to the web console.log(“Deepak”);
console, useful for debugging and
development.
alert() Displays an alert dialog box with a alert(“this is an alert”);
message and an OK button.
document.write() Writes directly to the HTML document, document.write(“Hello”);
often used for simple demonstrations.
Prompt() Displays a dialog box that prompts the var name = prompt("What's
user to input data, and returns the input your name?");
value.
4) Write a program to declare an array of 5 color names and display them.
Ans :-

<html>
<head></head>
<body>
<script type="text/javascript">
var arr=new Array ("Red", "Blue", "Green", "Pink", "Yellow");
var i;
document. write ("Array Elements are: ");
for (i=0; i<arr.length;i++)
{
document. write("<br>" + arr[i]);
}
</script>
</body>
</html>

5) State the use of if...else statement with suitable example.


Ans :- The if...else statement in JavaScript is a fundamental control flow structure that allows
you to execute different blocks of code based on a condition. It’s used for decision-
making, where you specify conditions to determine which code should be executed.

Syntax :-

if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example :-

// Define the age variable


let age = 18;

// Check if the age is 18 or older


if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
6) Describe any two features of java script language
Ans :- i) Build dynamic web pages
ii) Validate information in forms
iii) JavaScript can be used to add new elements to the page, change the text of an
existing element,

7) List various data types and describe with an example how to declare an integer
variable in java script.

Ans :- Primitive Data Types

1. Number: Represents both integer and floating-point numbers.


2. String: Represents a sequence of characters enclosed in quotes.
3. Boolean: Represents either true or false.
4. Undefined: Represents a variable that has been declared but not assigned a value.
5. Null: Represents a deliberate non-value or empty value.
6. Symbol: Represents a unique and immutable value often used as object property
keys.
7. BigInt: Represents integers with arbitrary precision.

Non-Primitive Data Types

1. Object: Represents a collection of properties and methods.


2. Array: Represents a list-like collection of values.
3. Function: Represents a callable object or a block of code that can be executed.

8) Describe substr ( ) method with example.


Ans :- The substr() method in JavaScript is used to extract a portion of a string, based on
specified start and length values. It returns a new string containing the characters from
the original string starting at a given position and continuing for a specified number of
characters

Example :-

<html>
<head></head>
<body>
<script type="text/javascript">
let str = "Deepak";
let result = str.substr(2);
document.write(result);
</script>
</body>
</html>
Output :- epak

9) Write the use of push() and join() methods of an array

Ans :-

push() :- The push() method adds one or more elements to the end of an array and
returns the new length of the array.

Example :-

<html>
<head></head>
<body>
<script type="text/javascript">
let arr = ["Esha","Sanjana","Shweta","Meena"];
let result = arr.push("Sneha","Vaibhavi");
document.write(arr);
document.write("<br>"+result);
</script>
</body>
</html>

Join() :- The join() method creates and returns a new string by concatenating all
elements of an array, separated by a specified separator.

Example :-

<html>
<head></head>
<body>
<script type="text/javascript">
let arr = ["Esha","Sanjana","Shweta","Meena"];
let result = arr.join("&");
document.write(result);
</script>
</body>
</html>
10) Write JavaScript code to create an array with five names of fruits

Ans :-

<html>
<head></head>
<body>
<script type="text/javascript">
var arr = new Array("Apple","Banana","Dragon Fruit","Pineapple","Mango");
var i;
document.write("Array elements are :");
for(i=0; i<arr.length; i++)
{
document.write("<br>"+arr[i]);
}
</script>
</body>
</html>

11) Write a java script code to use a function to multiply two given integers.

Ans:-

<html>
<head>
<script type="text/javascript">
function mul(a,b)
{
var result=a*b;
return result;
}
</script>
</head>
<body>
<script type="text/javascript">
var no1=prompt("Enter first number");
var no2=prompt("Enter second number");
var r=mul(no1,no2);
document.write("Multiplication = " + r);
</script>
</body>
</html>

12) Describe the term “DOM” with it’s use.

Ans :-DOM stands for Document Object Model. It's a programming interface for web
documents. When a webpage is loaded into a web browser, the browser creates a DOM
representation of the page in memory. This representation allows JavaScript to access and
manipulate the structure, content, and style of the document.

You might also like