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
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.
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
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
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.
- 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.
- Objects are kept in memory while they are accessible from roots through a reference or a chain of references.
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
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
Explanation: jQuery.cache[id] is used to associate handlers and other data with elements. When the data is read from an element:
- The element unique number is retrieved from id = elem[ jQuery.expando].
- The data is read from jQuery.cache[id].
6. The style property belongs to which of the following object?
a) Element
b) Window
c) Location
d) Navigation
View Answer
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
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.
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
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
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.
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
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.
- Practice Programming MCQs
- Check Programming Books
- Practice Information Science MCQs
- Apply for Computer Science Internship
- Check JavaScript Books