1
Which keyword is used to define a function in JavaScript?
A. method
B. func
C. function
D. def
Correct Answer: function
Explanation:
In JavaScript, the 'function' keyword is used to declare a function.
2
What is the correct syntax to call a function named 'myFunction'?
A. call myFunction()
B. call function myFunction()
C. myFunction()
D. myFunction
Correct Answer: myFunction()
Explanation:
A function is executed (called) by using its name followed by parentheses.
3
Which statement is used to return a value from a JavaScript function?
A. send
B. output
C. return
D. value
Correct Answer: return
Explanation:
The 'return' statement stops the execution of a function and returns a value to the function caller.
4
If a function is called with fewer arguments than declared parameters, what value are the missing parameters assigned?
A. 0
B. false
C. undefined
D. null
Correct Answer: undefined
Explanation:
In JavaScript, if a function is called with missing arguments, the corresponding parameters are set to 'undefined'.
5
Which built-in object within a function contains an array-like list of all arguments passed to that function?
A. args
B. arguments
C. list
D. parameters
Correct Answer: arguments
Explanation:
JavaScript functions have a built-in object called 'arguments' containing an array of arguments used when the function was invoked.
6
What happens when a return statement is executed inside a function?
A. The function continues until the last line
B. The function stops executing immediately
C. The function prints the value
D. The function restarts
Correct Answer: The function stops executing immediately
Explanation:
When JavaScript reaches a return statement, the function will stop executing.
7
Which event occurs when the user clicks on an HTML element?
A. onclick
B. onchange
C. onmouseclick
D. onmouseover
Correct Answer: onclick
Explanation:
The 'onclick' event occurs when the user clicks on an element.
8
Which event is triggered when the mouse pointer moves over an element?
A. onmouseenter
B. onmouseover
C. onhover
D. onmove
Correct Answer: onmouseover
Explanation:
The 'onmouseover' event occurs when the mouse pointer is moved onto an element.
9
Which event is triggered when the mouse pointer moves out of an element?
A. onexit
B. onmouseoff
C. onmouseout
D. onmouseleave
Correct Answer: onmouseout
Explanation:
The 'onmouseout' event occurs when the mouse pointer is moved out of an element.
10
Which event handler is used to execute code when a user submits a form?
A. onsubmit
B. onexecute
C. onclick
D. onsend
Correct Answer: onsubmit
Explanation:
The 'onsubmit' event occurs when a form is submitted.
11
In form validation, what should the 'onsubmit' event handler return to prevent the form from submitting?
A. 0
B. true
C. false
D. null
Correct Answer: false
Explanation:
Returning 'false' from the onsubmit handler prevents the browser from submitting the form data to the server.
12
Which event fires when an input field loses focus?
A. onchange
B. onfocus
C. onstop
D. onblur
Correct Answer: onblur
Explanation:
The 'onblur' event occurs when an object loses focus.
13
Which event fires immediately when an input field gets focus?
A. onselect
B. onblur
C. onfocus
D. onclick
Correct Answer: onfocus
Explanation:
The 'onfocus' event occurs when an element gets focus.
14
Which event is triggered when the content of a form element, the selection, or the checked state have changed?
A. oninput
B. onmodify
C. onchange
D. onblur
Correct Answer: onchange
Explanation:
The 'onchange' event occurs when the value of an element has been changed.
15
Which event occurs when the user presses a key on the keyboard?
A. All of the above
B. onkeydown
C. onkeyup
D. onkeypress
Correct Answer: All of the above
Explanation:
Keyboard events include onkeydown, onkeypress, and onkeyup.
16
Which property of the event object returns the Unicode character code of the key that triggered the onkeypress event?
A. unicode
B. keyType
C. keyCode
D. keyChar
Correct Answer: keyCode
Explanation:
The 'keyCode' property returns the Unicode character code of the key that triggered the onkeypress event.
17
Which event is triggered when a user releases a key?
A. onkeyup
B. onkeyrelease
C. onkeydown
D. onkeypress
Correct Answer: onkeyup
Explanation:
The 'onkeyup' event occurs when the user releases a key.
18
How can you access the value of an input field with id='email' in JavaScript?
A. document.getElementById('email').text
B. document.getElement('email').value
C. document.getElementById('email').content
D. document.getElementById('email').value
Correct Answer: document.getElementById('email').value
Explanation:
The '.value' property is used to get the current value of an input element.
19
Which function is commonly used to check if a value is not a number during validation?
A. isNumber()
B. validateNumber()
C. isNaN()
D. checkNumber()
Correct Answer: isNaN()
Explanation:
The global isNaN() function determines whether a value is an illegal number (Not-a-Number).
20
To set the focus to a specific element in validation, which method is used?
A. element.active()
B. element.highlight()
C. element.select()
D. element.focus()
Correct Answer: element.focus()
Explanation:
The .focus() method is used to give focus to an HTML element.
21
What is the 'this' keyword usually referring to inside an HTML event attribute (e.g., onclick='func(this)')?
A. The function
B. The element itself
C. The window object
D. The document object
Correct Answer: The element itself
Explanation:
In an HTML event handler, 'this' refers to the HTML element that received the event.
22
Which event occurs when a user double-clicks on an element?
A. onclick2
B. on2click
C. ondoubleclick
D. ondblclick
Correct Answer: ondblclick
Explanation:
The 'ondblclick' event occurs when the user double-clicks on an element.
23
Which event occurs when a web page has finished loading?
A. onfinish
B. onstart
C. onload
D. onready
Correct Answer: onload
Explanation:
The 'onload' event occurs when an object (like the body/page) has been loaded.
24
Which event is triggered when text is selected in an input or textarea?
A. onhighlight
B. onchoose
C. onmark
D. onselect
Correct Answer: onselect
Explanation:
The 'onselect' event occurs after some text has been selected in an element.
25
Which method is used to test if a string matches a regular expression pattern?
A. validate()
B. verify()
C. check()
D. test()
Correct Answer: test()
Explanation:
The RegExp test() method checks for a match in a string and returns true or false.
26
When validating an email, which logic is best to check for the presence of '@'?
A. email.length > 0
B. email.indexOf('@') > -1
C. email.contains('@')
D. email.value == '@'
Correct Answer: email.indexOf('@') > -1
Explanation:
indexOf('@') returns the position of the character. If it returns -1, the character is not found.
27
What is the correct way to access a form named 'loginForm' via the document object?
A. document.loginForm
B. document.getForm('loginForm')
C. document.forms['loginForm']
D. Both A and B
Correct Answer: Both A and B
Explanation:
Forms can be accessed directly by name as a property of document or via the forms collection.
28
Which property indicates the number of characters in a string value of an input field?
A. width
B. size
C. count
D. length
Correct Answer: length
Explanation:
String values have a 'length' property that returns the number of characters.
29
Which event handler is best suited for validating a field immediately after the user leaves it?
A. onmouseover
B. onload
C. onblur
D. onfocus
Correct Answer: onblur
Explanation:
onblur fires when the user leaves the field, making it ideal for immediate field-level validation.
30
What is the purpose of 'event.preventDefault()'?
A. To stop the event from bubbling
B. To refresh the page
C. To prevent the default action of the element
D. To stop the script
Correct Answer: To prevent the default action of the element
Explanation:
preventDefault() cancels the event if it is cancelable, meaning that the default action (like submitting a form) will not occur.
31
Which event is triggered when a form is reset?
A. onclear
B. onempty
C. onreset
D. ondelete
Correct Answer: onreset
Explanation:
The 'onreset' event occurs when the Reset button in a form is clicked.
32
How do you check if a checkbox with id='agree' is checked?
A. document.getElementById('agree').on
B. document.getElementById('agree').selected
C. document.getElementById('agree').value == 'on'
D. document.getElementById('agree').checked == true
Correct Answer: document.getElementById('agree').checked == true
Explanation:
Checkboxes have a 'checked' boolean property indicating their state.
33
Can a JavaScript function return another function?
A. Only if it returns a string
B. Yes, this is called a closure or higher-order function
C. No, never
D. Only in strict mode
Correct Answer: Yes, this is called a closure or higher-order function
Explanation:
JavaScript functions are first-class citizens and can return other functions.
34
Which mouse event is fired when a mouse button is pressed down (before releasing)?
A. onmousedown
B. onmouseup
C. onmousepress
D. onclick
Correct Answer: onmousedown
Explanation:
The 'onmousedown' event occurs when a mouse button is pressed down over an element.
35
To validate that a password is at least 8 characters long, which condition is used?
A. password.val.len >= 8
B. password.length >= 8
C. password.value.length >= 8
D. password.value.size >= 8
Correct Answer: password.value.length >= 8
Explanation:
You must access the '.value' of the input first, then the '.length' property of that string.
36
What defines the scope of a variable declared with 'var' inside a function?
A. Block scope
B. Document scope
C. Function scope
D. Global scope
Correct Answer: Function scope
Explanation:
Variables declared with 'var' inside a function are local to that function (Function Scope).
37
Which of the following is an anonymous function?
A. var myFunc = function() {}
B. function myFunc() {}
C. Both B and C
D. function() {}
Correct Answer: Both B and C
Explanation:
An anonymous function is a function without a name. It can be used in an expression or assigned to a variable.
38
Which regex syntax is used to create a Regular Expression object?
A. Both A and B
B. new RegExp('pattern', 'flags')
C. /pattern/flags
D. RegExp(/pattern/)
Correct Answer: Both A and B
Explanation:
Regular expressions can be created using literal syntax (/.../) or the RegExp constructor.
39
In a dropdown list (select tag), how do you get the index of the selected option?
A. element.optionIndex
B. element.selectedIndex
C. element.choice
D. element.value
Correct Answer: element.selectedIndex
Explanation:
The 'selectedIndex' property sets or returns the index of the selected option in a drop-down list.
40
What is the default return value of a function that does not have a return statement?
A. 0
B. undefined
C. void
D. null
Correct Answer: undefined
Explanation:
If a function does not return a value, it returns 'undefined' by default.
41
Which event is commonly used to detect if a user is typing inside a text box?
A. onload
B. onclick
C. onselect
D. onkeyup
Correct Answer: onkeyup
Explanation:
onkeyup (or oninput) is commonly used to trigger code as the user types.
42
How can you stop a function execution if a validation error occurs?
A. exit
B. break
C. return false
D. stop
Correct Answer: return false
Explanation:
Returning 'false' is the standard way to indicate failure in a validation function and stop subsequent logic (often used to stop form submission).
43
Which JavaScript function displays a dialog box with a message and an OK button?
A. prompt()
B. alert()
C. msg()
D. confirm()
Correct Answer: alert()
Explanation:
The alert() method displays an alert box with a specified message and an OK button.
44
To check if a radio button is selected, which property do we check?
A. checked
B. toggled
C. active
D. selected
Correct Answer: checked
Explanation:
Radio buttons, like checkboxes, use the 'checked' property.
45
When handling the 'onkeypress' event, how can you allow only numbers to be entered?
A. Check if event.key is 'Number'
B. Disable the keyboard
C. Check if event.keyCode is between 48 and 57
D. Use type='text'
Correct Answer: Check if event.keyCode is between 48 and 57
Explanation:
ASCII codes 48 to 57 correspond to digits 0-9. Checking keyCode allows blocking non-numeric input.
46
Which is the correct syntax to bind an event handler in HTML?
A. <button onclick='myFunc()'>
B. <button click='myFunc()'>
C. <button do='myFunc()'>
D. <button event='myFunc()'>
Correct Answer: <button onclick='myFunc()'>
Explanation:
The standard HTML attribute syntax for a click event is onclick='...'.
47
What is passed to the event handler function if defined as 'element.onclick = myFunction;'?
A. The Event object
B. The element
C. Nothing
D. The document
Correct Answer: The Event object
Explanation:
When assigning a function to an event property in JS, the browser automatically passes the event object as the first argument.
48
Which function converts a string argument to an integer?
A. parseInt()
B. NumberString()
C. toInteger()
D. Float()
Correct Answer: parseInt()
Explanation:
The parseInt() function parses a string argument and returns an integer.
49
In validation, if a field is empty, what does 'field.value' equal?
A. 0
B. null
C. "" (empty string)
D. undefined
Correct Answer: "" (empty string)
Explanation:
An empty HTML input field returns an empty string ("") via the .value property.
50
What is the order of mouse events when clicking a button?
A. onmouseup -> onmousedown -> onclick
B. onclick -> onmouseup -> onmousedown
C. onclick -> onmousedown -> onmouseup
D. onmousedown -> onmouseup -> onclick
Correct Answer: onmousedown -> onmouseup -> onclick
Explanation:
The sequence is: mouse button goes down, mouse button goes up, then the click event is fired.