JavaScript Questions & Answers – Keyboard Events


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

1. When are the keyboard events fired?
a) When the user manually calls the button
b) When the user clicks a key
c) When the user calls the modifier
d) When the user right clicks the mouse
View Answer

Answer: b
Explanation: The keydown and keyup are the keyboard events are fired when the user presses or releases a key on the keyboard. They are generated for modifier keys, function keys, and alphanumeric keys.

2. How does a user generate multiple keydown events?
a) Repeating the same process
b) Pressing multiple keys
c) Pressing the key longer than usual
d) Pressing the key multiple times
View Answer

Answer: c
Explanation: If the user holds the key down long enough for it to begin repeating, there will be multiple keydown events before the keyup event arrives. Pressing the key for long time results in multiple calls to the function onkeypress.

3. Which property is used to specify the key type when pressed?
a) keyCode
b) keyType
c) keyName
d) keyProperty
View Answer

Answer: a
Explanation: The keyCode property returns the Unicode character code of the key that triggered the onkeypress event, or the Unicode key code of the key that triggered the onkeydown or onkeyup event. The event object associated with these events has a numeric keyCode property that specifies which key was pressed.

4. For what value does the keyCode property persists even when a Shift key is pressed for adding punctuation character.
a) Special characters
b) Alphabets
c) Alphanumeric
d) Digits
View Answer

Answer: d
Explanation: The number keys always generate keyCode values for the digit that appears on the key, even if you are holding down Shift in order to type a punctuation character.

5. Which of the following are not key event properties?
a) Code key
b) Alt Key
c) Ctrl Key
d) Shift Key
View Answer

Answer: a
Explanation: altKey, ctrlKeY, shiftKey, and metaKey are key event object’s properties, which are set to true if the corresponding modifier key is held down when the event occurs. The keyCode values of ShiftKey, ctrlKey, altKey are respectively 16, 17 and 18.

6. Which of the following key property holds the key name as a string?
a) keyName
b) key
c) keyName(string)
d) Nameofkey(string)
View Answer

Answer: b
Explanation: The DOM Level 3 Events defines a new key property that contains the key name as a string. Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object.

7. Which of the following is not the value the key property will hold if the key is a function key?
a) F8
b) F2
c) F9
d) Left
View Answer

Answer: d
Explanation: If the key is a function key, the key property will be a value like “F2”, ”F8” or ”F9”etc. The Keys from F1-F12 are known as function keys.

8. Which method is used to add a binding?
a) binding()
b) add_bind()
c) bind()
d) addbind()
View Answer

Answer: c
Explanation: The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

9. Which method is used to remove a binding?
a) Unbind()
b) removebind()
c) remove(Bind b)
d) unbind()
View Answer

Answer: d
Explanation: The unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs.

10. Which of the following are the parameters passed to the keymap after installation?
a) Key property
b) Key name
c) Keydown event’s object
d) Key stroke
View Answer

Answer: c
Explanation: After the keymap is installed, the following parameters are passed:

  1. The event object for the keydown event.
  2. The key identifier of the key that was pressed.

Identifier key contains the information of the key that is pressed and the object information about the function to be performed when the key is pressed.

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