JavaScript Questions & Answers – JavaScript and Memory Leak

«
»

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “JavaScript and Memory Leak”.

1. Where are memory leaks found?
a) Client side objects
b) Server side objects
c) Both Client side and Server side objects
d) User side objects
View Answer

Answer: a
Explanation: Memory leaks happen when your code needs to consume memory in your application, which should be released after a given task is completed but isn’t. Memory leaks occur when we are developing client-side reusable scripting objects.
advertisement

2. Which handler is triggered when the content of the document in the window is stable and ready for manipulation?
a) onload
b) manipulate
c) create
d) oncreate
View Answer

Answer: a
Explanation: One of the most important event handlers is the onload handler of the Window object. It is triggered when the content of the document displayed in the window is stable and ready to be manipulated. JavaScript code is commonly wrapped within an onload event handler.

3. What is the central concept of JavaScript memory management?
a) Reliability
b) Reachability
c) Efficiency
d) Transparency
View Answer

Answer: b
Explanation: The central concept of JavaScript memory management is a concept of reachability. The main cause for leaks in garbage collected languages are unwanted references.

  1. A distinguished set of objects are assumed to be reachable: these are known as the roots. Typically, these include all the objects referenced from anywhere in the call stack (that is, all local variables and parameters in the functions currently being invoked), and any global variables.
  2. Objects are kept in memory while they are accessible from roots through a reference or a chain of references.
advertisement
advertisement

4. When does a memory leak happen?
a) Browser doesn’t release memory from objects unnecessary
b) Browser releases too many memories
c) Browser releases memory iteratively
d) Browser releases memory quickly
View Answer

Answer: a
Explanation: Memory leaks happen when your code needs to consume memory in your application, which should be released after a given task is complete but isn’t. Memory leak happens when the browser for some reason doesn’t release memory from objects which are not needed any more.

5. What will happen when the data of the jQuery.cache is read from an element?
a) Unique number is retrieved as elem[jQuery.expando]
b) Data is read from jQuery.cache[id]
c) Unique number is retrieved as elem[jQuery.expando] & Data is read from jQuery.cache[id]
d) Data is cleared from jQuery.cache[id]
View Answer

Answer: c
Explanation: jQuery.cache[id] is used to associate handlers and other data with elements. When the data is read from an element:

  1. The element unique number is retrieved from id = elem[ jQuery.expando].
  2. The data is read from jQuery.cache[id].
advertisement

6. The style property belongs to which of the following object?
a) Element
b) Window
c) Location
d) Navigation
View Answer

Answer: a
Explanation: Each Element object has style and className properties that allow scripts to specify CSS styles for a document element or to alter the CSS class names that apply to the element.

7. Which of the following functions are referenced internally?
a) setTimeout
b) setInterval
c) both setTimeout and setInterval
d) clearInterval
View Answer

Answer: c
Explanation: setTimeout(function, milliseconds) executes a function, after waiting a specified number of milliseconds. Functions used in setTimeout/setInterval are referenced internally and tracked until complete, then cleaned up.
advertisement

8. What is the purpose of destroying the functions and objects?
a) Consume unnecessary CPU cycles
b) Prevent the dropping of reference count to 0
c) Centralize the responsibility to clean up
d) All of the mentioned
View Answer

Answer: d
Explanation: The primary purpose of a destroy function is to centralize the responsibility for cleaning up anything that the object has done that will:

  • Prevent its reference count from dropping to 0 (for example, removing problematic event listeners and callbacks and unregistering from any services).
  • Consume unnecessary CPU cycles, such as intervals or animations.

9. When does a cycle occur during memory leak?
a) No reference occurs
b) Two objects reference
c) One object gets referenced
d) Three object gets referenced
View Answer

Answer: b
Explanation: Old versions of Internet Explorer could not detect cyclic references between DOM nodes and JavaScript code. A cycle happens when two objects reference each other in such a way that both objects retain each other.
advertisement

10. Which of the following is a way to retain an object in memory?
a) Console Log
b) Closures
c) Destroy objects
d) Clear object
View Answer

Answer: a
Explanation: Any object inside the timer will hold a reference in order to run that piece of code somewhere in the future without any problems. One particularly obscure way to retain an object in memory is to log it to the console.

Sanfoundry Global Education & Learning Series – Javascript Programming.

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!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
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 | Youtube | Instagram | Facebook | Twitter