Spread the love

JavaScript MCQ:-

  1. What does JavaScript use to indicate the start and end of a block of code?
    • A) Curly braces { }
    • B) Parentheses ( )
    • C) Square brackets [ ]
    • D) Angle brackets < >
  2. Which of the following is the correct syntax to create a function in JavaScript?
    • A) function myFunction[] {}
    • B) function myFunction() {}
    • C) function myFunction() () {}
    • D) function myFunction{ }
  3. How do you declare a variable in JavaScript?
    • A) var variableName;
    • B) variableName = var;
    • C) variableName: var;
    • D) declare variableName;
  4. Which operator is used to assign a value to a variable in JavaScript?
    • A) ==
    • B) !=
    • C) =
    • D) ===
  5. How do you write a single-line comment in JavaScript?
    • A) /* This is a comment */
    • B) <!-- This is a comment -->
    • C) // This is a comment
    • D) -- This is a comment
  6. Which of the following is not a valid JavaScript data type?
    • A) String
    • B) Number
    • C) Boolean
    • D) Character
  7. What will the following code output: console.log(typeof 'Hello');?
    • A) string
    • B) String
    • C) text
    • D) char
  8. Which method is used to remove whitespace from both ends of a string?
    • A) trim()
    • B) strip()
    • C) remove()
    • D) cut()
  9. How can you convert a string to an integer in JavaScript?
    • A) parseInt()
    • B) parseString()
    • C) toInteger()
    • D) convert()
  10. What does the push() method do in JavaScript arrays?
    • A) Removes the last element
    • B) Adds an element to the end
    • C) Adds an element to the beginning
    • D) Removes the first element
  11. Which keyword is used to create a new object in JavaScript?
    • A) object
    • B) new
    • C) create
    • D) this
  12. How do you check if two values are equal in JavaScript, considering type?
    • A) ==
    • B) ===
    • C) !=
    • D) !==
  13. Which function is used to parse a JSON string into an object in JavaScript?
    • A) JSON.parse()
    • B) JSON.stringify()
    • C) parseJSON()
    • D) stringifyJSON()
  14. What will console.log(0.1 + 0.2 === 0.3); output?
    • A) true
    • B) false
    • C) undefined
    • D) NaN
  15. How can you access the first element of an array named arr?
    • A) arr[0]
    • B) arr.first()
    • C) arr.begin()
    • D) arr.get(1)
  16. Which statement is used to exit a loop in JavaScript?
    • A) stop
    • B) exit
    • C) break
    • D) continue
  17. What will the following code output: console.log(1 + '1');?
    • A) 2
    • B) 11
    • C) NaN
    • D) undefined
  18. How do you define a constant in JavaScript?
    • A) const variableName;
    • B) constant variableName;
    • C) let variableName;
    • D) const variableName = value;
  19. Which method is used to find the length of an array in JavaScript?
    • A) length()
    • B) size()
    • C) count()
    • D) length
  20. What does the return statement do in a function?
    • A) Exits the function
    • B) Returns a value from the function
    • C) Ends the loop
    • D) Terminates the program
  21. How can you check if a variable x is an instance of a particular object Y?
    • A) x.instanceof(Y)
    • B) x.isInstance(Y)
    • C) x instanceof Y
    • D) instanceof(x, Y)
  22. What is the default value of an uninitialized variable in JavaScript?
    • A) 0
    • B) null
    • C) undefined
    • D) NaN
  23. How do you create an empty object in JavaScript?
    • A) var obj = {};
    • B) var obj = [];
    • C) var obj = ();
    • D) var obj = new Object();
  24. Which method is used to add an item to the beginning of an array?
    • A) push()
    • B) pop()
    • C) shift()
    • D) unshift()
  25. How do you create a new array with elements added to an existing array?
    • A) arr.concat(newElements)
    • B) arr.add(newElements)
    • C) arr.merge(newElements)
    • D) arr.append(newElements)
  26. Which keyword is used to define a block scope variable in JavaScript?
    • A) var
    • B) let
    • C) const
    • D) block
  27. What is the result of Boolean(0) in JavaScript?
    • A) true
    • B) false
    • C) undefined
    • D) null
  28. Which method converts an object to a JSON string?
    • A) JSON.parse()
    • B) JSON.stringify()
    • C) toJSON()
    • D) convertToJSON()
  29. How do you execute a function every 5 seconds?
    • A) setTimeout(myFunction, 5000);
    • B) setInterval(myFunction, 5000);
    • C) setDelay(myFunction, 5000);
    • D) setExecute(myFunction, 5000);
  30. Which function is used to evaluate an expression in JavaScript?
    • A) eval()
    • B) calculate()
    • C) compute()
    • D) evaluate()
  31. What does the this keyword refer to in JavaScript?
    • A) The global object
    • B) The object that is currently executing the code
    • C) The function that is currently executing
    • D) The window object
  32. How do you check if a number is NaN?
    • A) isNaN()
    • B) checkNaN()
    • C) isNotNumber()
    • D) NaN()
  33. Which property of the window object provides information about the browser’s user agent?
    • A) window.location
    • B) window.navigator
    • C) window.screen
    • D) window.document
  34. What does JSON.stringify() do?
    • A) Converts a JavaScript object into a JSON string
    • B) Parses a JSON string into a JavaScript object
    • C) Evaluates a JSON string
    • D) Formats a JSON string
  35. Which of the following will correctly check if a variable is an array?
    • A) Array.isArray(variable)
    • B) variable.isArray()
    • C) typeof variable === 'array'
    • D) variable instanceof Array
  36. What is the result of 3 + '3' in JavaScript?
    • A) 6
    • B) 33
    • C) NaN
    • D) undefined
  37. Which statement will output the largest number in an array arr?
    • A) Math.max(arr)
    • B) arr.max()
    • C) Math.max(...arr)
    • D) arr.reduce(max)
  38. How can you stop a loop from executing further in JavaScript?
    • A) stop
    • B) halt
    • C) break
    • D) return
  39. Which method will remove the last item from an array?
    • A) pop()
    • B) shift()
    • C) push()
    • D) unshift()
  40. How do you access a property of an object in JavaScript?
    • A) object.property
    • B) object->property
    • C) object[property]
    • D) object.property()
  41. What will console.log([1] == [1]); output?
    • A) true
    • B) false
    • C) undefined
    • D) NaN
  42. Which operator is used to check for inequality?
    • A) !=
    • B) !==
    • C) <>
    • D) !=!
  43. How do you define a class in JavaScript?
    • A) class MyClass {}
    • B) define MyClass {}
    • C) function MyClass() {}
    • D) new class MyClass {}
  44. Which keyword is used to handle errors in JavaScript?
    • A) error
    • B) catch
    • C) try
    • D) throw
  45. What is the result of 10 % 3 in JavaScript?
    • A) 3
    • B) 1
    • C) 10
    • D) 0
  46. How do you create a new instance of a class in JavaScript?
    • A) new ClassName()
    • B) ClassName.new()
    • C) ClassName.create()
    • D) ClassName()
  47. Which function is used to read a value from a text input field?
    • A) getValue()
    • B) readValue()
    • C) value()
    • D) input.value
  48. How do you compare two objects in JavaScript?
    • A) ==
    • B) ===
    • C) JSON.stringify()
    • D) Object.equals()
  49. What is the result of null == undefined?
    • A) true
    • B) false
    • C) undefined
    • D) NaN
  50. How do you define a method within an object?
    • A) object.method = function() {}
    • B) object.method = () => {}
    • C) object.method() {}
    • D) object.method() = function() {}
  51. Which method adds one or more elements to the end of an array and returns the new length of the array?
    • A) push()
    • B) pop()
    • C) shift()
    • D) unshift()
  52. What will console.log('5' - 2); output?
    • A) 3
    • B) 52
    • C) NaN
    • D) undefined
  53. How do you check if a string contains a certain substring?
    • A) str.includes(substring)
    • B) str.has(substring)
    • C) str.contains(substring)
    • D) str.indexOf(substring)
  54. Which method is used to join all elements of an array into a string?
    • A) join()
    • B) combine()
    • C) concat()
    • D) merge()
  55. How do you create a promise in JavaScript?
    • A) new Promise()
    • B) Promise.create()
    • C) Promise.new()
    • D) new Promise(function(resolve, reject) {})
  56. What will console.log(1 < 2 < 3); output?
    • A) true
    • B) false
    • C) undefined
    • D) NaN
  57. How do you call a function that is stored in a variable?
    • A) variable()
    • B) call(variable)
    • C) variable.call()
    • D) variable.invoke()
  58. Which method removes whitespace from the start of a string?
    • A) trimStart()
    • B) trimLeft()
    • C) stripStart()
    • D) removeWhitespace()
  59. How do you set a timeout to execute a function after 2 seconds?
    • A) setTimeout(function, 2000)
    • B) delay(function, 2000)
    • C) setInterval(function, 2000)
    • D) schedule(function, 2000)
  60. Which of the following is used to create a new array in JavaScript?
    • A) Array.create()
    • B) new Array()
    • C) Array.new()
    • D) Array.build()
  61. How do you remove an element from a specific index in an array?
    • A) splice(index, 1)
    • B) remove(index)
    • C) delete(index)
    • D) slice(index, 1)
  62. Which method returns the index of the first occurrence of a specified value in a string?
    • A) indexOf()
    • B) find()
    • C) search()
    • D) positionOf()
  63. How do you get a substring from a string?
    • A) substring(start, end)
    • B) slice(start, end)
    • C) substr(start, length)
    • D) All of the above
  64. What will console.log('hello'.toUpperCase()); output?
    • A) HELLO
    • B) hello
    • C) Hello
    • D) HELLO
  65. How do you define a default parameter value in a function?
    • A) function myFunction(param = defaultValue) {}
    • B) function myFunction(param, defaultValue) {}
    • C) function myFunction(param, defaultValue = param) {}
    • D) function myFunction(param: defaultValue) {}
  66. What will console.log(2 + '2' - 1); output?
    • A) 21
    • B) 22
    • C) 20
    • D) 1
  67. Which method is used to sort the elements of an array?
    • A) sort()
    • B) order()
    • C) arrange()
    • D) sequence()
  68. How do you create a new RegExp object?
    • A) new RegExp(pattern)
    • B) RegExp.create(pattern)
    • C) RegExp(pattern)
    • D) new RegExp(pattern, flags)
  69. Which keyword is used to define a class method in JavaScript?
    • A) class
    • B) method
    • C) function
    • D) static
  70. How do you determine if an object is a function?
    • A) typeof object === 'function'
    • B) object.isFunction()
    • C) object instanceof Function
    • D) object.type()
  71. What does the filter() method do in JavaScript arrays?
    • A) Returns a new array with elements that pass a test
    • B) Sorts the array elements
    • C) Finds the first element that passes a test
    • D) Modifies the original array
  72. How can you delay the execution of a function by 2 seconds?
    • A) setTimeout(myFunction, 2000)
    • B) setDelay(myFunction, 2000)
    • C) schedule(myFunction, 2000)
    • D) delay(myFunction, 2000)
  73. Which method is used to find the largest value in an array of numbers?
    • A) Math.max()
    • B) arr.max()
    • C) arr.reduce(max)
    • D) arr.largest()
  74. How do you access an HTML element with a specific ID using JavaScript?
    • A) document.getElementById('id')
    • B) document.querySelector('#id')
    • C) document.select('#id')
    • D) document.getElement('id')
  75. What will console.log(typeof []); output?
    • A) array
    • B) object
    • C) list
    • D) undefined
  76. Which property of the navigator object is used to detect the browser version?
    • A) navigator.version
    • B) navigator.appVersion
    • C) navigator.userAgent
    • D) navigator.browserVersion
  77. What does localStorage provide in JavaScript?
    • A) Temporary storage of key-value pairs
    • B) Persistent storage of key-value pairs
    • C) Session-based storage of key-value pairs
    • D) Network-based storage
  78. How can you execute a function once after a delay in JavaScript?
    • A) setTimeout()
    • B) setInterval()
    • C) requestAnimationFrame()
    • D) queueMicrotask()
  79. Which method is used to merge two or more arrays into a single array?
    • A) concat()
    • B) merge()
    • C) combine()
    • D) join()
  80. How do you update the text content of an HTML element?
    • A) element.textContent = 'new text';
    • B) element.innerHTML = 'new text';
    • C) element.setText('new text');
    • D) element.updateText('new text');
  81. What does the catch block in a try-catch statement do?
    • A) Catches exceptions thrown in the try block
    • B) Executes code after the try block
    • C) Executes code regardless of success or failure
    • D) Initializes variables used in the try block
  82. Which method removes all elements from an array?
    • A) clear()
    • B) empty()
    • C) splice(0, arr.length)
    • D) delete()
  83. How do you define a variable that is immutable (cannot be changed)?
    • A) const
    • B) let
    • C) var
    • D) immutable
  84. Which operator is used to perform a bitwise AND operation in JavaScript?
    • A) &
    • B) &&
    • C) |
    • D) |
  85. How do you create a new date object in JavaScript?
    • A) new Date()
    • B) Date.create()
    • C) new DateTime()
    • D) Date.new()
  86. What does the toFixed() method do?
    • A) Formats a number to a specified number of decimal places
    • B) Converts a string to a number
    • C) Rounds a number to the nearest integer
    • D) Fixes the value of a number
  87. How do you create a new array from an existing one with some elements removed?
    • A) filter()
    • B) slice()
    • C) splice()
    • D) reduce()
  88. Which method is used to iterate over the properties of an object?
    • A) forEach()
    • B) map()
    • C) Object.keys()
    • D) Object.values()
  89. How do you check if an object has a specific property?
    • A) object.hasOwnProperty(property)
    • B) property in object
    • C) object.contains(property)
    • D) object.includes(property)
  90. What does Object.assign() do?
    • A) Copies properties from one or more objects to a target object
    • B) Merges multiple objects into one
    • C) Creates a new object with the properties of the source object
    • D) Deletes properties from an object
  91. Which method will return the first index of a value in an array?
    • A) indexOf()
    • B) findIndex()
    • C) search()
    • D) getIndex()
  92. What does the Array.from() method do?
    • A) Creates a new array from an array-like or iterable object
    • B) Converts an array into a string
    • C) Merges multiple arrays into one
    • D) Filters elements in an array
  93. How do you create a promise that resolves immediately with a value?
    • A) Promise.resolve(value)
    • B) new Promise.resolve(value)
    • C) Promise(value)
    • D) new Promise(value)
  94. Which method will add a new element to the end of an array and return the new length?
    • A) push()
    • B) pop()
    • C) shift()
    • D) unshift()
  95. How do you define a method inside a class in JavaScript?
    • A) method() {}
    • B) function method() {}
    • C) class.method() {}
    • D) static method() {}
  96. Which method will return a new array with elements that meet a condition?
    • A) filter()
    • B) map()
    • C) reduce()
    • D) forEach()
  97. How do you create an empty array in JavaScript?
    • A) var arr = [];
    • B) var arr = {};
    • C) var arr = () => {};
    • D) var arr = new Array();
  98. What does the toString() method do?
    • A) Converts a value to a string
    • B) Extracts a substring from a string
    • C) Formats a number as a string
    • D) Parses a string into an integer
  99. Which method is used to get the last element of an array?
    • A) pop()
    • B) slice(-1)
    • C) last()
    • D) peek()
  100. How do you check if a value is a number? – A) typeof value === 'number' – B) isNumber(value) – C) value.isNumber() – D) Number.isInteger(value)

 

