JavaScript Questions & Answers – Cache Variables and Properties across Scope

«
»

This set of JavaScript Problems focuses on “Cache Variables and Properties across Scope”.

1. What does the interpreter do when you reference variables in other scopes?
a) Traverses the queue
b) Traverses the stack
c) Finds the bugs
d) Traverse the array
View Answer

Answer: b
Explanation: The interpreter executes the javascript code. Normally when you reference variables in other scopes at the global level, in other namespaces, and so on—the interpreter needs to traverse the stack to get to the variable.
advertisement

2. The attribute location belongs to which element?
a) document
b) html
c) image
d) pre
View Answer

Answer: a
Explanation: window.location.href returns the href (URL) of the current page. The attribute location belongs to the document element.

3. What will happen if you reference document.location from within an object?
a) Traverses the queue
b) Finds the bugs
c) Traverses the stack
d) Traverses the array
View Answer

Answer: c
Explanation: window.location.href returns the href (URL) of the current page. If you reference document.location from within an object, the interpreter will need to go from the function that references the variable, up out of the namespace to the global window scope, down to the document scope, and gets the location value.

4. Why do we need to create locally scoped variables to hold value?
a) To optimize the testing process
b) To increase the speed
c) To minimize memory usage
d) To cache the reference document.location
View Answer

Answer: d
Explanation: window.location.assign loads a new document. The locally scoped variables are created to cache the reference to document.location.
advertisement

5. What is the next step after calling the startTimeLogging()?
a) Interpret the code
b) Compile the code
c) Run the code
d) Debug the code
View Answer

Answer: c
Explanation: The startTimeLogging() method captures the timing data for ad hoc, etc for referencing an uncached document.location. Once the startTimeLogging() method is called, run the code to test.

6. During the traversing through the stack, where does it go after it goes to the namespace?
a) Window
b) Function
c) Document
d) Location
View Answer

Answer: a
Explanation: Namespace in JavaScript is nothing but a single global object which will contain all our functions, methods, variables and all that. During the traversal in the stack manner, after going to the namespace, it goes to the Window.

7. During the traversing through the stack, where does it go after it goes to the window?
a) Namespace
b) Function
c) Document
d) Nowhere
View Answer

Answer: d
Explanation: window.location.assign loads a new document. During the traversal in the stack manner, after going to the window, it goes nowhere.
advertisement

8. During the traversing of the stack when you create a locally scoped variable, where does it go after it goes to the namespace?
a) Window
b) Function
c) Document
d) Location
View Answer

Answer: b
Explanation: Namespace in JavaScript is nothing but a single global object which will contain all our functions, methods, variables and all that. When you create a locally scoped variable for caching the reference, the traversal reverses in the forward direction and thus, after going to the namespace, it goes to the function.

9. What is the function used to stop capturing the ad hoc timing ?
a) stopadhoc()
b) stopTimer()
c) stopTimeLogging()
d) stophoc()
View Answer

Answer: c
Explanation: The startTimeLogging() method captures the timing data for ad hoc, etc for referencing an uncached document.location. In order to stop capturing the ad hoc timing for referencing uncached document.location, we call the method stopTimeLogging().

10. During the traversing of the stack when you create a locally scoped variable, where does it go after it goes to the location?
a) Window
b) Function
c) Document
d) Nowhere
View Answer

Answer: d
Explanation: window.location.assign loads a new document. When you create a locally scoped variable for caching the reference, the traversal reverses in the forward direction and thus, after going to the location, it goes nowhere.
advertisement

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn