JavaScript Questions and Answers – Closures

This set of Top ten Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Closures”.

1. What kind of scoping does JavaScript use?
a. Literal
b. Lexical
c. Segmental
d. Sequential

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.

2. 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. one of the mentioned

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.
3. What is the fundamental rule of lexical scoping?
a. Functions are declared in the scope
b. Functions are executed using scope chain
c. Both a and b
d. None of the mentioned

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.


4. Which of the following are examples of closures?
a. Objects
b. Variables
c. Functions
d. All of the mentioned

Explanation : Technically, all JavaScript functions are closures: they are objects, and they have a scope chain associated with them.

5. 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

Explanation : Dynamically generating graphics from real-time data uses a lot of CPU cycles.

6. 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
Explanation : The above code snippet returns the value in scope.
7. What is a closure?
a. Function objects
b. Scope where function’s variables are resolved
c. Both a and b
d. None of the mentioned

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.

8. What is the opposite approach to the lexical scoping?
a. Literal scoping
b. Static scoping
c. Dynamic scoping
d. Generic scoping

Explanation : The opposite approach to the lexical scoping is the dynamic scoping.

9. 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

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.

10. Which of the algorithmic languages is lexical scoping standardized in?
a. Ada
b. Pascal
c. Modula2
d. All of the mentioned

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 Haskel.

So, what other JavaScript questions are there about Closures?If you liked this Javascript MCQ, share, recommend or like below!

Comments

comments