Javascript Multiple Choice Questions

Javascript Multiple Choice Questions

  1. Which of the following is NOT a valid data type in JavaScript? a) Number b) String c) Boolean d) Character

    Answer: d) Character

  2. What will be the output of the following code?

     var x = 5;
     console.log(x++);
    

    a) 5 b) 6 c) 4 d) Error Answer: a) 5

  3. What is the purpose of the use strict directive in JavaScript? a) It enables strict mode for the script. b) It allows using deprecated features. c) It is used to define global variables. d) It is a syntax error in JavaScript. Answer: a) It enables strict mode for the script.

  4. What is the difference between == and === in JavaScript? a) Both are the same. b) == checks value and type, === checks only value. c) == checks only value, === checks value and type. d) == is for strings, === is for numbers. Answer: c) == checks only value, === checks value and type.

  5. What is the output of the following code?

     var x = 10 + "5";
     console.log(x);
    

    a) 15 b) "105" c) 105 d) Error Answer: b) "105"

  6. Which method is used to add elements to the end of an array in JavaScript? a) push() b) add() c) append() d) insert() Answer: a) push()

  7. What does the NaN value represent in JavaScript? a) Not a Number b) Null and Not Defined c) Not Applicable Now d) None of the above Answer: a) Not a Number

  8. What is the purpose of the bind() method in JavaScript? a) To concatenate two strings b) To create a new array c) To attach an event handler function d) To set the value of this in a function Answer: d) To set the value of this in a function

  9. What will be the result of the following code?

     var x = 5;
     var y = "10";
     console.log(x + y);
    

    a) 15 b) 510 c) "510" d) Error Answer: c) "510"

  10. How do you check if a variable is an array in JavaScript? a) Using typeof b) Using isArray() c) Using type() d) Using isTypeOf() Answer: b) Using isArray()

  11. What is the output of the following code?

    var x = 10;
    function foo() {
      console.log(x);
      var x = 20;
    }
    foo();
    

    a) 10 b) 20 c) undefined d) Error Answer: c) undefined

  12. Which of the following is a correct way to create an object in JavaScript? a) var obj = object(); b) var obj = {} c) var obj = new Object(); d) All of the above Answer: d) All of the above

  13. What does the this keyword refer to in JavaScript? a) It refers to the current function's scope. b) It refers to the parent object. c) It refers to the global object. d) It depends on the context of its use. Answer: d) It depends on the context of its use.

  14. What is the output of the following code?

    console.log(2 + "2" - 2);
    

    a) 2 b) 22 c) 4 d) Error Answer: c) 4

  15. Which of the following is NOT a valid way to create a function in JavaScript? a) function myFunction() {} b) var myFunction = function() {}; c) var myFunction = () => {}; d) var myFunction = => {} Answer: d) var myFunction = => {}

  16. What is the purpose of the eval() function in JavaScript? a) To evaluate regular expressions b) To execute code in a string c) To evaluate mathematical expressions d) To evaluate object properties Answer: b) To execute code in a string

  17. What will be the result of the following code?

    var x = 0;
    function foo() {
      console.log(x);
      var x = 1;
    }
    foo();
    

    a) 0 b) 1 c) undefined d) Error Answer: c) undefined

  18. How do you remove the last element from an array in JavaScript? a) array.removeLast() b) array.pop() c) array.deleteLast() d) array.splice(-1) Answer: b) array.pop()

  19. What does the Array.prototype.map() method do in JavaScript? a) Adds an element to the end of the array b) Creates a new array by transforming each element of the original array c) Filters the array based on a condition d) Reverses the order of elements in the array Answer: b) Creates a new array by transforming each element of the original array

  20. What is the purpose of the setTimeout() function in JavaScript? a) To delay the execution of a function b) To execute a function repeatedly c) To set an interval for a function d) To stop the execution of a function Answer: a) To delay the execution of a function

  21. What is the result of the following code?

    var a = [1, 2, 3];
    var b = a;
    b.push(4);
    console.log(a);
    

    a) [1, 2, 3] b) [1, 2, 3, 4] c) [4, 1, 2, 3] d) Error Answer: b) [1, 2, 3, 4]

  22. What does the Array.prototype.filter() method do in JavaScript? a) Adds an element to the end of the array b) Creates a new array by transforming each element of the original array c) Filters the array based on a condition d) Reverses the order of elements in the array Answer: c) Filters the array based on a condition

  23. How do you check if an object has a specific property in JavaScript? a) Using obj.hasProperty(prop) b) Using obj.prop c) Using obj.hasOwnProperty(prop) d) Using obj.contains(prop) Answer: c) Using obj.hasOwnProperty(prop)

  24. What is the output of the following code?

    var x = 0;
    function foo() {
      x = 1;
    }
    foo();
    console.log(x);
    

    a) 0 b) 1 c) undefined d) Error Answer: b) 1

  25. What is the purpose of the Array.prototype.reduce() method in JavaScript? a) To add elements to the beginning of an array b) To create a new array by transforming each element of the original array c) To combine all elements of an array into a single value d) To sort the elements of an array Answer: c) To combine all elements of an array into a single value

  26. What is the result of the following code?

    console.log(typeof null);
    

    a) "null" b) "undefined" c) "object" d) "string" Answer: c) "object"

  27. What is the purpose of the Array.prototype.forEach() method in JavaScript? a) To create a new array by transforming each element of the original array b) To loop over each element of an array and execute a callback function c) To sort the elements of an array d) To filter the array based on a condition Answer: b) To loop over each element of an array and execute a callback function

  28. What will be the output of the following code?

    var x = 1;
    if (function foo() {}) {
      x += typeof foo;
    }
    console.log(x);
    

    a) 1undefined b) 1function c) 1string d) Error Answer: a) 1undefined

  29. How do you convert a string to a number in JavaScript? a) parseInt() b) Number() c) toNumber() d) convertNumber() Answer: b) Number()

  30. What does the Array.prototype.slice() method do in JavaScript? a) Removes elements from the beginning of an array b) Creates a shallow copy of an array from the specified start to end c) Adds elements to the beginning of an array d) Reverses the order of elements in the array Answer: b) Creates a shallow copy of an array from the specified start to end

  31. What is the output of the following code?

    var x = 10;
    var y = "5";
    console.log(x - y);
    

    a) 5 b) 10 c) 15 d) "55" Answer: a) 5

  32. What will be the result of the following code?

    console.log(typeof typeof 1);
    

    a) "number" b) "string" c) "object" d) "undefined" Answer: b) "string"

  33. What is the purpose of the Array.prototype.unshift() method in JavaScript? a) To remove elements from the beginning of an array b) To create a new array by transforming each element of the original array c) To add elements to the beginning of an array d) To filter the array based on a condition Answer: c) To add elements to the beginning of an array

  34. What is the output of the following code?

    var x = 1;
    function foo() {
      x = 10;
      return;
      function x() {}
    }
    foo();
    console.log(x);
    

    a) 1 b) 10 c) undefined d) Error Answer: a) 1

  35. What does the Array.prototype.join() method do in JavaScript? a) Adds elements to the end of an array b) Creates a new array by transforming each element of the original array c) Joins all elements of an array into a string using a specified separator d) Reverses the order of elements in the array Answer: c) Joins all elements of an array into a string using a specified separator

  36. How do you convert a number to a string in JavaScript? a) number.toString() b) String(number) c) number.toText() d) convertToString(number) Answer: b) String(number)

  37. What is the output of the following code?

    var x = 0;
    function foo() {
      x = 1;
      return x;
    }
    console.log(foo());
    

    a) 0 b) 1 c) undefined d) Error Answer: b) 1

  38. What is the purpose of the Array.prototype.some() method in JavaScript? a) To add elements to the end of an array b) To check if at least one element in the array satisfies a condition c) To create a new array by transforming each element of the original array d) To sort the elements of an array Answer: b) To check if at least one element in the array satisfies a condition

  39. What will be the result of the following code?

    var x = [1, 2, 3];
    x[10] = 10;
    console.log(x.length);
    

    a) 3 b) 4 c) 10 d) 11 Answer: d) 11

  40. What is the purpose of the Array.prototype.reverse() method in JavaScript? a) Adds elements to the end of an array b) Creates a new array by transforming each element of the original array c) Reverses the order of elements in the array d) Joins all elements of an array into a string using a specified separator Answer: c) Reverses the order of elements in the array

  41. What is the output of the following code?

    var x = 10;
    var y = 5;
    if (x > y) {
      var result = "x is greater than y";
    }
    console.log(result);
    

    a) "x is greater than y" b) 10 c) undefined d) Error Answer: a) "x is greater than y"

  42. What will be the result of the following code?

    var x = 5;
    console.log(x--);
    

    a) 5 b) 4 c) 6 d) Error Answer: a) 5

  43. What is the purpose of the `Array.prototype.find()` method in JavaScript? a) To check if at least one element in the array satisfies a condition b) To create a new array by transforming each element of the original array c) To find the index of a specific element in the array d) To find the first element in the array that satisfies a condition Answer: d) To find the first element in the array that satisfies a condition

  44. What does the Array.prototype.concat() method do in JavaScript? a) Adds elements to the end of an array b) Creates a new array by transforming each element of the original array c) Joins all elements of an array into a string using a specified separator d) Combines two or more arrays Answer: d) Combines two or more arrays

  45. What is the output of the following code?

    var x = [1, 2, 3];
    x.length = 0;
    console.log(x);
    

    a) [1, 2, 3] b) [] c) [null, null, null] d) Error Answer: b) []

  46. What will be the result of the following code?

    console.log("Hello" instanceof String);
    

    a) true b) false c) Error d) undefined Answer: b) false

  47. What is the purpose of the Array.prototype.splice() method in JavaScript? a) To create a new array by transforming each element of the original array b) To add elements to the end of an array c) To remove elements from an array and replace them with new elements d) To sort the elements of an array Answer: c) To remove elements from an array and replace them with new elements

  48. What is the output of the following code?

    var x = "5";
    var y = 2;
    console.log(x - y);
    

    a) 3 b) 7 c) "3" d) Error Answer: a) 3

  49. What does the Array.prototype.push() method do in JavaScript? a) Adds elements to the beginning of an array b) Creates a new array by transforming each element of the original array c) Adds elements to the end of an array d) Filters the array based on a condition Answer: c) Adds elements to the end of an array

  50. What is the purpose of the Array.prototype.every() method in JavaScript? a) To check if at least one element in the array satisfies a condition b) To create a new array by transforming each element of the original array c) To check if all elements in the array satisfy a condition d) To join all elements of an array into a string using a specified separator Answer: c) To check if all elements in the array satisfy a condition

  51. What will be the output of the following code?

    function foo() {
      return bar();
      function bar() {
        return "Hello";
      }
    }
    console.log(foo());
    

    a) "Hello" b) undefined c) Error d) "foo" Answer: a) "Hello"

  52. What is the result of the following code?

    var x = 10;
    var y = "10";
    console.log(x === y);
    

    a) true b) false c) "true" d) Error Answer: b) false

  53. What is the purpose of the Array.prototype.flat() method in JavaScript? a) To create a new array by transforming each element of the original array b) To flatten nested arrays into a single array c) To sort the elements of an array d) To join all elements of an array into a string using a specified separator Answer: b) To flatten nested arrays into a single array

  54. What is the output of the following code?

    console.log(typeof [1, 2]);
    

    a) "array" b) "object" c) "array,object" d) Error Answer: b) "object"

  55. How do you check if a function is a constructor in JavaScript? a) Using function.constructor b) Using function() instanceof Constructor c) Using function() instanceof Function d) Using function.prototype.constructor Answer: c) Using function() instanceof Function

  56. What is the purpose of the Object.prototype.hasOwnProperty() method in JavaScript? a) To check if an object has a specific property b) To add a property to an object c) To create a new object by transforming the original object d) To check if an object is a prototype of another object Answer: a) To check if an object has a specific property

  57. What will be the result of the following code?

    var x = 1;
    var y = 2;
    function foo() {
      console.log(x);
      var x = 10;
      console.log(y);
    }
    foo();
    

    a) undefined, undefined b) 1, 2 c) undefined, 2 d) Error Answer: a) undefined, undefined

  58. What is the purpose of the Object.prototype.toString() method in JavaScript? a) To convert an object to a string b) To check the constructor of an object c) To display the object's properties and values d) To get the string representation of an object Answer: d) To get the string representation of an object

  59. What does the Array.prototype.reduceRight() method do in JavaScript? a) Adds elements to the end of an array b) Creates a new array by transforming each element of the original array c) Combines all elements of an array into a single value, starting from the right d) Reverses the order of elements in the array Answer: c) Combines all elements of an array into a single value, starting from the right

  60. What is the output of the following code?

    function foo() {
      return this;
    }
    console.log(foo());
    

    a) null b) undefined c) Window object (in a browser environment) d) Error Answer: c) Window object (in a browser environment)

  61. How do you declare a private variable in JavaScript? a) this.privateVar = 10; b) var privateVar = 10; c) const privateVar = 10; d) let privateVar = 10; Answer: b) var privateVar = 10;

  62. What is the output of the following code?

    var x = 1;
    function foo() {
      console.log(x);
      var x = 10;
      console.log(x);
    }
    foo();
    

    a) undefined, 10 b) 1, 10 c) 1, undefined d) Error Answer: a) undefined, 10

  63. What is the purpose of the Array.prototype.indexOf() method in JavaScript? a) To check if an element exists in an array b) To find the index of the first occurrence of a specified element in an array c) To filter the array based on a condition d) To sort the elements of an array Answer: b) To find the index of the first occurrence of a specified element in an array

  64. What will be the result of the following code?

    var x = 10;
    function foo() {
      x = 20;
      return;
      function x() {}
    }
    foo();
    console.log(x);
    

    a) 10 b) 20 c) undefined d) Error Answer: a) 10

  65. What is the purpose of the Object.prototype.isPrototypeOf() method in JavaScript? a) To check if an object is a prototype of another object b) To convert an object to a string c) To display the object's properties and values d) To get the string representation of an object Answer: a) To check if an object is a prototype of another object

  66. What is the output of the following code?

    var x = 1;
    if (true) {
      var x = 2;
    }
    console.log(x);
    

    a) 1 b) 2 c) undefined d) Error Answer: b) 2

  67. What is the purpose of the Array.prototype.every() method in JavaScript? a) To add elements to the beginning of an array b) To check if at least one element in the array satisfies a condition c) To join all elements of an array into a string using a specified separator d) To check if all elements in the array satisfy a condition Answer: d) To check if all elements in the array satisfy a condition

  68. What will be the result of the following code?

    var x = [1, 2, 3];
    x[5] = 5;
    console.log(x.length);
    

    a) 3 b) 4 c) 6 d) 5 Answer: c) 6

  69. What is the output of the following code?

    var x = [1, 2, 3];
    console.log(x.toString());
    

    a) "1,2,3" b) [1, 2, 3] c) "1 2 3" d) Error Answer: a) "1,2,3"

  70. How do you create a copy of an object in JavaScript? a) var newObj = Object.create(obj); b) var newObj = obj; c) var newObj = Object.assign({}, obj); d) var newObj = obj.clone(); Answer: c) var newObj = Object.assign({}, obj);

  71. What is the output of the following code?

    var x = 1;
    var y = 2;
    if (x < y) {
      var result = function() {
        return x;
      }
    }
    console.log(result());
    

    a) 1 b) 2 c) undefined d) Error Answer: a) 1

  72. What is the purpose of the Array.prototype.slice() method in JavaScript? a) To remove elements from the beginning of an array b) To create a new array by transforming each element of the original array c) To add elements to the beginning of an array d) To create a shallow copy of an array from the specified start to end Answer: d) To create a shallow copy of an array from the specified start to end

  73. What does the Function.prototype.bind() method do in JavaScript? a) To concatenate two strings b) To create a new array by transforming each element of the original array c) To attach an event handler function d) To set the value of this in a function permanently Answer: d) To set the value of this in a function permanently

  74. What will be the result of the following code?

    var x = 1;
    function foo() {
      var x = 2;
      return x;
    }
    console.log(foo());
    

    a) 1 b) 2 c) undefined d) Error Answer: b) 2

  75. What is the output of the following code?

    var x = [1, 2, 3];
    console.log(x[6]);
    

    a) undefined b) null c) Error d) 6 Answer: a) undefined

  76. How do you check if an object is empty in JavaScript? a) Using Object.empty(obj) b) Using Object.keys(obj).length === 0 c) Using Object.isEmpty(obj) d) Using obj.isEmpty() Answer: b) Using Object.keys(obj).length === 0

  77. What is the output of the following code?

    console.log(2 + 2 - "2");
    

    a) 2 b) 0 c) 4 d) Error Answer: c) 4

  78. What is the purpose of the Array.prototype.flatMap() method in JavaScript? a) To create a new array by transforming each element of the original array b) To flatten nested arrays into a single array c) To sort the elements of an array d) To combine all elements of an array into a single value Answer: a) To create a new array by transforming each element of the original array

  79. What will be the result of the following code?

    var x = 5;
    var y = "5";
    console.log(x + y);
    

    a) 10 b) "55" c) "5" d) Error Answer: b) "55"

  80. What is the output of the following code?

    function foo() {
      console.log(this);
    }
    foo.call(null);
    

    a) null b) undefined c) Window object (in a browser environment) d) Error Answer: c) Window object (in a browser environment)

  81. What does the Array.prototype.copyWithin() method do in JavaScript? a) Adds elements to the end of an array b) Creates a new array by transforming each element of the original array c) Copies elements within an array to a specified position d) Reverses the order of elements in the array Answer: c) Copies elements within an array to a specified position

  82. What is the output of the following code?

    var x = 1;
    var y = 2;
    function foo() {
      console.log(y);
      var y = 3;
      console.log(y);
    }
    foo();
    

    a) undefined, 3 b) 2, 3 c) 2, undefined d) Error Answer: a) undefined, 3

  83. What is the purpose of the Array.prototype.lastIndexOf() method in JavaScript? a) To check if an element exists in an array b) To find the index of the last occurrence of a specified element in an array c) To filter the array based on a condition d) To sort the elements of an array Answer: b) To find the index of the last occurrence of a specified element in an array

  84. What will be the result of the following code?

    var x = [1, 2, 3];
    var y = x.map(function(a) {
      return a + 1;
    });
    console.log(y);
    

    a) [2, 3, 4] b) [1, 2, 3] c) [1, 2, 3, 4] d) Error Answer: a) [2, 3, 4]

  85. What is the output of the following code?

    var x = [1, 2, 3];
    x.length = 0;
    console.log(x);
    

    a) [1, 2, 3] b) [] c) [null, null, null] d) Error Answer: b) []

  86. What does the Array.prototype.forEach() method do in JavaScript? a) Adds elements to the end of an array b) Creates a new array by transforming each element of the original array c) Loops over each element of an array and executes a callback function d) Filters the array based on a condition Answer: c) Loops over each element of an array and executes a callback function

  87. What will be the result of the following code?

    var x = [1, 2, 3];
    console.log(x instanceof Array);
    

    a) true b) false c) Error d) undefined Answer: a) true

  88. What is the purpose of the Array.prototype.reverse() method in JavaScript? a) Adds elements to the end of an array b) Creates a new array by transforming each element of the original array c) Reverses the order of elements in the array d) Joins all elements of an array into a string using a specified separator Answer: c) Reverses the order of elements in the array

  89. What is the output of the following code?

    console.log(10 + 20 + "30");
    

    a) "3030" b) "6030" c) "103020" d) Error Answer: a) "3030"

  90. How do you check if an object is an instance of a specific class in JavaScript? a) Using object instanceof Class b) Using object.isInstance(Class) c) Using object.prototype.instanceOf(Class) d) Using object.instance(Class) Answer: a) Using object instanceof Class

  91. What is the purpose of the Array.prototype.some() method in JavaScript? a) To add elements to the beginning of an array b) To check if at least one element in the array satisfies a condition c) To join all elements of an array into a string using a specified separator d) To check if some elements in the array satisfy a condition Answer: d) To check if some elements in the array satisfy a condition

  92. What will be the result of the following code?

    var x = [1, 2, 3];
    x.shift();
    console.log(x);
    

    a) [1, 2, 3] b) [2, 3] c) [2] d) Error Answer: b) [2, 3]

  93. What is the purpose of the Array.prototype.sort() method in JavaScript? a) To create a new array by transforming each element of the original array b) To sort the elements of an array c) To add elements to the end of an array d) To filter the array based on a condition Answer: b) To sort the elements of an array

  94. What is the output of the following code?

    var x = [1, 2, 3];
    x.push(4);
    console.log(x);
    

    a) [1, 2, 3] b) [1, 2, 3, 4] c) [4, 1, 2, 3] d) Error Answer: b) [1, 2, 3, 4]

  95. How do you check if an object is an array in JavaScript? a) Using typeof b) Using isArray() c) Using type() d) Using isTypeOf() Answer: b) Using isArray()

  96. What is the output of the following code?

    var x = 10;
    var y = "10";
    console.log(x === y);
    

    a) true b) false c) "true" d) Error Answer: b) false

  97. What is the purpose of the Object.prototype.hasOwnProperty() method in JavaScript? a) To check if an object has a specific property b) To add a property to an object c) To create a new object by transforming the original object d) To check if an object is a prototype of another object Answer: a) To check if an object has a specific property

  98. What will be the result of the following code?

    var x = 1;
    function foo() {
      console.log(x);
      var x = 10;
      console.log(x);
    }
    foo();
    

    a) undefined, 10 b) 1, 10 c) 1, undefined d) Error Answer: a) undefined, 10

  99. What is the purpose of the Object.prototype.toString() method in JavaScript? a) To convert an object to a string b) To check the constructor of an object c) To display the object's properties and values d) To get the string representation of an object Answer: d) To get the string representation of an object

  100. What does the Array.prototype.flatMap() method do in JavaScript? a) To create a new array by transforming each element of the original array b) To flatten nested arrays into a single array c) To sort the elements of an array d) To combine all elements of an array into a single value Answer: a) To create a new array by transforming each element of the original array