I am posting here the Recent Elance And Odesk Javascript TestPapers.All are from odesk testpapers and I have given the answers too . If you have any other answer than the answer I have given please comment with the question number so that I can update if your answer will more appropriate than mine.I can say here that below quastions with answer is not only for odesk and elance javascript but it also for other examination of javascript papers. Also I can give you one tips here that before giving any exam or online exam please make sure you have read all the terms and policies with question pattern as well.So best of prepare well with below questions and answers then go for examination.

Q:1)which of the following will implement a singleton pattern in Javascript?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
a) <script> var anobject = { method1: function () { // ... }, method2: function () { // ... } }; </script> b) <script> function method1() { // ... } function method2() { // ... } </script> c) <script> var method1 = function() { // ... } var method2 = function() { // ... } </script> d) <script> var method1; var method2; </script> |
Q:2)which of the following determines whether cookies are enabled in a browser or not?
1 2 3 4 |
a)(navigator.cookie)? true : false b)(application.cookieEnabled)? true : false c)(navigator.cookieEnabled)? true : false d)(applicatio.cookie)? true : false |
Q:3)which of the following choices will change the source of the image to “image2.gif” when a user clicks on the image?
1 2 3 4 |
a)img.id="imageID" src="image1.gif" width="50" height="60" onuousedown="changeimg(image1.gif)" onnouseup="changeimg(image2.gif)"; b)img.id="imageID" src="image1.gif" width="50" height="60" onuousedown="changeimg(image2.gif)" onnouseup="changeimg(image1.gif)"; c)img.id="imageID" src="image2.gif" width="50" height="60" onuousedown="changeimg(image1.gif)" onnouseup="changeimg(image2.gif)"; d)img.id="imageID" src="image2.gif" width="50" height="60" onuousedown="changeimg(image2.gif)" onnouseup="changeimg(image1.gif)"; |
Q:4)In an HTML page, the form tag is defined as follows:
(form onSubmit=”return Validate()” action=”http://www.mysite.com/”>
The validate() function is intended to prevent the form from being submitted if the name field in the form is empty. what should the validate() function look like?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
a) <script type="text/javascript"> function Va1idate() { if(document.forms[0].name.value == ") return true; else return false; } </script> b) <script type="text/javascript"> function Va1idate() { if(document.forms[0].name.value == ") return false; else return true; } </script> c) <script type="text/javascript"> function Validate() { if(document.forms[0].name == ") return false; else return true; } </script> d) <script type="text/javascript"> function Va1idate() { if(document.forms[0].name == ") return true; else return false; } </script> |
5)Consider the following code snippet:
1 2 3 |
var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; What is the best way to remove the property "regex", so the result would be this code snippet? myJSONObject == {"ircEvent": "PRIVMSG", "method": "newURI"}; |
1 2 3 4 |
a)myJSONObject.regex.delete; b)myJSONObject.regex.remove; c)delete myJSONObject.regex; d)remove myJSONObject.regex; |
6)which of the following Javascript Regular Expression modifiers finds one or more occurrences of a specific character in a string ?
1 2 3 4 |
a) ? b) * c) + d) # |
7)which of the following Array Ilethods in Javascript runs a function on every itell in the Array and collects the result from previous calls, but in reverse?
1 2 3 4 |
a)reduce() b)reduceRight() c)reverse() d)pop() |
8)what will be output of the following code?
1 2 3 4 5 6 7 8 9 10 11 12 |
function testGenerator() { yield "first"; document.write('step1'); yield "second"; document.write('step2'); yield "third"; document.write('step3'); } var g = testgenerator(); document.write(g.next()); document.write(g.next()); |
1 2 3 4 |
a)firststep1second b)step1step2 c)step1 d)step1step2step3 |
9)which of the following code snippets changes an image on the page?
1 2 3 4 5 6 7 8 9 10 11 12 |
a) var img = document.getElementById("imageId"); img.src = "newImage.gif"; b) var img = document.getElementById("imageId"); img.style.src = "newImage.gif"; c) var img = document.getElementById("imageId"); img.src.value = "newImage.gif"; d) var img = document.getElementById("imageId"); img = "newImage.gif"; |
10)which of the following choices will remove a selection option from the code below?
1 2 3 4 5 6 7 |
<button id="newbtn" onclick='g();'>remove</button> <select nane='selectBox" id="selectBox"> <option value='option1'>option1</option> <option value='option2'>option2</option> <option value='option3'>option3</option> <option value='option4'>option4</option> </select> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
a) <script> function g(){ var index = 1; var d = document.getElenentById('selectBox"); var d_nested = d.chi1dNodes[index]; var throwawaynode = d.deleteChild(d_nested); } </script> b) <script> function g(){ var index = 1; var d = document.getElenentById('selectBox"); var d_nested = d.chi1dNodes[index]; var throwawaynode = d.clearChild(d_nested); } </script> c) <script> function g(){ var index = 1; var d = document.getElenentById('selectBox"); var d_nested = d.chi1dNodes[index]; var throwawaynode = d.removeChild(d_nested); } </script> d) <script> function g(){ var index = 1; var d = document.getElenentById('selectBox"); var d_nested = d.chi1dNodes[index]; var throwawaynode = d.emptyChild(d_nested); } </script> |
11)which of the following code snippets shows an alert for an empty string?
var a = “”;
1 2 3 4 |
a)If(a){ alert(‘This is empty string’);} b)If(a == NUL){ alert( ‘This is empty string’) ;} c)If ( !a){ alert( ‘This is empty string’) ; } d)If(a.empty){ a1ert(‘This is empty string’);} |
12)How can created cookies be deleted using Javascript?
1 2 3 4 5 6 |
a)They can't be deleted. They are valid until they expire. b)Overwrite with an expiry date in the past c)Use escape() on the value of the path attribute d)Use unescape() on the value of the path attribute e)The cookie file will have to be removed from the client machine. f)wait till the expiry date is reached |
13)Consider the following image definition:
1 |
<img id="logo" src="companylogo1.gif" height="12" width="12" > |
which of the following will change the image to companylogo2.gif when the page loads ?
1 2 3 4 |
a)logo.source="companylogo2.gif" b)logo.source="companylogo1.gif" c)document.getElenentById('logo').src="companylogo1.gif" d)document.getElenentById('logo').src="companylogo2.gif" |
14)Consider the following Javascript validation function:
1 2 3 4 5 6 7 |
function ValidateField() { if(docunent.forms[0].txtId.value ==""){ return false; } return true; } |
which of the following options will call the function as soon as the user leaves the field?
1 2 3 4 |
a)input nane=txtId type="text" onreset="return ValidateField()" b)input nane=txtId type="text" onfocus="return ValidateField()" c)input nane=txtId type="text" onsubmit="return ValidateField()" d)input nane=txtId type="text" onblur="return ValidateField()" |
15)which of the following modifiers Ilust be set if the Javascript lastlndex object property was used during pattern matching?
1 2 3 4 |
a) i b) m c) g d) s |
16)Consider the three variables:
1 2 3 4 |
someText = 'JavaScript1.2'; pattern = /(\w+)(\d)\.(\d)/i; outcone = pattern.exec(someText); what does pattern.ignoreCase contain? |
1 2 3 4 |
a)true; b)false c)undefined d)null |
17)which of the following is not a valid method in generator-iterator objects in Javascript ?
1 2 3 4 |
a)send() b)throw() c)next() d)stop() |
18)which of the following options can be used for adding direct support for XML to Javascript?
1 2 3 4 |
a)E4X b)Regex c)Generators and Iterators d)let |
19)which of the following code snippets will toggle a div element’s background color?
1 2 |
<button id="toggle">Togg1e</button> <div id="terd">Change Background Color.</div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
a) <script) var button = document.getEleuentById('toggle'); button.click = function<> { terd.sty1e.backgroundColor = terd.style.backgroundColor == 'blue' ? 'red' : 'blue'; }; </script> b) <script) var button = document.getEleuentById('toggle'); button.ready = function<> { terd.sty1e.backgroundColor = terd.style.backgroundColor == 'blue' ? 'red' : 'blue'; }; </script> c) <script) var button = document.getEleuentById('toggle'); button.focus = function<> { terd.sty1e.backgroundColor = terd.style.backgroundColor == 'blue' ? 'red' : 'blue'; }; </script> d) <script) var button = document.getEleuentById('toggle'); button.onclick = function<> { terd.sty1e.backgroundColor = terd.style.backgroundColor == 'blue' ? 'red' : 'blue'; }; </script> |
20)which of the following is not a valid method for looping an array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
a) var a= [1,2]; for (var i = 0; i < a.length; i++) { a1ert(a[i]); } b) var a= [1,2]; a.forEach( function(item) { alert(item); }) c) var a= [1,2]; a.nap( function(item) { a1ert(item); }) d) var a= [1,2]; a.loop( function(item) { a1ert(item); }) |
21)which of the following choices shows the correct result for the code below?
1 2 3 4 5 6 7 8 9 |
var arr = []; arr[0] = "Jani“; arr[1] = "Hege“; arr[2] = "Stale"; arr[3] = "Kai Jim‘; arr[4] = "Barge"; conso1e.log(arr.join()); arr.sp1ice(2, 0, 'Lene"); conso1e.log(arr.join()); |
1 2 3 4 5 6 7 8 9 10 11 12 |
a) Jani,Hege,Stale,Kai Jiu,Borge Lene,Jani,Hege,Sta1e,Kai Jiu,Borge b) Jani,Hege,Stale,Kai Jiu,Borge Jani,Hege,Lene,Sta1e,Kai Jiu,Borge c) Jani,Hege,Sta1e,Kai Jiu,Borge Jani,Hege,Sta1e,Kai Jin,Lene,Borge d) Jani,Hege,Stale,Kai Jiu,Borge Jani,Hege,Sta1e,Kai Jin,Borge |
22)A form contains two fields nailed idl and id2. How can you copy the value of the id2 field to idl?
1 2 3 4 |
a)document.forms[0].idl.value=document.forms[0].id2.value b)document.forms[0].id2.value=document.forms[0].id1.value c)document.idl.value=document.id2.value d)document.id2.value=document.id1.value |
23)which of the following will include a Javascript file in another Javascript file?
Note: There may be more than one right answer.
1 2 3 4 |
a)Adding a script tag with the script URL in the HTML b)Loading it with an AJAX call then using eval c)Using 'import' operator d)Using ‘include’ operator |
24)when setting cookies with Javascript, what will happen to the cookies.txt data if the file exceeds
the maximum size?
1 2 3 4 |
a)The script automatically generates a run-tine error. b)The script automatically generates a load-tine error. c)All processes using document.cookie are ignored. d)The file is truncated to the uaxinuu length. |
25)An image tag is defined as follows:
1 |
<img id="ERImage" width="100" height="100" onMouseover="ImageChange()" src="Image1.jpg"> |
The purpose of the ImageChange() function is to change the image source to Image2.jpg. which of the
following should the ImageChange() function look like?
1 2 3 4 |
a)document.getE1ementById('ERImage').src="Image1.jpg" b)document.getE1ementById('ERImage').src="Image2.jpg" c)document.getE1ementById('ERImage').style.src="Image1.jpg" d)document.getE1ementById('ERImage').style.src="Image2.jpg" |
26)Consider the following Javascript function to change the color of the text box named txtName:
1 2 3 |
function color(col) { document.forms[0].txtName.style.background=col; } |
which of the following will change the color of the text box to green, as long as the user is
pressing a key?
1 2 3 4 |
a)input type="text" onkeydown="color('white')" onkeyup="co1or('green')" name="txtName" b)input type="text" onkeydown="color('green')" onkeyup="co1or('white')" name="txtName" c)input type="text" onkeydown="color('white')" name="txtName" d)input type="text" onkeydown="color('green')" name="txtName" |
27)which of the following functions determines whether the beginning of the paralll instance matches the
param2?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
a) function (paraml, param2) { return param1.indexOf(param2) == 0; }; b) function (paraml, param2) { return param2.indexOf(param1) == 0; }; c) function (paraml, param2) { return param1.startsWith(param2) == 0; }; d) function (paraml, param2) { return param2.startsWith(param1) == 0; }; |
28)which of the following code snippets trims whitespace from the beginning and end of the given string
str?
1 2 3 4 |
a)str. rep1ace(/^\s+|\s+$/g, '') ; b)str. rep1ace(/^\s+/g, '') ; c)str. rep1ace(/\s+$/g, '') ; d)str. rep1ace(/\s+/g, '') ; |
29)In Javascript, the encodeURI() function is used to encode special characters. Which of the following
special characters is/are an exception to that rule?
A.£
B.€
C. Q
D. $
1 2 3 4 5 6 |
a)A b)B c)C d)D e)A and B f)C and D |
30)which of the following options is used to access the attributes in E4)(?
1 2 3 4 |
a)@ b):: c)# d)* |
31)which of the following correctly sets a class for an element?
1 2 3 4 |
a)document.getE1ementById(elementId).className = "Someclass"; b)document.getE1ementById(elementId).class = "Someclass"; c)document.getE1ementById(elementId).setAttribute("className","Someclass"); d)document.getE1ementById(elementId).style = "Someclass"; |
32)which of the following statements regarding this String prototype is correct?
1 2 3 |
String.prototype.doSomething = function(suffix) { return this.indexOf(suffix, this.1ength - suffix.1ength) !== -1; }; |
1 2 3 4 |
a)This method deternines whether or not a string ends with another string. b)This method determines whether or not a string begins with another string. c)This method returns the position of the last occurrence of a specified value in a string. d)This method returns the position of the first occurrence of a specified value in a string. |
33)what would be the default setting for the expires attribute of the document.cookie property?
1 2 3 4 5 |
a)The duration of the browser session b)The duration the current document stays loaded c)Twenty-four hours from the tine the cookie is set d)There is no default setting e)The duration for which the machine is on |
34)which of the following code snippets is more efficient, and why?
1 2 3 4 5 6 7 8 9 |
<script language="JavaScript"> for(i=0;i<docunent.images.1ength;i++) document.images[i].src='b1ank.gif“; </script> <script language="JavaScript"> var theimages = docunent.images; for(i=0;i<theimages.1ength;i++) theimages[i].src='b1ank.gif" </script> |
1 2 3 4 |
a)Both are equally efficient. b)The first code is more efficient as it contains less code. c)The first code is more efficient as it employs object caching. d)The second code is more efficient as it employs object caching. |
35)what value would Javascript assign to an uninitialized array element?
1 2 3 4 5 |
a)NAN b)NULL c)undefined d)false e)0 |
36)what value would Javascript assign to an uninitialized variable?
1 2 3 4 |
a)NaN b)undefined c)NULL d)false |
37)How can a Javascript object be printed?
1 2 3 4 |
a)console.1og(0bj) a)console.print(0bj) a)console.echo(0bj) a)none of these |
38)which of the following will change the color of a paragraph’s text to blue when a user hovers over
it, and reset it back to black when the user hovers out?
1 2 3 4 5 |
a)<p onmouseover="sty1e.color='black'" onmouseout='sty1e.color='blue"> The text of theparagraph..</p> b)<p onmouseover="sty1e.color='blue'" onmouseout='sty1e.color='black"> The text of theparagraph..</p> c)<p onmouseout='sty1e.color='blue"> The text of theparagraph..</p> d)<p onmouseover="sty1e.color='blue'"> The text of theparagraph..</p> e)<p onmousein="sty1e.color='blue'" onmouseout='sty1e.color='black"> The text of theparagraph..</p> |
where are the solutions?