Array And Function

Name: _____________________

Date: _____________________

Instructions: Answer all questions. Write your answers clearly in the space provided.

Question 1:

The pop() method of the array does which of the following task ?

A. decrements the total length by 1
B. increments the total length by 1
C. prints the first element but no effect on the length
D. None of the mentioned
Answer: _________
Question 2:

What will happen if reverse() and join() methods are used simultaneously ?

A. Reverses and stores in the same array
B. Reverses and concatenates the elements of the array
C. Reverses
D. All of the mentioned
Answer: _________
Question 3:

The primary purpose of the array map() function is that it

A. maps the elements of another array into itself
B. passes each element of the array and returns the necessary mapped elements
C. passes each element of the array on which it is invoked to the function you specify, and returns an array containing the values returned by that function
D. None of the mentioned
Answer: _________
Question 4:

The reduce and reduceRight methods follow a common operation called

A. filter and fold
B. inject and fold
C. finger and fold
D. fold
Answer: _________
Question 5:

The method or operator used to identify the array is

A. isarrayType()
B. ==
C. ===
D. typeof
Answer: _________
Question 6:

What is a closure?

A. Function objects
B. Scope where function’s variables are resolved
C. Both Function objects and Scope where function’s variables are resolved
D. None of the mentioned
Answer: _________
Question 7:

Which of the following are examples of closures?

A. Objects
B. Variables
C. Functions
D. All of the mentioned
Answer: _________
Question 8:

Which of the following uses a lot of CPU cycles?

A. GUI
B. Statically generated graphics
C. Dynamically generated graphics
D. All of the mentioned
Answer: _________
Question 9:

What is the fundamental rule of lexical scoping?

A. Functions are declared in the scope
B. Functions are executed using scope chain
C. All of the mentioned
D. None of the mentioned
Answer: _________
Question 10:

What is the opposite approach to the lexical scoping?

A. Literal scoping
B. Static scoping
C. Dynamic scoping
D. Generic scoping
Answer: _________
Question 11:

What is the purpose of the dynamic scoping?

A. Variables can be declared outside the scope
B. Variables must be declared outside the scope
C. Variables cannot be declared outside the scope
D. None of the mentioned
Answer: _________
Question 12:

Which of the algorithmic languages is lexical scoping standardized in?

A. Ada
B. Pascal
C. Modula2
D. All of the mentioned
Answer: _________
Question 13:

The function definitions in JavaScript begins with

A. Identifier and Parantheses
B. Return type and Identifier
C. Return type, Function keyword, Identifier and Parantheses
D. Identifier and Return type
Answer: _________
Question 14:

When does the function name become optional in JavaScript?

A. When the function is defined as a looping statement
B. When the function is defined as expressions
C. When the function is predefined
D. All of the mentioned
Answer: _________
Question 15:

What is the purpose of a return statement in a function?

A. Returns the value and continues executing rest of the statements, if any
B. Returns the value and stops the program
C. Returns the value and stops executing the function
D. Stops executing the function and returns the value
Answer: _________
Question 16:

What will happen if a return statement does not have an associated expression?

A. It returns the value 0
B. It will throw an exception
C. It returns the undefined value
D. None of the mentioned
Answer: _________
Question 17:

A function with no return value is called

A. Procedures
B. Method
C. Static function
D. Dynamic function
Answer: _________
Question 18:

Do functions in JavaScript necessarily return a value ?

A. It is mandatory
B. Not necessary
C. Few functions return values by default
D. All of the mentioned
Answer: _________
Question 19:

What kind of scoping does JavaScript use?

A. Literal
B. Lexical
C. Segmental
D. Sequential
Answer: _________
Question 20:

What must be done in order to implement Lexical Scoping?

A. Get the object
B. Dereference the current scope chain
C. Reference the current scope chain
D. None of the mentioned
Answer: _________
Question 21:

Consider the following code snippet function printprops ( o ) { for(var p in o ) console . log ( p + ": " + o [ p ] + "n" )

} What will the above code snippet result ?

A. Prints the contents of each property of o
B. Returns undefined
C. All of the mentioned
D. None of the mentioned
Answer: _________
Question 22:

Consider the following code snippet function hypotenuse ( a, b ) { function square ( x ) { return x * x

} return Math . sqrt ( square ( a ) + square ( b ))

} What does the above code result?

