Friday, December 11, 2015

JavaScript Upwork test answers of 2016.

In this post you can find Complete and recently updated Correct Question and answers of JavaScript.All Answers updated regularly with new questions.upwork JavaScript test answers of 2016.



Question:* Which of the following is true about setTimeOut()?

Answer: • The statement(s) it executes run(s) only once.

Question:* How can the operating system of the client machine be detected?

Answer: • Using the navigator object

Question:* Which of the following prints "AbBc"?

Answer: • var b = 'a'; var result = b.toUpperCase() + 'b' + 'b'.toUpperCase() +'C'['toLowerCase'](); alert(result);

Question:* Which of the following descriptions is true for the code below? var object0 = {}; Object.defineProperty(object0, "prop0", { value : 1, enumerable:false, configurable : true }); Object.defineProperty(object0, "prop1", { value : 2, enumerable:true, configurable : false }); Object.defineProperty(object0, "prop2", { value : 3 }); object0.prop3 = 4;

Answer: • Object 'object0' contains 4 properties. Property 'prop1' and property 'prop3' are available in the for...in loop. Property 'prop0' and property 'prop3' are available to delete.

Question:* Performance-wise, which is the fastest way of repeating a string in JavaScript?

Answer: • String.prototype.repeat = function(count) { if (count < 1) return ''; var result = '', pattern = this.valueOf(); while (count > 0) { if (count & 1) result += pattern; count >>= 1, pattern += pattern; } return result; };

Question:* Consider the following variable declarations: var a="adam" var b="eve" Which of the following would return the sentence "adam and eve"?

Answer: • a.concat(" and ", b)

Question:* Which of the following code snippets will correctly split "str"?

Answer: • <script> var str = 'something -- something_else'; var substrn = str.split(' -- '); </script>

Question:* Which object can be used to ascertain the protocol of the current URL?

Answer: • location

Question:* Which of the following best describes a "for" loop?

Answer: • "for" loop consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement executed in the loop.

Question:* Which of the following descriptions best describes the code below? <script> var variable1 = { fastFood: "spaghetti", length: 10 }; Object.freeze(variable1); variable1.price = 50; delete variable1.length; </script>

Answer: • Object is frozen, a property named "price" is not added in the variable1 object, a property named "length" is not deleted from this object. At the end of the code, object "variable1" contains 2 properties.

Question:* Which of the following is not a valid HTML event?

Answer: • onblink

Question:* Analyze the following code snippet which uses a Javascript Regular Expression character set. What will be the output of this code? <html> <body> <script type="text/javascript"> var str = "Is this enough?"; var patt1 = new RegExp("[^A-J]"); var result = str.match(patt1); document.write(result); </script> </body> </html

Answer: • s

Question:* Consider the following image definition: <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?

Answer: • document.getElementById('logo').src="companylogo2.gif"

Question:* What is the final value of the variable bar in the following code? var foo = 9; bar = 5; (function() { var foo = 2; bar= 1; }()) bar = bar + foo;

Answer: • 10

Question:* Which of the following are JavaScript unit testing tools?

Answer: • Buster.js, YUI Yeti, Jasmine

Question:* Which of the following can be used for disabling the right click event in Internet Explorer?

Answer: • event.button == 2

Question:* An image tag is defined as follows: <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?

Answer: • document.getElementById('ERImage').src="Image2.jpg"

Question:* Consider the following JavaScript alert: <script type="text/JavaScript"> function message() { alert("Welcome to ExpertRating!!!") } </script> Which of the following will run the function when a user opens the page?

Answer: • body onload="message()"

Question:* Which of the following code snippets will correctly get the length of an object?

Answer: • <script> var newObj = new Object(); newObj["firstname"] = "FirstName"; newObj["lastname"] = "LastName"; newObj["age"] = 21; Object.size = function(obj) { var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) size++; } return size; }; var size = Object.size(newObj); </script>

Question:* Which of the following Array methods in JavaScript runs a function on every item in the Array and collects the result from previous calls, but in reverse?

Answer: • reduceRight()

Question:* 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?

Answer: • <script type="text/javascript"> function Validate() { if(document.forms[0].name.value == "") return false; else return true; } </script>

Question:* Which of the following code snippets changes an image on the page?

Answer: • var img = document.getElementById("imageId"); img.src = "newImage.gif";

Question:* Which of the following results is returned by the JavaScript operator "typeof" for the keyword "null"?

Answer: • object

Question:* What will be the final value of the variable "apt"? var apt=2; apt=apt<<2;

Answer: • 8

Question:* How can a JavaScript object be printed?

Answer: • console.log(obj)

Question:* Which of the following is the correct syntax for using the JavaScript exec() object method?

Answer: • RegExpObject.exec(string)

Question:* Having an array object var arr = new Array(), what is the best way to add a new item to the end of an array?

Answer: • arr.push("New Item")

