JavaScript Questions & Answers – Closures

This set of 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
View Answer

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.

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) Return the value
View Answer

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.

3. 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
View Answer

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

4. Which of the following is not an example of closures?
a) Objects
b) Variables
c) Functions
d) Graphics
View Answer

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.

5. Which of the following uses a lot of CPU cycles?
a) GUI
b) Statically generated graphics
c) Dynamically generated graphics
d) Generic scoping
View Answer

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.

6. 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
View Answer

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

7. What is the fundamental rule of lexical scoping?
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
View Answer

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

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

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.

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) Variable cannot be declared within the function
View Answer

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.

10. Which of the algorithmic languages is not lexical scoping standardized in?
a) Ada
b) Pascal
c) Modula2
d) Html
View Answer

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.

Sanfoundry Global Education & Learning Series – Javascript Programming.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.