Tuesday, 30 December 2014

JavaScript Test Answers

JavaScript is …
object based

How can you get the square root of 2 in JavaScript?
Math.SQRT2

What is the result? “” ? “a” : “b”
“b”

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’]?
x.splice(0, 1);
var foo = ‘Global'; function fun() { log( foo ); var foo = ‘Local'; log( foo ); } fun(); What the output of the above to log()?
undefined Local
var y = 3, x = y++; What is the value of x?
3
You use the Math.tan( ) method to:
Return the tangent of an angle (in radians)
What is the value of x after the following statement? var x = 1 == ‘1’;
true
Which fact is true about the keyword “default”?
It catches any case clauses not caught by case statements within a switch statement
What is the end value of myAddedVar with the following code: var myVar = ‘5’; var myAddedVar = myVar + 10;
‘510’
What is the value of `x` after the following? var x = “hello”; (function() { x = “goodbye”; }());
“goodbye”
var x = Math.ceil(10.126); What is the value of x?
11
If a function doesn’t explicitly use the “return” operator, what will the return value be when the function is invoked?
undefined
In the loop, “for (first clause; second clause; third clause) { statements; }” What does the second clause represent?
A condition to check at the beginning of each loop cycle
split() is a method of which constructors’ prototype?
String.prototype
How do you create an object in JavaScript?
All of these work.
Which has the correct syntax of a ternary operation?
var x = y === true ? “true” : “false”;
Which of the following is the syntax for an object literal (with no properties)?
{};
The _______ operator returns a string that identifies the type of its operand.
typeof
Which of the following orders can be performed with the Array prototype “sort()” callback?
All of these
Which of these is not a logical operator?
&
avaScript is an implementation of the ______ language standard.
ECMAScript
Which of the following is the equivalent of the following. if (a) { x = b; } else { x = c; }
x = a ? b : c;
Which of the following variable types does not exist in JavaScript?
double
What is the RegExp object constructor used for?
Match text against regular expressions
In JavaScript, to call a function directly, you use:
function_expression ( arguments_if_any )
What is the name of the String prototype that appends the given string to the base string and returns the new string?
“x”.concat(“foo”)
What is the value of x? var a = false; var x = a ? “A” : “B”;
“B”
How do you assign an anonymous function to a variable?
var anon = function() { };
When an array index goes out of bounds, what is returned?
undefined
var a = {1:’one’,2:’two’,3:’three’}; var b = Object.keys(a); What’s the value of b?
An array with all of the distinct keys from the obj a
What is the value of x? var x = 2 + “2”;
“22”
How do you assign object properties?
obj[“age”] = 25 OR obj.age = 25
Which message does the following log to the console? bar(); function bar() { console.log(‘bar’); }
“bar”
Which is the correct syntax to write array literals in JavaScript?
var x = [“blank”,”blank”,”blank”];
Which of the following operators can assign a value to a variable?
All of these
Which event fires whenever a control loses focus?
onblur
Which of these operators compares two variables by value AND type?
===
What is the difference between == and === ?
The == operator converts both operands to the same type, whereas === returns false for different types.
Which of the following is a JavaScript comment?
// comment
A for loop is written as such: “for (first property; second property; third property) {…}” What does the third property represent?
An action to take at the end of the current loop cycle
function foo(){ var tmp = ‘one_two_three_four_five'; return tmp.replace(/_/g, ‘+’); } What does foo() return?
one+two+three+four+five
The function call Math.ceil(3.5) returns:
4
How is an object property referenced?
myObj.foo
What is the value of a : var a = 3; var b = 2; var c = a; var a=b=c=1;
1
What is the value of x? var obj = {}; obj[“function”] = 123; x = obj.function;
123
What keyword is used to define the alternative path to take in a conditional statement?
else
Which of the following invokes a user-defined object constructor function?
var x = new myConstructor()
What is the value of x? var x = ‘1’+2+3;
“123”
Which of the following is an Error object constructor?
All of these
Which of these is not a JavaScript statement?
None, these are all valid statements.
null === undefined
false
(function( ) { var x = foo( ); function foo( ){ return “foobar” }; return x; })( ); What does this function return?
“foobar”
How would one declare a string variable?
Any of these
USERNAME and userName
Represent the name of different variables
What is the value of the array myArr after execution of the following code: var myArr = [1,2,3,4,5]; myArr.shift();
[2,3,4,5]
Which of these could be a correct way to create an instance of Person?
var john = new Person(‘John’, ‘Doe’, 50, ‘blue’);
What operator is used for string concatenation?
+
Which is the correct way to write a JavaScript array?
var names = [“Tim”,”Kim”,”Jim”];
What is the difference between a while loop and a do…while loop?
The code inside a do…while loop will always be executed at least once, even if the condition is false.
What does isNaN() do?
Only returns true if the argument is not a number
The `else` statement is ___
used together with the `if` statement to specify the code that should execute when the `if` condition is false.
Which of these descriptors applies to JavaScript?
Loosely typed, values of any type can be assigned to any variable.
Which of the following asserts that the variables `A`, `B`, and `C` have unequal values?
A !== B && B !== C && A !== C
What is the value of the following expression: 8 % 3
2
Properties of objects may be accessed using…
the dot notation in JavaScript.
var a = ‘011’; parseInt(a); will return:
11
How does a “while” loop start?
while (i<=10)
Given the following code, what does myFunc() return? var foo = ‘foo'; var bar = ‘bar'; function myFunc() { return foo + bar; }
“foobar”
How to return the first value of this array? var myArr = [1, 2, 3, 4, 5]; var myVal = …
myArr[0];
Which symbol is not used in logical operations?
%
How do you round the number 7.25, to the nearest whole number?
Math.round(7.25)
The “if” statement is used to:
Deal with logic that should execute only when a condition is true
String literals are written using:
Either double quotes or single quotes: “example” and ‘example’
You use the Math.pow() method to:
Return a number raised to the power of a second numbe
Properties of a RegExp object include:
All of these
Which of the following declares a variable with a value of string type?
var myVar = “This is a string”;
Which of these will throw a SyntaxError?
if (x ==== 1) { }
How do you define a function called “fName”?
function fName() { }
Which of the following is not a reserved word?
program
How do you check what the type of a value in variable x is?
typeof(x);
What does the “break” statement do?
Aborts the current loop or switch statement.
What is the value of (“dog”.length)?
3
Are variable identifiers case-sensitive?
Yes
What is the correct JavaScript syntax to insert a comment that can span multiple lines?
/* This comment has more than one line */
Which keyboard character represents the assignment operator?
=
Which of the following is a valid function definition?
function myFunc(arg1,arg2) { }
JavaScript supports dynamic typing, you can assign different types of values to the same variable.
true
String concatenation…
Is the combination of two or more text Strings
he var statement is used to:
Create a new local variable
Which is NOT a way to create a loop in javascript?
repeat (…) { }
What is the value of x? var a = “A”; var x = a.concat(“B”);
“AB”
Where do you use the “break” statement?
To terminate a switch statement, loop, or labeled block.
In an array object, what is the key of the first value?
0
Which statement loops through an array?
for (var i=0; i < myArray.length; i++)
What character ends a javascript statement?
A semicolon “;”.
Which of the following primitive values exist in JavaScript?
All of these
What keyword is used to begin a conditional statement?
if
What character combination is used to create a single line comment?
//
Java script start with……
<script>
The “exploit” property:
Does not exist in JavaScript
How do you write a conditional statement that will *only* execute the contained code if variable x has a value 5 of type *number*?
if (x === 5) { … }
Functions in javascript are always ..
objects
How do you declare a function?
function doSomething() {}
How do you find the number with the highest value of x and y?
Math.max(x, y)
What are curly braces (“{” and “}”) used for?
Block declarations and object literals
Which of the following have special meanings within the language syntax?
Reserved words
To what type are values converted internally when evaluating a conditional statement?
boolean
Primitive types are passed by :
Value
What will calling the below test function log to console? function test(){ console.log(a); var a = ‘hello'; console.log(a); }
undefined, “hello”
How can you concatenate multiple strings?
Both of these
var data = [“A”, “B”, “C”, “D”]; data.unshift(“X”); data.push(“Y”); What does data look like?
[“X”, “A”, “B”, “C”, “D”, “Y”]
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();
6
What is the result? 0 == “”
true
Which is an example of (only) an object literal in Javascript?
var obj = { prop1: ‘property 1′, prop2: ‘property 2′ };
Which of the following is a way to add a new value to the end of an array?
arr[arr.length] = value;
Which of these is a correct method to create a new array?
var myArray = [];
Which of the following is not a method in the “JSON” object according to the ECMAScript specification?
JSON.fromString
var x = “foo”; x = !!x; What is the value of x?
true
What is result of the following expression: var a = “test”; console.log(!!a);
true
What does “2” + 3 + 4 evaluate to?
‘234’
When writing an object literal, what is used to separate the properties from each other?
a comma “,”
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>
The inner loop resets the outer for-loop, leaving it a fixed position each time, causing an infinite loop (hint: no block scope).
What is the result of the following statement: 0 == “”;
true
What is the value of x? function foo(y) { var z = 10; z = 7; }; var x = foo(“bar”);
undefined
What is the result of the following expression? ({“foo”: true}).foo;
true
Which is not a primitive data type in JavaScript?
character
Consider this code: var x = [‘Hello’]; What value will ‘x[1]’ return?
undefined
How can you get the number of characters in a string ?
“1234567”.length
What will invoking `foo` return? function foo() { var x = 10; x = 7; };
undefined
Every object is linked to a _________ object from which it can inherit properties.
prototype
Given a variable named stringVar with a string value, what does the following do? stringVar.toUpperCase();
return a copy of stringVar with all letters in uppercase
Math.random() returns..
a random number between 0 and 1
Given the following code, what is the value of x? var x = [‘foo’, ‘bar’]; x.length = 1;
[“foo”]
How do you read the first character in a string?
data.charAt(0);
Is there a value type for individual string characters?
No, there is only type “string” for characters.
The “this” keyword refers to …
current execution context (could be any value).
What character combination is used to alter the order of operations by grouping expressions?
( )
The length property of an Array object is always:
equal to the highest index of that object + 1
&’ Operator is _____
a bitwise operator
What is the result of the following statement: typeof “x”;
“string”
What does null, undefined, “string”, 20, true and false have in common?
they are primitive values
nfinity * null will return :
NaN
What is the value of x? var x = typeof null;
“object”
What is the type of `null`, according to the `typeof` operator?
“object”
What is the value of x? var x = typeof new String(“abc”);
object
Does NaN equal itself?
No, NaN does not equal itself (== comparison would return false).
What does the following expression return? 1 + 5 + ” bottles of milk”;
“6 bottles of milk”
What does this line do? variable++;
Increments the value of “variable” but returns the previous value
Which of the following types does NOT exist in javascript?
integer
What is the value of x after the following code is executed? var x = 0; x = x++;
0
What’s the difference between the “var” keyword and the new “let” keyword which appears in ECMAScript 6 ?
The “let” declares a local value to a block scope whereas the “var” defines a variable more globally
var a = new Boolean(false); What is (typeof a)?
‘object’
var data = [1, 2, 3, 4, 5, 6]; data.shift(); What does data look like?
[2, 3, 4, 5, 6]
Which of these is not a built-in object constructo
Time
How would you iterate over the following object? var my_data = {a: ‘Ape’, b: ‘Banana’, c: ‘Citronella’};
for (var key in my_data) {}
function b(x, y, a) { arguments[2] = 10; alert(a); } b(1, 2, 3); What is alerted?
10
Which are the different ways to affect the “this” reference in a function?
Invoking a function with the “new” keyword, invoking through the .call() method, invoking through the .apply() method.
What is the difference between using call() and apply() to invoke a function with multiple arguments?
apply() is identical to call(), except apply() requires an array as the second parameter

What would this code print out? if (new Boolean(false)) console.log(“True”); else console.log(“False”);
True

What will happen in the console after executing this code? if (“foo”) { console.log(“foo” === false); console.log(“foo” === true); }
false false
A javascript variable prefixed with a $ is:
valid javascript syntax as any other character
Which Object method takes a `propertyName` parameter and returns `true` if the object contains an uninherited property with that key?
obj.hasOwnProperty(‘propertyName’);
Which String prototype method is capable of removing a character from a string?
replace()
var obj1 = {}; var obj2 = {}; What is the value of (obj1 === obj2)
false
Which of these will invoke a function?
function.apply(…)
What is the right way to combine two arrays into a new array? var a = [“a”, “b”, “c”]; var b = [“d”, “e”, “f”];
var c = a.concat(b);
What will the expression a === b return after the following? var a = { “foo”: “bar” }; var b = { “foo”: “bar” };
false
An (inner) function enjoys access to the parameters and variables of all the functions it is nested in. This is called:
Lexical scoping
Given the following code: var myVar = ‘5’; var myAddedVar = myVar + 10; What is the value of (myAddedVar.constructor === Number)?
false
hich of the following is NOT a valid way to write a loop that will iterate over the values in the array in variable “myArray”?
None of these, they are all valid
Which two values are logged by the following code? var x = 5; (function () { console.log(x); var x = 10; console.log(x); }());
undefined 10
What is the output? var one; var two = null; console.log(one == two, one === two);
true false
Which of the following assigned values of x will cause (x == x) to return false?
All of the answers
function() { ‘use strict'; foo = “bar”; })();
It throws an error : foo is not defined
Object.keys(x)
returns the enumerable properties of x as an array of strings.
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?
undefined
What does the following return? Number(null);
0
What is the value of x.length after running this code? x = [“foo”]; x.quux = “Hello”; x[1] = “bar”;
2
What is the difference between the two declaration methods below? var functionOne = function() { /* some code */ } function functionTwo() { /* some code */ }
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.
What will be in console after executing this code: console.log(1 + ‘1’ – 1);
10
When reserved words are used as keys in object literals they must be ______?
quoted
What is the result of: function foo() { output( “biz ” + bar() ); } bar(); var bar = function() { return “baz”; }
TypeError: Undefined is not a function
Mathematical expression, “10” – (12+5).toString(), evaluates to what?
-7
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); }
“name”, “email”, “sendmail”
How does a for loop start?
for (i = 0; i < variable.length; i++) { //do something }
The expression (typeof NaN === “number”) evaluates to:
true
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;
“Hi”
true + true will return :
2
Math.Pi returns the mathematical constant of Pi. What standard JavaScript method would truncate Math.Pi to 3.14 ?
Math.Pi.toFixed(2)
How does JavaScript interpret numeric constants outside of strict mode?
As octal if they are preceded by a zero, and as hexadecimal if they are preceded by a zero and “x”
What is the value of x? var x = typeof NaN;
“number”
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();
“foo foo “
What will: typeof typeof(null) return?
string
Evaluate: undefined + 2
NaN
Which of the following String prototype method takes a regular expression?
search()
What is the value of x? var z = [typeof z, typeof y][0]; var x = typeof typeof z;
“string”
How can you remove an element from an array and replace it with a new one ?
array.splice(…)
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();
Matt Matt
function Question() { this.answered = false; } Question.prototype.rightAnswer = 5; console.log( new Question().rightAnswer, Question.rightAnswer ); What gets printed to the console?
5 undefined
Which operator has highest precedence?
*
Evaluate: new Boolean(new Boolean(false)).valueOf()
true
What will be the result of this expression: void 0
undefined
Which of the following is not a reserved word in the language
and
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);
1 2
What is the value of x? var x = 10/0;
Infinity
console.log( typeof [1,2] ) will print out:
object
Given the following code, what will myFunction(123, false, “test”) return? function myFunction(param) { return arguments[1] || param; }
123
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();
x = “Hello from foo”; y = “I am global y”;
What will be the value of result? function foo(bar) { return bar ? bar == foo : foo(foo); } var result = foo();
true
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))?
[object Object]
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);
‘Nut’ then ‘Bolt’
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?
var newArray = oldArray.slice(0);
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?
“A” “B”
Whats the output of the below : for(var i = 0; i < 10;i++){ setTimeout(function(){console.log(i) }, 10)}
10 10 10 10 10 10 10 10 10 10
function foo(){ var tmp = ‘one_two_three_four_five'; return tmp.replace(‘_’, ‘+’); } What does foo() return?
one+two_three_four_five
bar”.split().length returns:
1
Math.min() < Math.max(); will return
false
What is the output of the following? var x = 1; console.log(x++ + ++x + x);
7
ar q = null; q++; What is q?
1
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’);
undefined
unction foo() { this = “foo”; } var a = foo(); What will the preceding code produce?
ReferenceError: Invalid left-hand side in assignment
What does Math.random() do?
Returns a random number from and including 0 to less than 1.
What value is passed to function “foo” as first argument? foo( +”5″ );
5
What will this code produce: +new Date()
Unix timestamp in milliseconds (UTC timezone)
Why the last statement is evaluated to false ? var a = 0.1; var b = 0.2; 0.3 === a + b;
Because a + b represents a floating point result
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;
true
Evaluate the following expression: ~-(2 + “2”)
21
Which of the following Array prototype method actually modifies the array it’s been called on?
splice()
What is the value of “x” after the following code runs? var x; x++;
NaN
var obj1 = {}; var obj2 = {}; What are the value of the below: !!(obj1 && obj2) (obj1 && obj2)
true {}
Which of the following expressions evaluates to false?
new Boolean(‘false’) == false
Math.log(x) returns:
Logarithm base e (Euler’s number) of x.
What does the following return? Math.max();
-Infinity
What will the following code, when evaluated, do? var void = function () {};
Throw a SyntaxError
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();
Error thrown when running the code
n JavaScript (ES5) Oriented-Object Programming, is it possible to have a member property of a constructor function to a private state ?
Yes, by nesting your constructor function into a IIFE and creating local variables in it
Object(“s”) instanceof String === “s” instanceof String
false
console.log(1 + +”2″ + “2”)
32
Which are the different value types in JavaScript?
boolean, number, string, function, object and undefined
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) {}
Because the “length” property has internal [[Enumerable]] set to false.
What is the value of x? var a = “abc”; var x = a instanceof String;
false
What’s the correct syntax for creating a Date object for January 10, 1998?
new Date(1998, 0, 10);
Evaluate: ![]+[]
‘false’

No comments:

Post a Comment

Alternative content