Question:* Consider the following JavaScript validation function: function ValidateField() { if(document.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?

Answer: • input name=txtId type="text" onblur="return ValidateField()"

Question:* Which of following uses the "with" statement in JavaScript correctly?

Answer: • with (document.getElementById("blah").style) { background = "black"; color = "blue"; border = "1px solid green"; }

Question:* Consider the following JavaScript validation function: <script type="text/JavaScript"> function ValidateField() { if(document.forms[0].txtId.value =="") {return false;} return true; } </script> Which of the following options will call the function as soon as the user leaves the field?

Answer: • input name=txtId type="text" onblur="return ValidateField()"

Question:* Which of the following modifiers must be set if the JavaScript lastIndex object property was used during pattern matching?

Answer: • g

Question:* Consider the following image definition: <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?

Answer: • document.getElementById('logo').src="companylogo2.gif"

Question:* Which of the following will check whether the variable vRast exists or not?

Answer: • if (typeof vRast =="undefined") {}

Question:* What would be the use of the following code? function validate(field) { var valid=''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz''; var ok=''yes''; var temp; for(var i=0;i<field.value.length;i++) { temp='''' + field.value.substring(i,i+1) if(valid.indexOf(temp)==''-1'') { ok=''no''; } } if(ok==''no'') { alert(''error''); field.focus(); } }

Answer: • It will force a user to enter only English alphabet character values.

Question:* An image tag is defined as follows: <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?

Answer: • document.getElementById('ERImage').src="Image2.jpg"

Question:* Which of the following choices will detect if "variableName" declares a function? <script> var variableName= function(){}; </script>

Answer: • typeof variableName;

Question:* Which of the following choices will change the source of the image to "image2.gif" when a user clicks on the image?

Answer: • img id="imageID" src="image1.gif" width="50" height="60" onmousedown="changeimg(image1.gif)" onmouseup="changeimg(image2.gif)"

Question:* How can created cookies be deleted using JavaScript?

Answer: • Overwrite with an expiry date in the past

Question:* What would be the value of 'ind' after execution of the following code? var msg="Welcome to ExpertRating" var ind= msg.substr(3, 3)

Answer: • com

Question:* Are the two statements below interchangeable? object.property object[''property'']

Answer: • Yes

Question:* Which of the following is not a valid method in generator-iterator objects in JavaScript?

Answer: • stop()

Question:* Which of the following code snippets will return all HTTP headers?

Answer: • var req = new XMLHttpRequest(); req.open('GET', document.location, false); req.send(null); var headers = req.getAllResponseHeaders().toLowerCase(); alert(headers);

Question:* Consider the following JavaScript alert: <script type="text/JavaScript"> function message() { alert("Welcome to ExpertRating!!!") } </script> Which of the following will run the function when a user opens the page?

Answer: • body onload="message()"

Question:* Which of the following is the most secure and efficient way of declaring an array?

Answer: • var a = []

Question:* Which of the following Regular Expression pattern flags is not valid?

Answer: • p

Question:* Which of the following built-in functions is used to access form elements using their IDs?

Answer: • getElementById(id)

Question:* Which of the following statements is correct?

Answer: • Undefined object properties can be checked using the following code: if (typeof something === "undefined") alert("something is undefined");

Question:* Which of the following correctly uses a timer with a function named rearrange()?

Answer: • tmr=setTimeout("rearrange ()",1)

Question:* Which of the following can be used to escape the ' character?

Answer: • \

Question:* Which event can be used to validate the value in a field as soon as the user moves out of the field by pressing the tab key?

Answer: • onblur

Question:* When setting cookies with JavaScript, what will happen to the cookies.txt data if the file exceeds the maximum size?

Answer: • The file is truncated to the maximum length.

Question:* Which of the following are not global methods and properties in E4X?

Answer: • setName() and setNamespace()

Question:* 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?

Answer: • <p onmouseover="style.color='blue'" onmouseout="style.color='black'"> The text of the paragraph..</p>

Question:* What is the purpose of while(1) in the following JSON response? while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'],['remindOnRespondedEventsOnly','true'],['hideInvitations_remindOnRespondedEventsOnly','false_true'],['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]

Answer: • It makes it difficult for a third-party to insert the JSON response into an HTML document with a <script> tag.

Question:* Consider the three variables: someText = 'JavaScript1.2'; pattern = /(\w+)(\d)\.(\d)/i; outCome = pattern.exec(someText); What does outCome[0] contain?

Answer: • JavaScript1.2

Question:* Which of the following choices will turn a string into a JavaScript function call (case with objects) of the following code snippet? <script> window.foo = { bar: { baz: function() { alert('Hello!'); } } }; </script>

Answer: • window['foo']['bar']['baz']();

Question:* Which of the following determines whether cookies are enabled in a browser or not?

Answer: • (navigator.cookieEnabled)? true : false

Question:* Which of the following options can be used for adding direct support for XML to JavaScript?

Answer: • E4X

Question:* Which of the following will detect which DOM element has the focus?

Answer: • document.activeElement

Question:* Which of the following will randomly choose an element from an array named myStuff, given that the number of elements changes dynamically?

Answer: • randomElement = myStuff[Math.floor(Math.random() * myStuff.length)];

Question:* How can global variables be declared in JavaScript?

Answer: • Declare the variable between the 'script' tags, and outside a function to make the variable global

Question:* Which of the following objects in JavaScript contains the collection called "plugins"?

Answer: • Navigator

Question:* What will be output of the following code? 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());

Answer: • firststep1second

Question:* Which of the following methods will copy data to the Clipboard?

Answer: • execCommand('Copy')

Question:* Which of the following code snippets trims whitespace from the beginning and end of the given string str?

Answer: • str.replace(/^\s+|\s+$/g, '');

Question:* What is the difference between call() and apply()?

Answer: • The call() function accepts an argument list of a function, while the apply() function accepts a single array of arguments.

Question:* Which of the following code snippets is more efficient, and why? <script language="JavaScript"> for(i=0;i<document.images.length;i++) document.images[i].src="blank.gif"; </script> <script language="JavaScript"> var theimages = document.images; for(i=0;i<theimages.length;i++) theimages[i].src="blank.gif" </script>

Answer: • The second code is more efficient as it employs object caching.

Question:* What is the meaning of obfuscation in JavaScript?

Answer: • Making code unreadable using advanced algorithms.

Question:* Which of the following JavaScript Regular Expression modifiers finds one or more occurrences of a specific character in a string?

Answer: • +

Question:* Which of the following is not a valid JavaScript operator?

Answer: • ^

Question:* Which of the following code snippets returns "[object object]"?

Answer: • <script> var o = new Object(); o.toString(); </script>

Question:* Which of the following can be used to invoke an iframe from a parent page?

Answer: • window.frames

Question:* Select the following function that shuffles an array?

Answer: • function shuffle(array) { var tmp, current, top = array.length; if(top) while(--top) { current = Math.floor(Math.random() * (top + 1)); tmp = array[current]; array[current] = array[top]; array[top] = tmp; } return array; }

Question:* Which of the following code snippets removes objects from an associative array?

Answer: • delete array["propertyName"];

Question:* What is the error in the statement: var charConvert = toCharCode('x');?

Answer: • toCharCode() is a non-existent method.

Question:* What value would JavaScript assign to an uninitialized variable?

Answer: • undefined

Question:* What does the following JavaScript code do? contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { return true; } } return false; }

Answer: • It checks if an array contains 'obj'.

Question:* If an image is placed styled with z-index=-1 and a text paragraph is overlapped with it, which one will be displayed on top?

Answer: • The paragraph.

Question:* Which of the following code snippets gets an image's dimensions (height & width) correctly?

Answer: • var img = document.getElementById('imageid'); var width = img.clientWidth; var height = img.clientHeight;

Question:* Which of the following are correct values of variableC, and why? <script> variableA = [6,8]; variableB =[7,9]; variableC = variableA + variableB; </script>

Answer: • 6, 87 and 9. The + operator is not defined for arrays, and it concatenates strings, so it converts the arrays to strings.

Question:* The following are the samples for getting a selected value in the from a dropdown list: <select id="ddlViewBy"> <option value="1">test1</option> <option value="2" selected="selected">test2</option> <option value="3">test3</option> </select> Which code block is correct?

Answer: • var e = document.getElementById("ddlViewBy"); var strUser = e.options[e.selectedIndex].value;

Question:* var profits=2489.8237 Which of the following code(s) produces the following output? output : 2489.824

Answer: • profits.toFixed(3)

Question:* A form contains two fields named id1 and id2. How can you copy the value of the id2 field to id1?

Answer: • document.forms[0].id1.value=document.forms[0].id2.value

Question:* Which of the following code snippets will toggle a div element's background color? <button id="toggle">Toggle</button> <div id="terd">Change Background Color.</div>

Answer: • <script> var button = document.getElementById('toggle'); button.onclick = function() { terd.style.backgroundColor = terd.style.backgroundColor == 'blue' ? 'red' : 'blue'; }; </script>

Question:* How can the user's previously navigated page be determined using JavaScript?

Answer: • Using the window object

Question:* Which of the following is not a valid method for looping an array?

Answer: • var a= [1,2]; a.loop( function(item) { alert(item); })

Question:* Which of the following correctly sets a class for an element?

Answer: • document.getElementById(elementId).setAttribute("className", "Someclass");

Question:* An HTML form contains 10 checkboxes all named "chkItems". Which JavaScript function can be used for checking all the checkboxes together?

Answer: • function CheckAll() { for (z = 0; z < document.forms[0].chkItems.length; z++) { document.forms[0].chkItems[z].checked=true } }

Question:* Which of the following is a JavaScript comment?

Answer: • // comment

Question:* var foo = 'Global'; function fun() { log( foo ); var foo = 'Local'; log( foo ); } fun(); What the output of the above to log()?

Answer: • undefined Local

Question:* console.log( typeof [1,2] ) will print out:

Answer: • object

Question:* JavaScript is ...

Answer: • object based

Question:* Every object is linked to a _________ object from which it can inherit properties.

Answer: • prototype

Question:* What is the value of x? var x = typeof NaN;

Answer: • "number"

Question:* What will be the value of result? function foo(bar) { return bar ? bar == foo : foo(foo); } var result = foo();

Answer: • true

Question:* What values will the output function be called with, in the following code: var foo; var bar = { name: "baz", email: "fiz@example.com", sendmail: function() { } }; for (foo in bar) { output(foo); }

Answer: • "name", "email", "sendmail"

Question:* Consider: var x = ['a', 'b', 'c']; Which line of code will remove the first element of the array, resulting in x being equal to ['b', 'c']?

Answer: • x.splice(0, 1);

Question:* What is the value of x? var z = [typeof z, typeof y][0]; var x = typeof typeof z;

Answer: • "string"

Question:* var x = {}; var foo = function () { this.hello = "Hi"; return this; }; x.bar = foo; What is the value of the following code: x.bar().bar().hello;

Answer: • "Hi"

Question:* Which of the following assigned values of x will cause (x == x) to return false?

Answer: • All of the answers

Question:* What would this code print out? if (new Boolean(false)) console.log("True"); else console.log("False");

Answer: • True

Question:* Which two values are logged by the following code? var x = 5; (function () { console.log(x); var x = 10; console.log(x); }());

Answer: • undefined 10

Question:* Infinity * null will return :

Answer: • NaN

Question:* Assuming alert displays a string of its argument: var a = 10; function example(){ alert(a); var a = 5; } example(); What will be shown if the preceding code is executed?

Answer: • undefined

Question:* What is the result? "" ? "a" : "b"

Answer: • "b"

Question:* What is the value of x? var x = typeof null;

Answer: • "object"

Question:* The length property of an Array object is always:

Answer: • equal to the highest index of that object + 1

Question:* var data = [1, 2, 3, 4, 5, 6]; data.shift(); What does data look like?

Answer: • [2, 3, 4, 5, 6]

Question:* Which fact is true about the keyword "default"?

Answer: • It catches any case clauses not caught by case statements within a switch statement

Question:* Which String prototype method is capable of removing a character from a string?

Answer: • replace()

Question:* What is the value of x after the following statement? var x = 1 == '1';

Answer: • true

Question:* What is the result of the following code snippet? var small = 'BIG'; var BIG = 'small'; console.log(/small/.test(BIG)); console.log(/BIG/.test(small));

Answer: • true true

Question:* What is the result of the following statement: typeof "x";

Answer: • "string"

Question:* Is there a value type for individual string characters?

Answer: • No, there is only type "string" for characters.

Question:* Given a variable named stringVar with a string value, what does the following do? stringVar.toUpperCase();

Answer: • Return a copy of stringVar with all letters in uppercase

Question:* Which is an example of (only) an object literal in Javascript?

Answer: • var obj = { prop1: 'property 1', prop2: 'property 2' };

Question:* The loop isn't working. What's the problem? <code> var foos = ['a', 'b', 'c' , 'd', 'e']; var bars = ['x', 'y', 'z']; for (var i = 0; i < foos.length; i++) { var foo = foos[i]; for (var i = 0; i < bars.length; i++) { var bar = bars[i]; /* some code using `bar` */ } } </code>

Answer: • The inner loop resets the outer for-loop, leaving it a fixed position each time, causing an infinite loop (hint: no block scope).

Question:* Functions in javascript are always ..

Answer: • objects

Question:* How do you read the first character in a string?

Answer: • data.charAt(0);

Question:* Which of the following have special meanings within the language syntax?

Answer: • Reserved words

Question:* What is the result? 0 == ""

Answer: • true

Question:* Primitive types are passed by :

Answer: • Value

Question:* To what type are values converted internally when evaluating a conditional statement?

Answer: • boolean

Question:* var x = "foo"; x = !!x; What is the value of x?

Answer: • true

Question:* What will calling the below test function log to console? function test(){ console.log(a); var a = 'hello'; console.log(a); }

Answer: • undefined, "hello"

Question:* How do you assign an anonymous function to a variable?

Answer: • var anon = function() { };

Question:* What is the value of x? var x = 2 + "2";

Answer: • "22"

Question:* Which of these is not a JavaScript statement?

Answer: • None, these are all valid statements.

Question:* Which of the following variable types does not exist in JavaScript?

Answer: • double

Question:* Which of the following is a way to add a new value to the end of an array?

Answer: • arr[arr.length] = value;

Question:* What is the RegExp object constructor used for?

Answer: • Match text against regular expressions

Question:* What does "2" + 3 + 4 evaluate to?

Answer: • '234'

Question:* When an array index goes out of bounds, what is returned?

Answer: • undefined

Question:* Which is the correct syntax to write array literals in JavaScript?

Answer: • var x = ["blank","blank","blank"];

Question:* The "exploit" property:

Answer: • Does not exist in JavaScript

Question:* split() is a method of which constructors' prototype?

Answer: • String.prototype

Question:* Which of the following orders can be performed with the Array prototype "sort()" callback?

Answer: • All of these

Question:* var a = {1:'one',2:'two',3:'three'}; var b = Object.keys(a); What's the value of b?

Answer: • An array with all of the distinct keys from the obj a

Question:* How do you write a conditional statement that will *only* execute the contained code if variable x has a value 5 of type *number*?

Answer: • if (x === 5) { ... }

Question:* What is the value of c? var a = function(){ this.b = 1; } var b = function(){ var b = new a().b; return 5 + b; } var c = b();

Answer: • 6

Question:* How do you find the number with the highest value of x and y?

Answer: • Math.max(x, y)

Question:* What is the value of x? var a = false; var x = a ? “A” : “B”;

Answer: • "B"

Question:* Which of the following operators can assign a value to a variable?

Answer: • All of these

Question:* What is the value of x? var x = '1'+2+3;

Answer: • "123"

Question:* Which of the following is the equivalent of the following. if (a) { x = b; } else { x = c; }

Answer: • x = a ? b : c;

Question:* What is the value of x? var obj = {}; obj["function"] = 123; x = obj.function;

Answer: • 123

Question:* null === undefined

Answer: • false

Question:* Which event fires whenever a control loses focus?

Answer: • onblur

Question:* Which of these is not a logical operator?

Answer: • &

Question:* Which of these operators compares two variables by value AND type?

Answer: • ===

Question:* Which message does the following log to the console? bar(); function bar() { console.log('bar'); }

Answer: • "bar"

Question:* Which of the following invokes a user-defined object constructor function?

Answer: • var x = new myConstructor();

Question:* The function call Math.ceil(3.5) returns:

Answer: • 4

Question:* In JavaScript, to call a function directly, you use:

Answer: • function_expression ( arguments_if_any )

Question:* How do you declare a function?

Answer: • function doSomething() {}

Question:* How would one declare a string variable?

Answer: • Any of these

Question:* What is the value of a : var a = 3; var b = 2; var c = a; var a=b=c=1;

Answer: • 1

Question:* Properties of objects may be accessed using...

Answer: • the dot notation in JavaScript.

Question:* What is the value of the array myArr after execution of the following code: var myArr = [1,2,3,4,5]; myArr.shift();

Answer: • [2,3,4,5]

Question:* What does isNaN() do?

Answer: • Only returns true if the argument is not a number

Question:* What keyword is used to define the alternative path to take in a conditional statement?

Answer: • else

Question:* What is the difference between a while loop and a do...while loop?

Answer: • The code inside a do...while loop will always be executed at least once, even if the condition is false.

Question:* What is the difference between == and === ?

Answer: • The == operator converts both operands to the same type, whereas === returns false for different types.

Question:* Which of these could be a correct way to create an instance of Person?

Answer: • var john = new Person('John', 'Doe', 50, 'blue');

Question:* (function( ) { var x = foo( ); function foo( ){ return "foobar" }; return x; })( ); What does this function return?

Answer: • "foobar"

Question:* What operator is used for string concatenation?

Answer: • +

Question:* Which is the correct way to write a JavaScript array?

Answer: • var names = ["Tim","Kim","Jim"];

Question:* USERNAME and userName

Answer: • Represent the name of different variables

Question:* Which of these descriptors applies to JavaScript?

Answer: • Loosely typed, values of any type can be assigned to any variable.

Question:* Which of the following is an Error object constructor?

Answer: • All of these

Question:* How do you check what the type of a value in variable x is?

Answer: • typeof(x);

Question:* Which of the following is a JavaScript comment?

Answer: • // comment

Question:* What is the value of the following expression: 8 % 3

Answer: • 2

Question:* var a = '011'; parseInt(a); will return:

Answer: • 11

Question:* var a = '011'; parseInt(a); will return:

Answer: • 11

Question:* How does a "while" loop start?

Answer: • while (i<=10)

Question:* The `else` statement is ___

Answer: • used together with the `if` statement to specify the code that should execute when the `if` condition is false.

Question:* Given the following code, what does myFunc() return? var foo = 'foo'; var bar = 'bar'; function myFunc() { return foo + bar; }

Answer: • "foobar"

Question:* String literals are written using:

Answer: • Either double quotes or single quotes: "example" and 'example'

Question:* How is an object property referenced?

Answer: • myObj.foo

Question:* Which symbol is not used in logical operations?

Answer: • %

Question:* Which of these will throw a SyntaxError?

Answer: • if (x ==== 1) { }

Question:* JavaScript supports dynamic typing, you can assign different types of values to the same variable.

Answer: • true

Question:* How do you round the number 7.25, to the nearest whole number?

Answer: • Math.round(7.25)

Question:* How to return the first value of this array? var myArr = [1, 2, 3, 4, 5]; var myVal = ...

Answer: • myArr[0];

Question:* How do you define a function called "fName"?

Answer: • function fName() { }

Question:* Which of the following is not a reserved word?

Answer: • program

Question:* Which of the following asserts that the variables `A`, `B`, and `C` have unequal values?

Answer: • A !== B && B !== C && A !== C

Question:* What does the "break" statement do?

Answer: • Aborts the current loop or switch statement.

Question:* Which of the following is a valid function definition?

Answer: • function myFunc(arg1,arg2) { }

Question:* Which of the following declares a variable with a value of string type?

Answer: • var myVar = "This is a string";

Question:* Are variable identifiers case-sensitive?

Answer: • Yes

Question:* What is the correct JavaScript syntax to insert a comment that can span multiple lines?

Answer: • /* This comment has more than one line */

Question:* The "if" statement is used to:

Answer: • Deal with logic that should execute only when a condition is true

Question:* Which keyboard character represents the assignment operator?

Answer: • =

Question:* String concatenation...

Answer: • Is the combination of two or more text Strings

Question:* Properties of a RegExp object include:

Answer: • All of these

Question:* You use the Math.pow() method to:

Answer: • Return a number raised to the power of a second number

Question:* What is the value of ("dog".length)?

Answer: • 3

Question:* Which is NOT a way to create a loop in javascript?

Answer: • repeat (...) { }

Question:* What is the value of x? var a = "A"; var x = a.concat("B");

Answer: • "AB"

Question:* Which statement loops through an array?

Answer: • for (var i=0; i < myArray.length; i++)

Question:* The var statement is used to:

Answer: • Create a new local variable

Question:* function foo(){ var tmp = 'one_two_three_four_five'; return tmp.replace(/_/g, '+'); } What does foo() return?

Answer: • one+two+three+four+five

Question:* A for loop is written as such: "for (first property; second property; third property) {...}" What does the third property represent?

Answer: • An action to take at the end of the current loop cycle

Question:* Where do you use the "break" statement?

Answer: • To terminate a switch statement, loop, or labeled block.

Question:* In an array object, what is the key of the first value?

Answer: • 0

Question:* Which of the following primitive values exist in JavaScript?

Answer: • All of these

Question:* What keyword is used to begin a conditional statement?

Answer: • if

Question:* What character ends a javascript statement?

Answer: • A semicolon ";".

Question:* Which has the correct syntax of a ternary operation?

Answer: • var x = y === true ? "true" : "false";

Question:* JavaScript is an implementation of the ______ language standard.

Answer: • ECMAScript

Question:* If a function doesn't explicitly use the "return" operator, what will the return value be when the function is invoked?

Answer: • undefined

Question:* What is result of the following expression: var a = "test"; console.log(!!a);

Answer: • true

Question:* What is the name of the String prototype that appends the given string to the base string and returns the new string?

Answer: • "x".concat("foo")

Question:* Which of these is a correct method to create a new array?

Answer: • var myArray = [];

Question:* var data = ["A", "B", "C", "D"]; data.unshift("X"); data.push("Y"); What does data look like?

Answer: • ["X", "A", "B", "C", "D", "Y"]

Question:* What character combination is used to create a single line comment?

Answer: • //

Question:* Which of the following is the syntax for an object literal (with no properties)?

Answer: • {};

Question:* How can you concatenate multiple strings?

Answer: • Both of these

Question:* How do you assign object properties?

Answer: • obj["age"] = 25 OR obj.age = 25

Question:* Consider this code: var x = ['Hello']; What value will 'x[1]' return?

Answer: • undefined

Question:* The _______ operator returns a string that identifies the type of its operand.

Answer: • typeof

Question:* When writing an object literal, what is used to separate the properties from each other?

Answer: • a comma ","

Question:* What are curly braces ("{" and "}") used for?

Answer: • Block declarations and object literals

Question:* What is the value of x? function foo(y) { var z = 10; z = 7; }; var x = foo("bar");

Answer: • undefined

Question:* How do you create an object in JavaScript?

Answer: • All of these work.

Question:* Which of the following is not a method in the "JSON" object according to the ECMAScript specification?

Answer: • JSON.fromString

Question:* How can you get the number of characters in a string ?

Answer: • "1234567".length

Question:* What is the result of the following statement: 0 == "";

Answer: • true

Question:* What does null, undefined, "string", 20, true and false have in common?

Answer: • they are primitive values

Question:* Given the following code, what is the value of x? var x = ['foo', 'bar']; x.length = 1;

Answer: • ["foo"]

Question:* Math.random() returns..

Answer: • a random number between 0 and 1

Question:* What is the result of the following expression? ({"foo": true}).foo;

Answer: • true

Question:* In the loop, "for (first clause; second clause; third clause) { statements; }" What does the second clause represent?

Answer: • A condition to check at the beginning of each loop cycle

Question:* What is the value of `x` after the following? var x = "hello"; (function() { x = "goodbye"; }());

Answer: • "goodbye"

Question:* Which is not a primitive data type in JavaScript?

Answer: • character

Question:* What will invoking `foo` return? function foo() { var x = 10; x = 7; };

Answer: • undefined

Question:* What is the value of x? var str = "what is this"; var x = str.search("is");

Answer: • 5

Question:* The "this" keyword refers to ...

Answer: • current execution context (could be any value).

Question:* '&' Operator is _____

Answer: • a bitwise operator

Question:* What character combination is used to alter the order of operations by grouping expressions?

Answer: • ( )

Question:* var x = Math.ceil(10.126); What is the value of x?

Answer: • 11

Question:* What is the type of `null`, according to the `typeof` operator?

Answer: • "object"

Question:* function b(x, y, a) { arguments[2] = 10; alert(a); } b(1, 2, 3); What is alerted?

Answer: • 10

Question:* What is the difference between using call() and apply() to invoke a function with multiple arguments?

Answer: • apply() is identical to call(), except apply() requires an array as the second parameter

Question:* var y = 3, x = y++; What is the value of x?

Answer: • 3

Question:* Which of the following types does NOT exist in javascript?

Answer: • integer

Question:* What is the end value of myAddedVar with the following code: var myVar = '5'; var myAddedVar = myVar + 10;

Answer: • '510'

Question:* What does this line do? variable++;

Answer: • Increments the value of "variable" but returns the previous value

Question:* var a = new Boolean(false); What is (typeof a)?

Answer: • 'object'

Question:* What is the value of x? var x = typeof new String("abc");

Answer: • object

Question:* Which of these will invoke a function?

Answer: • function.apply(...)

Question:* Does NaN equal itself?

Answer: • No, NaN does not equal itself (== comparison would return false).

Question:* What will happen in the console after executing this code? if ("foo") { console.log("foo" === false); console.log("foo" === true); }

Answer: • false false

Question:* What is the value of the function log? var _ = '_'; log(parseInt(_));

Answer: • NaN

Question:* Which of these is not a built-in object constructor?

Answer: • Time

Question:* Given the following code: var myVar = '5'; var myAddedVar = myVar + 10; What is the value of (myAddedVar.constructor === Number)?

Answer: • false

Question:* How would you iterate over the following object? var my_data = {a: 'Ape', b: 'Banana', c: 'Citronella'};

Answer: • for (var key in my_data) {}

Question:* What is the value of x after the following code is executed? var x = 0; x = x++;

Answer: • 0

Question:* You use the Math.tan( ) method to:

Answer: • Return the tangent of an angle (in radians)

Question:* A javascript variable prefixed with a $ is:

Answer: • valid javascript syntax as any other character

Question:* Math.min() < Math.max(); ...will return:

Answer: • false

Question:* Math.min() < Math.max(); will return

Answer: • false

Question:* What does the following expression return? 1 + 5 + " bottles of milk";

Answer: • "6 bottles of milk"

Question:* Which Object method takes a `propertyName` parameter and returns `true` if the object contains an uninherited property with that key?

Answer: • obj.hasOwnProperty('propertyName');

Question:* var foo = "Global"; function fun() { log( foo ); var foo = "Local"; log( foo ); } fun(); What the output of the above to log()?

Answer: • undefined Local

Question:* Which of the following is NOT a valid way to write a loop that will iterate over the values in the array in variable "myArray"?

Answer: • None of these, they are all valid

Question:* What is the value of c? var A = function () { this.b = 1; } A.prototype.b = 2; var c = new A().b;

Answer: • 1

Question:* What will be the value of x? function A() { this.do = function () { return 'foo'; }; } A.prototype = { do: function () { return 'bar'; } }; var x = new A().do();

Answer: • "foo"

Question:* What is the right way to combine two arrays into a new array? var a = ["a", "b", "c"]; var b = ["d", "e", "f"];

Answer: • var c = a.concat(b);

Question:* What is the output? var one; var two = null; console.log(one == two, one === two);

Answer: • true false

Question:* Which are the different ways to affect the "this" reference in a function?

Answer: • Invoking a function with the "new" keyword, invoking through the .call() method, invoking through the .apply() method.

Question:* What is the difference between the two declaration methods below? var functionOne = function() { /* some code */ } function functionTwo() { /* some code */ }

Answer: • functionOne is defined in-place (until that line, functionOne is undefined), whereas functionTwo is hoisted to the top of the scope and is available as a function throughout the scope.

Question:* How does JavaScript interpret numeric constants outside of strict mode?

Answer: • As octal if they are preceded by a zero, and as hexadecimal if they are preceded by a zero and "x"

Question:* When reserved words are used as keys in object literals they must be ______?

Answer: • quoted

Question:* Mathematical expression, "10" - (12+5).toString(), evaluates to what?

Answer: • -7

Question:* What does the following return? Number(null);

Answer: • 0

Question:* What is the value of x.length after running this code? x = ["foo"]; x.quux = "Hello"; x[1] = "bar";

Answer: • 2

Question:* What is the value of x after the code below is executed? var x = ""; function foo() { x += "foo "; } var bar = function() { x += "bar "; }; foo(); var quux = bar = foo; quux();

Answer: • "foo foo "

Question:* What will be in console after executing this code: console.log(1 + '1' - 1);

Answer: • 10

Question:* The expression (typeof NaN === "number") evaluates to:

Answer: • true

Question:* What will: typeof typeof(null) return?

Answer: • string

Question:* What will the expression a === b return after the following? var a = { "foo": "bar" }; var b = { "foo": "bar" };

Answer: • false

Question:* How can you remove an element from an array and replace it with a new one ?

Answer: • array.splice(...)

Question:* What will we see in the console after the following code run: var a = 'Bolt'; function f() { if (!a) { var a = 'Nut'; } console.log(a); } f(); console.log(a);

Answer: • 'Nut' then 'Bolt'

Question:* true + true will return :

Answer: • 2

Question:* What is the result of: function foo() { output( "biz " + bar() ); } bar(); var bar = function() { return "baz"; }

Answer: • TypeError: Undefined is not a function

Question:* What is the value of x? var x = 10/0;

Answer: • Infinity

Question:* What will be the result of this expression: void 0

Answer: • undefined

Question:* What does Math.random() do?

Answer: • Returns a random number from and including 0 to less than 1.

Question:* In the following code: function test() { var foo = bar = 5; } test();

Answer: • Variable foo is a local variable. But bar is a global variable.

Question:* What are the values of x and y after the invocation of `foo` in following? var x = "I am global x"; var y = "I am global y"; function foo() { var y = x = "Hello from foo"; } foo();

Answer: • x = "Hello from foo"; y = "I am global y";

Question:* var q = null; q++; What is q?

Answer: • 1

Question:* Given the following code, what will myFunction(123, false, "test") return? function myFunction(param) { return arguments[1] || param; }

Answer: • 123

Question:* After the following code: var a = function(){ this.b = 1; this.deleteMe = function(){ delete this; } }; var c = new a(); c.deleteMe(); What is the value of (String(c))?

Answer: • [object Object]

Question:* Which operator has highest precedence?

Answer: • *

Question:* function Question() { this.answered = false; } Question.prototype.rightAnswer = 5; console.log( new Question().rightAnswer, Question.rightAnswer ); What gets printed to the console?

Answer: • 5 undefined

Question:* What will the console log when running this code? Function.prototype.a = 1; var a = new Function(); a.prototype.a = 2; var c = new a(); console.log(a.a , c.a);

Answer: • 1 2

Question:* Evaluate: undefined + 2

Answer: • NaN

Question:* Evaluate: new Boolean(new Boolean(false)).valueOf()

Answer: • true

Question:* An (inner) function enjoys access to the parameters and variables of all the functions it is nested in. This is called:

Answer: • Lexical scoping

Question:* Object.keys(x)

Answer: • returns the enumerable properties of x as an array of strings.

Question:* "bar".split().length returns:

Answer: • 1

Question:* Math.Pi returns the mathematical constant of Pi. What standard JavaScript method would truncate Math.Pi to 3.14 ?

Answer: • Math.Pi.toFixed(2)

Question:* function foo(){ var tmp = 'one_two_three_four_five'; return tmp.replace('_', '+'); } What does foo() return?

Answer: • one+two_three_four_five

Question:* var x = { foo: "A" }; x.constructor.prototype.foo = "B"; var y = {}; console.log(x.foo); console.log(y.foo); Which two values will be logged?

Answer: • "A" "B"

Question:* Evaluate the following expression: ~-(2 + "2")

Answer: • 21

Question:* Which of these will create a copy of an array such that changes to the old array will not be reflected in the new array?

Answer: • var newArray = oldArray.slice(0);

Question:* What is the output of the following? var x = 1; console.log(x++ + ++x + x);

Answer: • 7

Question:* console.log( ~[]+~![]+~!![] ); Console output is:

Answer: • -4

Question:* What value is passed to function "foo" as first argument? foo( +"5" );

Answer: • 5

Question:* What will be printed to the console as a result of this code? var printName = function() { console.log('Matt'); printName = function() { console.log('James'); }; }; var copy = printName; printName(); copy();

Answer: • Matt Matt

Question:* What is the result of: console.log((!+[]+[]+![]).length);

Answer: • 9

Question:* What is the value of x after the following code is run? var obj = { 0: 'who', 1: 'what', 2: 'idontknow'}; var x = 1 in obj;

Answer: • true

Question:* Which of the following String prototype method takes a regular expression?

Answer: • search()

Question:* What is the value of mike after this code is run? function Person(name, age) { this.name = name; this.age = parseInt(age, 10); } var mike = Person('Mike', '25');

Answer: • undefined

Question:* var x = ([]+!![])[+!+[]]; What is the value of x?

Answer: • 'r'

Question:* Which of the following expressions evaluates to false?

Answer: • new Boolean('false') == false

Question:* Math.log(x) returns:

Answer: • Logarithm base e (Euler's number) of x.

Question:* Which of the following is not a reserved word in the language?

Answer: • and

Question:* What is the result of the below expression? Assume output is a function that outputs a line of text. output(typeof (function() {output("Hello World!")})());

Answer: • Hello World! undefined

Question:* What is the value of "x" after the following code runs? var x; x++;

Answer: • NaN

Question:* function foo() { this = "foo"; } var a = foo(); What will the preceding code produce?

Answer: • ReferenceError: Invalid left-hand side in assignment

Question:* What will this code produce: +new Date()

Answer: • Unix timestamp in milliseconds (UTC timezone)

Question:* What is the value of c? var a = function(){ this.b = 1; } var b = function(){ this.b = new a().b; return 5; } var c = b() + new b();

Answer: • Error thrown when running the code

Question:* Which of the following Array prototype method actually modifies the array it's been called on?

Answer: • splice()

Question:* What does the following return? Math.max();

Answer: • -Infinity

Question:* Which are the different value types in JavaScript?

Answer: • boolean, number, string, function, object and undefined

Question:* String values have a "length" property. Why is this property not included in a for-in loop over a string object? var prop, str; str = 'example'; /* str.length === 7 */ for ( prop in str) {}

Answer: • Because the "length" property has internal [[Enumerable]] set to false.

Question:* What will the following code, when evaluated, do? var void = function () {};

Answer: • Throw a SyntaxError

Question:* Object("s") instanceof String === "s" instanceof String

Answer: • false

Question:* What's the correct syntax for creating a Date object for January 10, 1998?

Answer: • new Date(1998, 0, 10);

Question:* Evaluate: ![]+[]

Answer: • 'false'

Question:* What is the value of x? var a = "abc"; var x = a instanceof String;

Answer: • false

Question:* Which of the following is NOT a reserved word in JavaScript?

Answer: • array



No comments:

HTML5 Upwork (oDesk) TEST ANSWERS 2022

HTML5 Upwork (oDesk) TEST ANSWERS 2022 Question: Which of the following is the best method to detect HTML5 Canvas support in web br...

Disqus for upwork test answers