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.

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

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.

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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

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.

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.