JavaScript Interview Questions with Answers Page III


From freshersonline.com

Jump to: navigation, search

Interview Question Home


1. What does the EnableViewStateMac setting in an aspx page do?

Setting EnableViewStateMac=true is a security measure that allows ASP.NET to ensure that the viewstate for a page has not been tampered with. If on

Postback, the ASP.NET framework detects that there has been a change in the value of viewstate that was sent to the browser, it raises an error -

Validation of viewstate MAC failed.

Use <%@ Page EnableViewStateMac="true"%> to set it to true (the default value, if this attribute is not specified is also true) in an aspx page.


2. How to Accessing Elements using javascript?

To do something interesting with HTML elements, we must first be able to uniquely identify which element we want. In the example

<body>

<form action="">

<input type="button" id="useless" name="mybutton" value="doNothing" />

</form>

</body>


We can use the "getElementById" method (which is generally preferred)

document.getElementById("useless").style.color = "red";

or we can use the older hierarchical navigation method,

document.forms[0].mybutton.style.color = "blue";

Notice that this uses the "name" attribute of the element to locate it.

  1. Example of Accessing Elements in a DOM.


<script type="text/javascript" >

function showStatus() {

var selectWidget = document.forms.beerForm.elements["beer"];

var myValue = selectWidget.options[selectWidget.selectedIndex].value;

alert('You drank a \"'+ myValue +"\"");

return true;

}

</script>


<form name="beerForm" action="">

<select name="beer">

<option selected="selected">Select Beer</option>

<option>Heineken</option>

<option>Amstel Light</option>

<option>Corona</option>

<option>Corona Light</option>

<option>Tecate</option>

</select>


<input type="button" name="submitbutton" value="Drink"

onclick="showStatus()" />

</form>


3. What looping structures are there in JavaScript?

for, while, do-while loops, but no foreach.


4. To put a "close window" link on a page ?

<a href='javascript:window.close()' class='mainnav'> Close </a>


5. How to hide javascript code from old browsers that dont run it?

Use the below specified style of comments <script language=javascript> or Use the <NOSCRIPT>some html code

</NOSCRIPT> tags and code the display html statements between these and this will appear on the page if the browser does not support javascript


6. How to comment javascript code?

Use // for line comments and

