JavaScript Questions & Answers – Web Workers


This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Web Workers”.

1. Which of the following is one of the fundamental features of JavaScript?
a) Single-threaded
b) Multi-threaded
c) Both Single-threaded and Multi-threaded
d) Simple-threaded
View Answer

Answer: a
Explanation: In computer programming, single-threading is the processing of one command at a time. One of the fundamental features of client-side JavaScript is that it is single-threaded: a browser will never run two event handlers at the same time, and it will never trigger a timer while an event handler is running.

2. Which of the following functions are synchronous?
a) load()
b) require()
c) both load() and require()
d) create()
View Answer

Answer: c
Explanation: JavaScript has two synchronous load() and require(). The load() method loads data from a server and puts the returned data into the selected element.

3. Why shouldn’t JavaScript functions not be too long?
a) User friendliness
b) Tie up event loops
c) Browser becomes unresponsive
d) All of the mentioned
View Answer

Answer: d
Explanation: When JavaScript code runs for longer than a predefined amount of time than the browser shows a message of unresponsive script. The client-side JavaScript functions must not run too long: otherwise, they will tie up the event loop and the web browser will become unresponsive to user input.

4. The object that looks to the thread that creates it is _______________
a) Window
b) Worker
c) Element
d) Hash
View Answer

Answer: b
Explanation: A web worker is a JavaScript running in the background, without affecting the performance of the page. The Worker object: this is what a worker looks like from the outside to the thread that creates it.

5. Which of the following is a global object for a new worker?
a) WorkerGlobalScope
b) Worker
c) WorkerScope
d) Window
View Answer

Answer: a
Explanation: A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. The WorkerGlobalScope is the global object for a new worker, and it is what a worker thread looks like, on the inside, to itself.

6. Which will be invoked to create a new worker?
a) Function
b) Destructor
c) Constructor
d) Interface
View Answer

Answer: c
Explanation: When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. To create a new worker, just use the Worker() constructor, passing a URL that specifies the JavaScript code that the worker is to run :

var loader = new Worker("utils/loader.js");

7. What will happen if you specify an absolute URL in the Worker constructor?
a) Resolves itself
b) Must have the same origin
c) Must not have the same origin
d) Specify the address
View Answer

Answer: b
Explanation: If you specify an absolute URL, it must have the same origin (same protocol, host, and port) as that containing document. In Firefox, if you want to use workers in extensions and would like to have access to js-ctypes, you should use the ChromeWorker object instead.

8. How can you send data using a Worker object?
a) postMessage()
b) sendMessage()
c) Message()
d) post()
View Answer

Answer: a
Explanation: Once you have a Worker object, you can send data to it with postMessage(). The value you pass to postMessage() will be cloned, and the resulting copy will be delivered to the worker via a message event. postMessage() sends a message — which can consist of any JavaScript object — to the worker’s inner scope.


9. Which property is used to manage multiple event handlers?
a) onmessage
b) onerror
c) both onmessage and onerror
d) postmessage
View Answer

Answer: c
Explanation: message event is fired when the worker’s parent receives a message from its worker which is also available via the onmessage property. You can use onmessage and onerror properties if you want to manage multiple event handlers.

10. Which is the function that allows a worker to terminate itself?
a) close()
b) exit()
c) terminate()
d) halt()
View Answer

Answer: a
Explanation: Worker.terminate() Immediately terminates the worker. This does not offer the worker an opportunity to finish its operations. The close() function allows a worker to terminate itself, and it is similar in effect to the terminate() method of a Worker object.

Sanfoundry Global Education & Learning Series – Javascript Programming.


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