A. Sum of square of a and b
B. Square of sum of a and b
C. Sum of a and b square
D. None of the mentioned
Answer: _________
Question 23:

Which of the following is the correct code for invoking a function without this keyword at all, and also too determine whether the strict mode is in effect?

A. var strict = (function { return this })
B. mode strict = (function() { return !this }())
C. var strict = (function() { return !this }())
D. mode strict = (function { })
Answer: _________
Question 24:

Which is an equivalent code to invoke a function m of class o that expects two arguments x and y?

A. o(x,y)
B. o.m(x) && o.m(y)
C. m(x,y)
D. o.m(x,y)
Answer: _________
Question 25:

Consider the following code snippet : var grand_Total =eval (" 10*10+5 ")

The output for the above statement would be :

A. 10*10+5
B. 105 as a string
C. 105 as an integer value
D. Exception is thrown
Answer: _________
Question 26:

Consider the following code snippet : var tensquared = ( function ( x ) {return x * x

}( 10 ))

Will the above code work ?

A. Yes, perfectly
B. Error
C. Exception will be thrown
D. Memory leak
Answer: _________
Question 27:

Consider the following code snippet : var string2Num =parseInt (" 123xyz ")

The result for the above code snippet would be :

A. 123
B. 123xyz
C. Exception
D. NaN
Answer: _________
Question 28:

If you have a function f and an object o, you can define a method named m of o with

A. o.m=m.f
B. o.m=f
C. o=f.m
D. o=f
Answer: _________
Question 29:

For the below mentioned code snippet: var o = new Object ()

The equivalent statement is:

A. var o = Object()
B. var o
C. var o= new Object
D. Object o=new Object()
Answer: _________
Question 30:

What is the difference between the two lines given below ? !!( obj1 && obj2 )

( obj1 && obj2 )

A. Both the lines result in a boolean value “True”
B. Both the lines result in a boolean value “False”
C. Both the lines checks just for the existence of the object alone
D. The first line results in a real boolean value whereas the second line merely checks for the existence of the objects
Answer: _________
Question 31:

Consider the following code snippet : var c = counter () , d = counter ()

c . count () d . count () c . reset () c . count () d . count () The state stored in d is :

A. 1
B. 0
C. Null
D. Undefined
Answer: _________
Question 32:

Consider the following code snippet : var c = counter () , d = counter ()

function constfuncs () { var funcs = []

for(var i = 0

i < 10

i ++ ) funcs [ i ] = function () { return i

}

return funcs

} var funcs = constfuncs ()

funcs [ 5 ]() What does the last statement return ?

A. 9
B. 0
C. 10
D. None of the mentioned
Answer: _________
Question 33:

