JavaScript Questions and Answers Part-18

1. What are the events generated by the Node objects called?
a) generators
b) emitters
c) dispatchers
d) highevents

Answer: b
Explanation: There are two classes of events one is called event listener and the other is called event emitter. Node objects that generate events (known as event emitters) define an on() method for registering handlers.

2. What is the function used to deregister event handler ‘f’?
a) deleteAllListeners(name)
b) deleteListener(name,f)
c) removeListener(name,f)
d) removeAllListeners(name)

Answer: c
Explanation: The removeEventListener() method removes an event handler that has been attached with the addEventListener() method. The removeListeners(name,f) is used to deregister event handler f represented as :
emitter.removeListener(name,f)

3. What is the function used to remove all handlers for name events?
a) deleteAllListeners(name)
b) deleteListener(name,f)
c) removeListener(name,f)
d) removeAllListeners(name)

Answer: d
Explanation: The removeAllListeners(name) is used to remove all handlers from name events represented as :
emitter.removeAllListeners(name)

4. Which function is a synonym for on()?
a) addListener()
b) listeners()
c) once()
d) add()

Answer: a
Explanation: The on() method is used for registering handlers. addListener() is a synonym for on().

5. Which of the following is an event emitter?
a) once
b) process
c) listeners
d) on

Answer: b
Explanation: The process object is an event emitter. The Node defines other important globals under the process namespaces that contain properties of that object like version, argv, env, pid, getuid(), cwd(), chdir() and exit().

6. When do uncaught exceptions generate events?
a) When handlers are registered
b) When handlers are deregistered
c) When handler functions are called
d) When handlers do not have a matching catch clause

Answer: a
Explanation: The on() method and addListener() method perform the same task of acting as an event emitter. The on() and addListener() method is used for registering handlers.

7. Which among the following POSIX signals generate events?
a) SIGDOWN
b) SIGFLOAT
c) SIGINT
d) SIGSHORT

Answer: c
Explanation: The SIGINT is a POSIX signal that generates event. Simple code like below can do a proper clean up and exit on CTRL-C or SIGINT passed from command line / other application to the nodejs app’s ProcessID.
process.on('SIGINT', function()
console.log('SIGINT');
cleanup()
process.exit(1));

8. What is the method used to pause “data” events?
a) s.pause();
b) s.stop();
c) s.halt();
d) s.wait();

Answer: a
Explanation: Data events are the events which are performed by either the user or the browser. The above code snippet is used to pause data events, for throttling uploads.

9. When the “end” event fires on EOF when no more data will arrive, which function is called?
a) s.on(“data”,f);
b) s.on(“end”,f);
c) s.on(“error”,f);
d) s.on(“default”,f);

Answer: b
Explanation: ”EOF” stands for end of file.The above code snippet gets “end” event fired on EOF when no more data will arrive.

10. What will be the return value of the write() method when the Node cannot write the data immediately and has to buffer it internally?
a) 0
b) 1
c) true
d) false

Answer: d
Explanation: The write() method writes HTML expressions or JavaScript code to a document. The write() method is mostly used for testing: If it is used after an HTML document is fully loaded, it will delete all existing HTML. write() method never blocks. If Node cannot write the data immediately and has to buffer it internally, the write() method returns false.