/*

  • / for block comments


7. Name the numeric constants representing max,min values

Number.MAX_VALUE

Number.MIN_VALUE


8. What does javascript null mean?

The null value is a unique value representing no value or no object.

It implies no object,or null string,no valid boolean value,no number and no array object.


9. How do you create a new object in JavaScript?

var obj = new Object(); or var obj = {};


10. How do you assign object properties?

obj["age"] = 17 or obj.age = 17.


11. What’s a way to append a value to an array?

arr[arr.length] = value;


12. What is this keyword?

It refers to the current object.


13. What does the term sticky session mean in a web-farm scenario? Why would you use a sticky session? What is the potential disadvantage of using

a sticky session?

Sticky session refers to the feature of many commercial load balancing solutions for web-farms to route the requests for a particular session to the same

physical machine that serviced the first request for that session. This is mainly used to ensure that a in-proc session is not lost as a result of

requests for a session being routed to different servers. Since requests for a user are always routed to the same machine that first served the request

for that session, sticky sessions can cause uneven load distribution across servers.


14. You have an ASP.NET web application running on a web-farm that does not use sticky sessions - so the requests for a session are not guaranteed

to be served the same machine. Occasionally, the users get error message Validation of viewstate MAC failed. What could be one reason that is causing

this error?

The most common reason for this error is that the the machinekey value in machine.config is different for each server. As a result, viewstate encoded by

one machine cannot be decoded by another. To rectify this, edit the machine.config file on each server in the web-farm to have the same value for

machinekey.


15. To set all checkboxes to true using JavaScript?

//select all input tags

function SelectAll() {

var checkboxes = document.getElementsByTagName("input");

for(i=0;i<checkboxes.length;i++) {

if(checkboxes.item(i).attributes["type"].value == "checkbox") {

checkboxes.item(i).checked = true;

}

}

}


16. How to select an element by id and swapping an image ?

...

<script language="JavaScript" type="text/javascript" >

function setBeerIcon() {


var beerIcon = document.getElementById("beerIcon");

beerIcon.src = "images/"+getSelectValue("beer")+".jpg";

}

</script>

...

<img border="0" src="" id="brandIcon" alt="brand" />


<select name="beer" id="beer" onChange="setButton();setBeerIcon();">

<option value="--Select--">Select beer</option>

<option value="heineken">heineken</option>

<option value="sol">sol</option>

<option value="amstellight">amstellight</option>

<option value="coronalight">coronalight</option>

<option value="coronaextra">coronaextra</option>

<option value=""></option>

</select>


17. What does undefined value mean in javascript?

Undefined value means the variable used in the code doesnt exist or is not assigned any value or the property doesnt exist.


18. What is the difference between undefined value and null value?

(i)Undefined value cannot be explicitly stated that is there is no keyword called undefined whereas null value has keyword called null

(ii)typeof undefined variable or property returns undefined whereas typeof null value returns object


19. What is variable typing in javascript?

It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows

example

i = 10;

i = "string";

This is called variable typing


20. Does javascript have the concept level scope?

No.Javascript does not have block level scope,all the variables declared inside a function possess the same level of scope unlike c,c++,java.


21. What are undefined and undeclared variables?

Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if

undeclared variables are assigned then implicit declaration is done .

Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called

undefined value.


22. What is === operator ?

==== is strict equality operator ,it returns true only when the two operands are having the same value without any type conversion.


23. How to find the selected radio button immediately using the 'this' variable?

<script>

function favAnimal(button) {

alert('You like '+button.value+'s.');

}

</script>

<input type="radio" name="marsupial" value="kangaroo"

onchange="favAnimal(this)">Kangaroo


<input type="radio" name="marsupial" value="Opossum"

onchange="favAnimal(this)">Opossum


<input type="radio" name="marsupial" value="Tasmanian Tiger"

onchange="favAnimal(this)">Tasmanian Tiger


24. How to find radio button selection when a form is submitted?

<script type="text/javascript">

function findButton() {

var myForm = document.forms.animalForm;

var i;

for(i=0;i<myForm.marsupial.length; i++) {

if(myForm.marsupial[i].checked) {

break;

}

}

alert("You selected \""+myForm.marsupial[i].value+"\".");

}

</script>

<form name="animalForm" action="">

<input type="radio" name="marsupial" value="kangaroo" />Kangaroo


<input type="radio" name="marsupial" value="Opossum" />Opossum


<input type="radio" name="marsupial" value="Tasmanian Tiger" />Tasmanian Tiger


<input type="button" name="GO" value="GO" onclick="findButton()" />


25.How to disable an HTML object

document.getElementById("myObject").disabled = true;


26. How to associate functions with objects using JavaScript?

Let's now create a custom "toString()" method for our movie object. We can embed the function directly in the object like this.


<script type="text/javascript">

function movie(title, director) {

this.title = title;

this.director = director;

this.toString = function movieToString() {

return("title: "+this.title+" director: "+this.director);

}

}

var narnia = new movie("Narni","Andrew Adamson");

document.write(narnia.toString());

</script>

This produces

title: Narni director: Andrew Adamson


Or we can use a previously defined function and assign it to a variable. Note that the name of the function is not followed by parenthisis, otherwise

it would just execute the function and stuff the returned value into the variable.


<script type="text/javascript">

function movieToString() {

return("title: "+this.title+" director: "+this.director);

}

function movie(title, director) {

this.title = title;

this.director = director;

this.toString = movieToString; //assign function to this method pointer

}

var aliens = new movie("Aliens","Cameron");

document.write(aliens.toString());

</script>

This produces

title: Aliens director: Cameron


27. eval()?

The eval() method is incredibly powerful allowing you to execute snippets of code during exection.


<script type="text/javascript">

var USA_Texas_Austin = "521,289";

document.write("Population is "+eval("USA_"+"Texas_"+"Austin"));

</script>

This produces

Population is 521,289


28. What does break and continue statements do?

Continue statement continues the current loop (if label not specified) in a new iteration whereas break statement exits the current loop.


29.How to create a function using function constructor?

The following example illustrates this

It creates a function called square with argument x and returns x multiplied by itself.

var square = new Function ("x","return x*x");


30. What's Prototypes for JavaScript?

Objects have "prototypes" from which they may inherit fields and functions.

<script type="text/javascript">

function movieToString() {

return("title: "+this.title+" director: "+this.director);

}

function movie(title, director) {

this.title = title;

this.director = director || "unknown"; //if null assign to "unknown"

this.toString = movieToString; //assign function to this method pointer

}

movie.prototype.isComedy = false; //add a field to the movie's prototype

var officeSpace = new movie("OfficeSpace");

var narnia = new movie("Narni","Andrew Adamson");

document.write(narnia.toString());

document.write("

Narnia a comedy? "+narnia.isComedy);

officeSpace.isComedy = true; //override the default just for this object

document.write("

Office Space a comedy? "+officeSpace.isComedy);

</script>

Personal tools