Consider the following code snippet : var scope = "global scope"
function checkscope () { var scope = "local scope"
function f ()
{
return scope
}
return f
What is the function of the above code snippet?

A. Returns value null
B. Returns exception
C. Returns the value in scope
D. None of the mentioned
Answer: _________
Question 34:

Consider the code snippet given below var count = [ 1 ,, 3 ]
What is the observation made?

A. The omitted value takes “undefined”
B. This results in an error
C. This results in an exception
D. None of the mentioned
Answer: _________
Question 35:

Consider the following code snippet var a1 = [,,,]
var a2 = new Array ( 3 )
0 in a1 0 in a2 The result would be

A. true false
B. false true
C. true true
D. None of the above
Answer: _________
Question 36:

Consider the following code snippet : if ( !a [ i ]) continue
What is the observation made ?

A. Skips the undefined elements
B. Skips the non existent elements
C. Skips the null elements
D. All of the mentioned
Answer: _________
Question 37:

Consider the following code snippet : var a = [ 1 , 2 , 3 , 4 , 5 ]
a . slice ( 0 , 3 )
What is the possible output for the above code snippet ?

A. Returns [1,2,3]
B. Returns [4,5]
C. Returns [1,2,3,4]
D. Returns [1,2,3,4,5]
Answer: _________
Question 38:

Consider the following code snippet : var a = []
a . unshift ( 1 )
a . unshift ( 22 )
a . shift ()
a . unshift ( 3 , [ 4 , 5 ])
a . shift ()
a . shift ()
a . shift ()
The final output for the shift() is

A. 1
B. [4,5]
C. [3,4,5]
D. Exception is thrown
Answer: _________

Answer Key

1: A
Solution: Arrays have a pop() method (it works with push()) that reduces the length of an array by 1 but also returns the value of the deleted element.
2: A
Solution: The reverse() followed by a join() will reverse the respective array and will store the reversed array in the memory.
3: C
Solution: The map() method passes each element of the array on which it is invoked to the functionyou specify, and returns an array containing the values returned by that function.
4: B
5: D
Solution: The typeof property is used to identify the array type.
6: C
Solution: A combination of a function object and a scope (a set of variable bindings) in which the function’s variables are resolved is called a closure.
7: D
Solution: Technically, all JavaScript functions are closures: they are objects, and they have a scope chain associated with them.
8: C
Solution: Dynamically generating graphics from real-time data uses a lot of CPU cycles.
9: B
Solution: The fundamental rule of lexical scoping is that the JavaScript functions are executed using the scope chain that was in effect when they were defined.
10: C
Solution: The opposite approach to the lexical scoping is the dynamic scoping.
11: A
Solution: Dynamic scoping creates variables that can be called from outside the block of code in which they are defined. A variable declared in this fashion is sometimes called a public variable.
12: D
Solution: Lexical scoping is standardized in all algorithmic languages (ALGOL), such as Ada, Pascal, and Modula2. Additionally, it is used in modern functional languages like ML and Haskel.
13: A
Solution: The function definitions begin with the keyword function followed by an identifier that names the function and a pair of parantheses around a comma-separated list of zero or more identifiers.
14: B
Solution: The function name is optional for functions defined as expressions. A function declaration statement actually declares a variable and assigns a function object to it.
15: D
Solution: The return statement causes the function to stop executing and to return the value of its expression (if any) to the caller.
16: C
Solution: If the return statement does not have an associated expression, it returns the undefined value.
17: A
Solution: Functions with no return value are sometimes called procedures.
18: C
19: B
Solution: Like most modern programming languages, JavaScript uses lexical scoping. This means that functions are executed using the variable scope that was in effect when they were defined, not the variable scope that is in effect when they are invoked.
20: C
Solution: In order to implement lexical scoping, the internal state of a JavaScript function object must include not only the code of the function but also a reference to the current scope chain.
21: B
Solution: The above code snippet returns undefined.
22: A
Solution: The above code snippet contains nested function in which the function hypotenuse(a,b) has another function inside its scope, function square(x). The interesting thing about nested functions is their variable scoping rules. They can acceess the parameters and variables of the function (or functions) they are nested within.
23: C
Solution: The above code defines and invokes a function to determine if we’re in strict mode.
24: D
Solution: The code above is an invocation expression: it includes a function expression o.m and two argument expressions, x and y.
25: C
Solution: Even if the string value passed as a parameter to eval does represent a numeric value the use of eval() results in an error being generated.
26: A
Solution: Function name is optional for functions defined as expressions. Function expressions are sometimes defined and immediately invoked.
27: B
Solution: The parseInt() function returns the first integer contained in the string or 0 if the string does not begin with an integer.
28: A
Solution: A method is nothing more than a JavaScript function that is stored in a property of an object. If you have a function f and an object o, you can define a method named m of o with the following line: o.m = f

29: C
Solution: You can always omit a pair of empty parentheses in a constructor invocation.
30: D
31: A
Solution: The state stored in d is 1 because d was not reset.
32: C
Solution: The code above creates 10 closures, and stores them in an array. The closures are all defined within the same invocation of the function, so they share access to the variable i. When constfuncs() returns, the value of the variable i is 10, and all 10 closures share this value. Therefore, all the functions in the returned array of functions return the same value.
33: C
Solution: The above code snippet returns the value in scope.
34: A
Solution: If you omit a value from an array literal, the omitted element is given the value.
35: A
Solution: a1 has an element with index 0 and a2 has no element with index 0.
36: D
37: A
38: A
Solution: The unshift() and shift() methods behave much like push() and pop(), except that they insert and remove elements from the beginning of an array rather than from the end. unshift() adds an element or elements to the beginning of the array, shifts the existing array elements up to higher indexes to make room, and returns the new length of the array. shift() removes and returns the first element of the array, shifting all subsequent elements down one place to occupy the newly vacant space at the start of the array.