Here are the answers to the JavaScript multiple-choice questions:

  1. A) Curly braces { }
  2. B) function myFunction() {}
  3. A) var variableName;
  4. C) =
  5. C) // This is a comment
  6. D) Character
  7. A) string
  8. A) trim()
  9. A) parseInt()
  10. B) Adds an element to the end
  11. B) new
  12. B) ===
  13. A) JSON.parse()
  14. B) false
  15. A) arr[0]
  16. C) break
  17. B) 11
  18. D) const variableName = value;
  19. D) length
  20. B) Returns a value from the function
  21. C) x instanceof Y
  22. C) undefined
  23. A) var obj = {};
  24. D) unshift()
  25. A) arr.concat(newElements)
  26. B) let
  27. B) false
  28. B) JSON.stringify()
  29. B) setInterval(myFunction, 5000);
  30. A) eval()
  31. B) The object that is currently executing the code
  32. A) isNaN()
  33. B) window.navigator
  34. A) Converts a JavaScript object into a JSON string
  35. A) Array.isArray(variable)
  36. B) 33
  37. C) Math.max(...arr)
  38. C) break
  39. A) pop()
  40. A) object.property
  41. B) false
  42. A) !=
  43. A) class MyClass {}
  44. D) throw
  45. B) 1
  46. A) new ClassName()
  47. D) input.value
  48. C) JSON.stringify()
  49. A) true
  50. A) object.method = function() {}
  51. A) push()
  52. A) 3
  53. A) str.includes(substring)
  54. A) join()
  55. D) new Promise(function(resolve, reject) {})
  56. B) false
  57. A) variable()
  58. B) trimLeft()
  59. A) setTimeout(myFunction, 2000)
  60. B) new Array()
  61. A) splice(index, 1)
  62. A) indexOf()
  63. D) All of the above
  64. A) HELLO
  65. A) function myFunction(param = defaultValue) {}
  66. C) 20
  67. A) sort()
  68. A) new RegExp(pattern)
  69. D) static
  70. A) typeof object === 'function'
  71. A) Returns a new array with elements that pass a test
  72. A) setTimeout(myFunction, 2000)
  73. A) Math.max()
  74. A) document.getElementById('id')
  75. B) object
  76. B) navigator.appVersion
  77. B) Persistent storage of key-value pairs
  78. A) setTimeout()
  79. A) concat()
  80. A) element.textContent = 'new text';
  81. A) Catches exceptions thrown in the try block
  82. C) splice(0, arr.length)
  83. A) const
  84. A) &
  85. A) new Date()
  86. A) Formats a number to a specified number of decimal places
  87. B) slice()
  88. C) Object.keys()
  89. A) object.hasOwnProperty(property)
  90. A) Copies properties from one or more objects to a target object
  91. A) indexOf()
  92. A) Creates a new array from an array-like or iterable object
  93. A) Promise.resolve(value)
  94. A) push()
  95. A) method() {}
  96. A) filter()
  97. A) var arr = [];
  98. A) Converts a value to a string
  99. A) pop()
  100. **A) typeof value === 'number'

techbloggerworld.com

Nagendra Kumar Sharma I Am Software engineer

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *