142). What kind of scoping does JavaScript use?
[A]Literal
[B]Lexical
[C]Segmental
[D]Sequential
Show Answer
Correct Answer: Lexical
Notes:
Answer: b
Explanation: 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.
143). 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]Return the value
Show Answer
Correct Answer: Reference the current scope chain
Notes:
Answer: c
Explanation: 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.
144). What is 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]Function return value
Show Answer
Correct Answer: Both Function objects and Scope where function’s variables are resolved
Notes:
Answer: c
Explanation: 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.
145). Which of the following is not an example of closures?
[A]Objects
[B]Variables
[C]Functions
[D]Graphics
Show Answer
Correct Answer: Graphics
Notes:
Answer: d
Explanation: In JavaScript, closures are created every time a function is created, at function creation time. Technically, all JavaScript functions are closures: they are objects, and they have a scope chain associated with them.
146). Which of the following uses a lot of CPU cycles?
[A]GUI
[B]Statically generated graphics
[C]Dynamically generated graphics
[D]Generic scoping
Show Answer
Correct Answer: Dynamically generated graphics
Notes:
Answer: c
Explanation: Dynamic graphics for data, means simulating motion or movement using the computer. It may also be thought of as multiple plots linked by time. Hence dynamically generating graphics from real-time data uses a lot of CPU cycles.
147). What will be the function of the following JavaScript code?
var scope = "global scope";
function checkscope()
{
var scope = "local scope";
function f()
{
return scope;
}
return f;
}
[A] Returns value null
[B]Returns exception
[C]Returns the value in scope
[D]Shows an error message
Show Answer
Correct Answer: Returns the value in scope
Notes:
Answer: c
Explanation: In JavaScript, every running function, code block, and the script as a whole have an associated object known as the Lexical Environment. The above code snippet returns the value in scope.
148). What is the fundamental rule of lexical scoping?
var scope = "global scope";
function checkscope()
{
var scope = "local scope";
function f()
{
return scope;
}
return f;
}
[A]Functions are declared in the scope
[B]Functions are executed using scope chain
[C] Functions are declared outside the scope
[D]Variables are declared within the function
Show Answer
Correct Answer: Functions are executed using scope chain
Notes:
Answer: b
Explanation: 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.
149). What is the opposite approach to the lexical scoping?
var scope = "global scope";
function checkscope()
{
var scope = "local scope";
function f()
{
return scope;
}
return f;
}
[A] Literal scoping
[B]Static scoping
[C]Dynamic scoping
[D]Generic scoping
Show Answer
Correct Answer: Dynamic scoping
Notes:
Answer: c
Explanation: The opposite approach to the lexical scoping is the dynamic scoping. Dynamic scoping does not care how the code is written, but instead how it executes. Each time a new function is executed, a new scope is pushed onto the stack. This scope is typically stored with the function’s call stack. When a variable is referenced in the function, the scope in each call stack is checked to see if it provides the value.
150). What is the purpose of the dynamic scoping?
var scope = "global scope";
function checkscope()
{
var scope = "local scope";
function f()
{
return scope;
}
return f;
}
[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]Variable cannot be declared within the function
Show Answer
Correct Answer: Variables can be declared outside the scope
Notes:
Answer: a
Explanation: 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.
151). Which of the algorithmic languages is not lexical scoping standardized in?
var scope = "global scope";
function checkscope()
{
var scope = "local scope";
function f()
{
return scope;
}
return f;
}
[A]Ada
[B]Pascal
[C]Modula2
[D]Html
Show Answer
Correct Answer: Html
Notes:
Answer: d
Explanation: 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 Haskell.