JavaScript Strings MCQs

1 min read

Strings in JavaScript represent sequences of UTF-16 code units used to store and manipulate textual data. Declared using single quotes, double quotes, or ES6 template literals (backticks), strings are primitive values and strictly immutable meaning methods do not mutate the original string in place but instead return a new string reference. JavaScript provides a rich set of built-in prototype methods for searching (indexOf, includes), extracting (slice, substring), modifying (replace, replaceAll, toLowerCase), and converting (split). Understanding indexing, string immutability, UTF-16 encoding details, and regex integration is vital for manipulating user inputs and passing technical code evaluations.

1. What is the output of `console.log(typeof [1, 2, 3])`?

a) array
b) object
c) list
d) undefined
Correct Answer: b) object
Explanation:
In JavaScript, arrays are non-primitive object types. The typeof operator returns 'object' for array instances.

2. Which method determines whether a passed value is a true Array instance?

a) Array.isArray()
b) Object.isArray()
c) Array.prototype.isInstance()
d) typeof Array
Correct Answer: a) Array.isArray()
Explanation:
Array.isArray() is the standard reliable static method used to verify if a variable is an Array.

3. What is the output of `console.log([1, 2, 3] + [4, 5, 6])`?

a) [1, 2, 3, 4, 5, 6]
b) 1,2,34,5,6
c) NaN
d) TypeError
Correct Answer: b) 1,2,34,5,6
Explanation:
Using the '+' operator on arrays coerces both arrays to strings ('1,2,3' and '4,5,6') and concatenates them into '1,2,34,5,6'.

4. What will `console.log([] == ![])` evaluate to?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
![] coerces to boolean false. Loose equality [] == false coerces [] to '' (0) and false to 0, yielding 0 == 0 which is true.

5. What will `console.log([] === ![])` evaluate to?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: b) false
Explanation:
Strict equality checks both type and reference. An empty array reference is not strictly equal to the primitive boolean false.

6. What is the output of `console.log(0.1 + 0.2 === 0.3)` in JavaScript?

a) true
b) false
c) TypeError
d) NaN
Correct Answer: b) false
Explanation:
Floating-point precision issues cause 0.1 + 0.2 to evaluate to 0.30000000000000004, which does not strictly equal 0.3.

7. Which array method mutates an array by removing the first element?

a) pop()
b) shift()
c) unshift()
d) slice()
Correct Answer: b) shift()
Explanation:
shift() removes the first element from an array and shifts all subsequent elements down by one index.

8. Which array method adds one or more elements to the beginning of an array?

a) push()
b) unshift()
c) prepend()
d) splice()
Correct Answer: b) unshift()
Explanation:
unshift() adds new items to the start of an array and returns the updated array length.

9. What is returned by `[1, 2, 3].push(4)`?

a) [1, 2, 3, 4]
b) 4
c) 3
d) undefined
Correct Answer: b) 4
Explanation:
The push() method mutates the array and returns the new length of the modified array (which is 4).

10. What will `console.log([1, 2, 3].shift())` return?

a) 1
b) [2, 3]
c) 3
d) undefined
Correct Answer: a) 1
Explanation:
shift() removes and returns the first element of the array, which is 1.

11. What happens when `pop()` is called on an empty array `[]`?

a) null
b) undefined
c) 0
d) TypeError
Correct Answer: b) undefined
Explanation:
Calling pop() on an empty array leaves the array unchanged and returns undefined.

12. What is the output of `[1, 2, 3, 4].splice(1, 2)`?

a) [2, 3]
b) [1, 4]
c) [2]
d) [1, 2]
Correct Answer: a) [2, 3]
Explanation:
splice(start, deleteCount) removes deleteCount elements from start index. Starting at index 1, it removes 2 items: [2, 3].

13. What is the value of original array `arr` after executing `let arr = [1, 2, 3, 4]; arr.splice(1, 2);`?

a) [1, 4]
b) [2, 3]
c) [1, 2, 3, 4]
d) [1]
Correct Answer: a) [1, 4]
Explanation:
splice() mutates the target array in place. Removing index 1 and 2 leaves [1, 4].

14. How does `slice()` differ from `splice()`?

a) slice() mutates the original array; splice() returns a shallow copy.
b) slice() returns a shallow copy without mutating the original array; splice() mutates the original array.
c) slice() works only on objects; splice() works on arrays.
d) There is no functional difference.
Correct Answer: b) slice() returns a shallow copy without mutating the original array; splice() mutates the original array.
Explanation:
slice() is non-mutating and extracts a section into a new array. splice() modifies the existing array in place.

15. What will `[10, 20, 30, 40].slice(-2)` return?

a) [30, 40]
b) [10, 20]
c) [20, 30]
d) [40]
Correct Answer: a) [30, 40]
Explanation:
Negative start indices in slice() count back from the end of the array. Index -2 extracts the last 2 elements: [30, 40].

16. What is the default sorting behavior of `Array.prototype.sort()` without a comparator function?

a) Sorts elements numerically in ascending order.
b) Sorts elements as strings according to UTF-16 code units.
c) Sorts elements randomly.
d) Leaves the array unchanged.
Correct Answer: b) Sorts elements as strings according to UTF-16 code units.
Explanation:
Without a comparator, sort() converts elements to strings and compares their UTF-16 code unit sequences.

17. What is the output of `[1, 10, 2, 25].sort()`?

a) [1, 2, 10, 25]
b) [1, 10, 2, 25]
c) [25, 10, 2, 1]
d) [2, 1, 25, 10]
Correct Answer: b) [1, 10, 2, 25]
Explanation:
Default lexicographical string sorting compares '10' before '2' because string character '1' comes before '2'.

18. How do you correctly sort an array of numbers `[1, 10, 2, 25]` in ascending numerical order?

a) arr.sort((a, b) => a - b)
b) arr.sort((a, b) => b - a)
c) arr.sort(true)
d) arr.sortNumbers()
Correct Answer: a) arr.sort((a, b) => a - b)
Explanation:
Providing comparator `(a, b) => a - b` returns negative when a < b, correctly ordering numbers numerically.

19. Which method reverses an array in place without returning a new array reference?

a) toReversed()
b) reverse()
c) invert()
d) flip()
Correct Answer: b) reverse()
Explanation:
reverse() mutates the array in place and returns the reference to the same inverted array.

20. Which non-mutating ES2023 method returns a new array with elements in reversed order?

a) toReversed()
b) reverse()
c) sliceReversed()
d) copyReverse()
Correct Answer: a) toReversed()
Explanation:
ES2023 introduced toReversed() to return a reversed copy of an array without mutating the original.

21. Which ES2023 method returns a sorted copy of an array without mutating the original array?

a) toSorted()
b) sortCopy()
c) sort()
d) sliceSort()
Correct Answer: a) toSorted()
Explanation:
toSorted() returns a new sorted copy of an array, keeping the original array intact.

22. What is the output of `[1, 2, 3].concat([4, 5])`?

a) [1, 2, 3, 4, 5]
b) [[1, 2, 3], [4, 5]]
c) [1, 2, 3, [4, 5]]
d) 1,2,3,4,5
Correct Answer: a) [1, 2, 3, 4, 5]
Explanation:
concat() merges two or more arrays into a single new array.

23. What does `[1, [2, [3]]].flat(1)` evaluate to?

a) [1, 2, [3]]
b) [1, 2, 3]
c) [1, [2, [3]]]
d) [[1, 2, 3]]
Correct Answer: a) [1, 2, [3]]
Explanation:
flat(depth) flattens sub-array structures up to specified depth. Depth 1 flattens outer array to [1, 2, [3]].

24. What does `[1, [2, [3]]].flat(Infinity)` evaluate to?

a) [1, 2, 3]
b) [1, 2, [3]]
c) [1, [2, 3]]
d) TypeError
Correct Answer: a) [1, 2, 3]
Explanation:
Passing Infinity to flat() recursively flattens nested arrays of any arbitrary depth.

25. What will `console.log([1, 2, 3].indexOf(4))` return?

a) -1
b) false
c) null
d) undefined
Correct Answer: a) -1
Explanation:
indexOf() returns the first index at which a given element is found, or -1 if it is not present.

26. What will `console.log([1, 2, NaN].includes(NaN))` return?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
Unlike indexOf() (which uses strict equality and fails on NaN), includes() uses SameValueZero algorithm and correctly finds NaN.

27. What will `console.log([1, 2, NaN].indexOf(NaN))` return?

a) -1
b) 2
c) true
d) false
Correct Answer: a) -1
Explanation:
indexOf uses strict comparison (===). Since NaN === NaN is false, indexOf fails to locate NaN and returns -1.

28. What does `[10, 20, 30].find(x => x > 15)` return?

a) 20
b) [20, 30]
c) 1
d) true
Correct Answer: a) 20
Explanation:
find() returns the value of the FIRST element in the array that satisfies the provided testing function.

29. What does `[10, 20, 30].findIndex(x => x > 15)` return?

a) 1
b) 20
c) 2
d) 0
Correct Answer: a) 1
Explanation:
findIndex() returns the index position of the FIRST element that passes the predicate function. Value 20 is at index 1.

30. What does `[10, 20, 30].findLast(x => x > 15)` return in ES2023?

a) 30
b) 20
c) 2
d) [20, 30]
Correct Answer: a) 30
Explanation:
findLast() iterates the array in reverse order and returns the value of the first matching element (30).

31. What does `[1, 2, 3].every(x => x > 0)` evaluate to?

a) true
b) false
c) [1, 2, 3]
d) 1
Correct Answer: a) true
Explanation:
every() checks if ALL elements in the array pass the test condition. Since 1, 2, 3 are all > 0, it returns true.

32. What does `[1, 2, 3].some(x => x > 2)` evaluate to?

a) true
b) false
c) 3
d) [3]
Correct Answer: a) true
Explanation:
some() checks if AT LEAST ONE element in the array passes the condition. Element 3 is > 2, so it returns true.

33. What will `[].every(x => x > 0)` return?

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
every() called on an empty array returns true for any condition (vacuous truth).

34. What will `[].some(x => x > 0)` return?

a) false
b) true
c) undefined
d) TypeError
Correct Answer: a) false
Explanation:
some() called on an empty array returns false for any condition.

35. What is the output of `[1, 2, 3].join("-")`?

a) "1-2-3"
b) ["1-2-3"]
c) "123"
d) 6
Correct Answer: a) "1-2-3"
Explanation:
join(separator) concatenates all array elements into a string separated by the specified separator string.

36. What is the default separator for `Array.prototype.join()` if no argument is passed?

a) "," (comma)
b) " " (space)
c) "" (empty string)
d) "-" (dash)
Correct Answer: a) "," (comma)
Explanation:
If no separator is provided, join() defaults to using a comma (",").

37. What is the output of `[1, 2, 3].reduce((acc, x) => acc * x, 1)`?

a) 6
b) 0
c) 123
d) [1, 2, 6]
Correct Answer: a) 6
Explanation:
reduce accumulates products: initial 1 * 1 = 1; * 2 = 2; * 3 = 6.

38. What happens if `reduce()` is called on an empty array without providing an initial value?

a) TypeError: Reduce of empty array with no initial value
b) undefined
c) null
d) 0
Correct Answer: a) TypeError: Reduce of empty array with no initial value
Explanation:
Calling reduce() on an empty array without an initial value throws a TypeError.

39. What does `[1, 2, 3].reduceRight((acc, val) => acc + val, "")` evaluate to?

a) "321"
b) "123"
c) 6
d) TypeError
Correct Answer: a) "321"
Explanation:
reduceRight processes array elements from right to left (last element to first), producing "321".

40. What is the output of `console.log(Array.from("foo"))`?

a) ["f", "o", "o"]
b) ["foo"]
c) foo
d) TypeError
Correct Answer: a) ["f", "o", "o"]
Explanation:
Array.from creates a new array instance from an iterable object, splitting string 'foo' into characters.

41. What does `Array.from([1, 2, 3], x => x * 2)` return?

a) [2, 4, 6]
b) [1, 2, 3]
c) 6
d) undefined
Correct Answer: a) [2, 4, 6]
Explanation:
Array.from accepts an optional map function as its second argument to transform elements during array creation.

42. What is the output of `Array.of(7)` vs `new Array(7)`?

a) Array.of(7) yields [7]; new Array(7) yields array with 7 empty slots.
b) Array.of(7) yields 7 empty slots; new Array(7) yields [7].
c) Both yield [7].
d) Both yield array of length 7.
Correct Answer: a) Array.of(7) yields [7]; new Array(7) yields array with 7 empty slots.
Explanation:
Array.of(7) creates array containing item [7]. single numeric argument to Array constructor creates empty array of length 7.

43. What is a sparse array in JavaScript?

a) An array in which elements are non-contiguous and contain empty slots (holes).
b) An array with zero elements.
c) An array containing only null values.
d) An frozen array.
Correct Answer: a) An array in which elements are non-contiguous and contain empty slots (holes).
Explanation:
Sparse arrays contain empty slots (indices with no values assigned), where array length exceeds actual element count.

44. What is the length of `let a = []; a[10] = "test";`?

a) 11
b) 10
c) 1
d) 0
Correct Answer: a) 11
Explanation:
Assigning to index 10 sets length to index + 1 = 11, creating indices 0-9 as empty slots.

45. What is the output of `console.log(Object.keys([, , ])) ` for a sparse array?

a) []
b) ["0", "1"]
c) [undefined, undefined]
d) TypeError
Correct Answer: a) []
Explanation:
Object.keys ignores sparse array empty slots (holes) and returns an empty array.

46. How does `forEach` treat empty slots (holes) in sparse arrays?

a) It skips unassigned empty slots entirely.
b) It executes the callback with undefined.
c) It throws a TypeError.
d) It stops iteration.
Correct Answer: a) It skips unassigned empty slots entirely.
Explanation:
Built-in array iteration methods like forEach, map, filter skip unassigned indices in sparse arrays.

47. How does `for...of` handle empty slots (holes) in sparse arrays?

a) It iterates over empty slots yielding `undefined` for each.
b) It skips empty slots.
c) It throws a TypeError.
d) It exits the loop.
Correct Answer: a) It iterates over empty slots yielding `undefined` for each.
Explanation:
The for...of loop relies on array iterator, treating empty holes as elements with value `undefined`.

48. What is the output of `let arr = [1, 2, 3]; arr.length = 1; console.log(arr);`?

a) [1]
b) [1, 2, 3]
c) TypeError
d) [undefined]
Correct Answer: a) [1]
Explanation:
Setting array length property to a smaller number truncates the array in place, deleting trailing elements.

49. What is the output of `let arr = [1, 2]; arr.length = 0; console.log(arr);`?

a) []
b) [1, 2]
c) undefined
d) TypeError
Correct Answer: a) []
Explanation:
Setting length to 0 completely clears all elements from the array in place.

50. What happens if you set array length property to a negative number `arr.length = -1`?

a) RangeError: Invalid array length
b) Array becomes empty
c) TypeError
d) Length is ignored
Correct Answer: a) RangeError: Invalid array length
Explanation:
Setting array length to a negative number or non-integer throws a RangeError.

51. What will `console.log(delete [10, 20, 30][1])` do?

a) Deletes value at index 1 leaving an empty slot hole; array length remains 3.
b) Removes index 1 and shrinks array length to 2.
c) Throws TypeError.
d) Sets index 1 to 0.
Correct Answer: a) Deletes value at index 1 leaving an empty slot hole; array length remains 3.
Explanation:
Deleting an array element via delete operator removes property value, creating a sparse slot without modifying array length.

52. What will `console.log(1 in [10, 20, 30])` return?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
The 'in' operator checks if index key 1 exists as a property in the array object, returning true.

53. What will `console.log(3 in [10, 20, 30])` return?

a) false
b) true
c) TypeError
d) undefined
Correct Answer: a) false
Explanation:
Index 3 does not exist in an array of length 3 (indices 0, 1, 2), returning false.

54. What does `[1, 2, 3].fill(0, 1, 3)` return?

a) [1, 0, 0]
b) [0, 0, 0]
c) [1, 0, 3]
d) [0, 2, 3]
Correct Answer: a) [1, 0, 0]
Explanation:
fill(value, start, end) changes array elements to static value from start index 1 up to end index 3.

55. What does `[1, 2, 3, 4, 5].copyWithin(0, 3, 5)` return?

a) [4, 5, 3, 4, 5]
b) [1, 2, 3, 4, 5]
c) [4, 5, 1, 2, 3]
d) [3, 4, 5, 4, 5]
Correct Answer: a) [4, 5, 3, 4, 5]
Explanation:
copyWithin(target, start, end) copies elements from index 3 to 5 ([4, 5]) to target position index 0.

56. Which of the following creates an object that inherits directly from target prototype object `proto`?

a) Object.create(proto)
b) Object.assign({}, proto)
c) new Object(proto)
d) Object.clone(proto)
Correct Answer: a) Object.create(proto)
Explanation:
Object.create(proto) creates a new object using the existing object passed as prototype.

57. How do you create a clean dictionary object in JavaScript with NO prototype chain (no prototype properties)?

a) Object.create(null)
b) Object.create({})
c) new Object(null)
d) { prototype: null }
Correct Answer: a) Object.create(null)
Explanation:
Object.create(null) creates an object with a null prototype chain, free from standard Object.prototype methods.

58. What is the output of `console.log(Object.create(null).toString)`?

a) undefined
b) [function toString]
c) TypeError
d) null
Correct Answer: a) undefined
Explanation:
Since Object.create(null) has no prototype, standard Object methods like toString are undefined.

59. What method copies all enumerable own properties from one or more source objects to a target object?

a) Object.assign()
b) Object.copy()
c) Object.extend()
d) Object.append()
Correct Answer: a) Object.assign()
Explanation:
Object.assign(target, ...sources) performs shallow copies of own enumerable properties into target.

60. What type of copy does `Object.assign()` perform?

a) Shallow copy
b) Deep copy
c) Immutable copy
d) Recursive copy
Correct Answer: a) Shallow copy
Explanation:
Object.assign() only copies property references at the top level (shallow copy).

61. Which modern global method performs a deep copy of an object or array containing serializable data?

a) structuredClone()
b) Object.deepClone()
c) JSON.clone()
d) Object.assignDeep()
Correct Answer: a) structuredClone()
Explanation:
structuredClone() is a standard built-in global function for performing deep copies of data structures.

62. What happens when trying to deep clone an object containing circular references using `JSON.parse(JSON.stringify(obj))`?

a) TypeError: Converting circular structure to JSON
b) Clones successfully
c) Returns null
d) Infinite loop hanging process
Correct Answer: a) TypeError: Converting circular structure to JSON
Explanation:
JSON.stringify throws a TypeError if it encounters objects with circular references.

63. Can `structuredClone()` handle circular references?

a) Yes, structuredClone handles circular references automatically.
b) No, it throws a TypeError.
c) Only if configured with options.
d) It converts circular references to null.
Correct Answer: a) Yes, structuredClone handles circular references automatically.
Explanation:
The structured clone algorithm natively supports structures containing circular references.

64. What method returns an array of an object's own enumerable string-keyed property values?

a) Object.values()
b) Object.keys()
c) Object.entries()
d) Object.properties()
Correct Answer: a) Object.values()
Explanation:
Object.values(obj) extracts property values of own enumerable string properties into an array.

65. What method converts an array of key-value pairs `[ [key, value] ]` back into an object?

a) Object.fromEntries()
b) Object.toObject()
c) Object.parse()
d) Object.assign()
Correct Answer: a) Object.fromEntries()
Explanation:
Object.fromEntries() performs inverse operation of Object.entries(), building object from key-value pairs.

66. What does `Object.hasOwn(obj, prop)` do in ES2022?

a) Returns true if specified object has indicated property as its own property.
b) Returns true if property exists anywhere in prototype chain.
c) Checks if property is frozen.
d) Checks if property is non-enumerable.
Correct Answer: a) Returns true if specified object has indicated property as its own property.
Explanation:
Object.hasOwn() is the recommended ES2022 replacement for obj.hasOwnProperty().

67. Why is `Object.hasOwn(obj, prop)` preferred over `obj.hasOwnProperty(prop)`?

a) It works reliably on objects created with Object.create(null) and overridden properties.
b) It is faster.
c) It checks prototype properties.
d) It works asynchronously.
Correct Answer: a) It works reliably on objects created with Object.create(null) and overridden properties.
Explanation:
obj.hasOwnProperty fails if object prototype is null or hasOwnProperty is overridden; Object.hasOwn static method avoids this.

68. What is the result of `let obj = { a: 1 }; delete obj.a; console.log(obj.a);`?

a) undefined
b) 1
c) null
d) TypeError
Correct Answer: a) undefined
Explanation:
The delete operator removes the property from object, making subsequent lookup return undefined.

69. What does `Object.preventExtensions(obj)` do?

a) Prevents new properties from ever being added to an object.
b) Freezes existing properties from being modified.
c) Prevents deleting existing properties.
d) Hides object properties.
Correct Answer: a) Prevents new properties from ever being added to an object.
Explanation:
Object.preventExtensions() prevents addition of new properties, but existing properties can still be modified or deleted.

70. What method checks if an object is frozen by `Object.freeze()`?

a) Object.isFrozen()
b) Object.isSealed()
c) Object.checkFreeze()
d) Object.isLocked()
Correct Answer: a) Object.isFrozen()
Explanation:
Object.isFrozen(obj) returns boolean true if object is frozen.

71. What method defines a new property or modifies an existing property directly on an object with descriptor rules?

a) Object.defineProperty()
b) Object.setProperty()
c) Object.createProperty()
d) Object.addProperty()
Correct Answer: a) Object.defineProperty()
Explanation:
Object.defineProperty(obj, prop, descriptor) allows granular configuration of property attributes.

72. What are the default values for `enumerable`, `writable`, and `configurable` when adding property via `Object.defineProperty()`?

a) false
b) true
c) undefined
d) null
Correct Answer: a) false
Explanation:
Properties added via Object.defineProperty default descriptor flags (enumerable, writable, configurable) to false.

73. What occurs if you attempt to write to a non-writable property in strict mode (`"use strict"`)?

a) TypeError
b) Silent failure
c) SyntaxError
d) Warning in console
Correct Answer: a) TypeError
Explanation:
In strict mode, attempting to modify a non-writable property raises a TypeError.

74. What happens if a property has `enumerable: false` during a `for...in` loop?

a) The loop skips that property.
b) The loop includes that property.
c) The loop throws TypeError.
d) The loop terminates.
Correct Answer: a) The loop skips that property.
Explanation:
Non-enumerable properties are hidden from for...in loops and Object.keys().

75. Which method returns array of ALL own property names including non-enumerable properties?

a) Object.getOwnPropertyNames()
b) Object.keys()
c) Object.values()
d) Object.allProperties()
Correct Answer: a) Object.getOwnPropertyNames()
Explanation:
Object.getOwnPropertyNames() returns array of all string-keyed property names regardless of enumerability.

76. Which method returns array of ALL own Symbol properties of an object?

a) Object.getOwnPropertySymbols()
b) Object.getSymbols()
c) Object.keys()
d) Object.getOwnPropertyNames()
Correct Answer: a) Object.getOwnPropertySymbols()
Explanation:
Object.getOwnPropertySymbols(obj) retrieves array of Symbol keys defined on target object.

77. Which static method returns array of ALL own property keys including string keys (enumerable or not) AND Symbols?

a) Reflect.ownKeys()
b) Object.keys()
c) Object.allKeys()
d) Object.getOwnPropertyNames()
Correct Answer: a) Reflect.ownKeys()
Explanation:
Reflect.ownKeys(target) returns array containing all own property keys (string names + Symbols).

78. What will `let { a, b } = { a: 10, b: 20 }; console.log(a + b);` output?

a) 30
b) 1020
c) undefined
d) TypeError
Correct Answer: a) 30
Explanation:
Object destructuring extracts values of properties 'a' and 'b' into variables. 10 + 20 = 30.

79. How do you rename property `a` to local variable `x` during object destructuring `{ a: 10 }`?

a) let { a: x } = { a: 10 };
b) let { a as x } = { a: 10 };
c) let { x = a } = { a: 10 };
d) let { a => x } = { a: 10 };
Correct Answer: a) let { a: x } = { a: 10 };
Explanation:
The colon syntax `{ propertyName: localVariableName }` renames destructured properties.

80. What is the output of `let [a, , c] = [1, 2, 3]; console.log(a, c);`?

a) 1 3
b) 1 2
c) 2 3
d) 1 undefined
Correct Answer: a) 1 3
Explanation:
Elision (empty comma slot) in array destructuring skips index 1, assigning a=1 and c=3.

81. What is the output of `let [a = 5, b = 10] = [1]; console.log(a, b);`?

a) 1 10
b) 5 10
c) 1 undefined
d) 5 1
Correct Answer: a) 1 10
Explanation:
Default value for 'a' is ignored because value 1 exists. 'b' gets fallback default 10.

82. How can you swap two variables `x` and `y` using array destructuring?

a) [x, y] = [y, x];
b) swap(x, y);
c) x, y = y, x;
d) { x, y } = { y, x };
Correct Answer: a) [x, y] = [y, x];
Explanation:
Array destructuring assignment `[x, y] = [y, x]` swaps variable values cleanly without temporary variables.

83. What will `let [a, ...b] = [1, 2, 3, 4]; console.log(b);` output?

a) [2, 3, 4]
b) 2
c) [1, 2, 3, 4]
d) undefined
Correct Answer: a) [2, 3, 4]
Explanation:
Rest element `...b` collects remaining unallocated array elements into a new array.

84. Where must the rest element `...rest` be placed in array or object destructuring?

a) Must always be the last element in destructuring pattern.
b) Can be placed anywhere.
c) Must be the first element.
d) In the middle position.
Correct Answer: a) Must always be the last element in destructuring pattern.
Explanation:
Rest elements must appear last in destructuring parameter list, otherwise a SyntaxError occurs.

85. What is property shorthand syntax in object literals `let a = 1; let obj = { a };`?

a) Shorthand for { a: a }
b) Shorthand for { a: true }
c) Shorthand for { 'a': 1 }
d) SyntaxError
Correct Answer: a) Shorthand for { a: a }
Explanation:
ES6 object property shorthand allows omitting property value if matching variable name is in scope.

86. What is computed property name syntax in object literals `{ [key]: "value" }`?

a) Evaluates bracket expression at runtime to determine object key name.
b) Creates an array property key.
c) Defines a computed getter property.
d) Throws SyntaxError.
Correct Answer: a) Evaluates bracket expression at runtime to determine object key name.
Explanation:
Square brackets around property key permit dynamic expressions as object property names.

87. What is the output of `let key = "name"; let obj = { [key]: "Alice" }; console.log(obj.name);`?

a) Alice
b) undefined
c) key
d) TypeError
Correct Answer: a) Alice
Explanation:
Computed key `[key]` evaluates variable `key` ("name"), establishing property `obj.name = "Alice"`.

88. What is the prototype object of standard plain object literal `{}`?

a) Object.prototype
b) Function.prototype
c) null
d) Array.prototype
Correct Answer: a) Object.prototype
Explanation:
Plain objects created via literals inherit directly from Object.prototype.

89. What method returns prototype object of specified target object?

a) Object.getPrototypeOf()
b) Object.getProto()
c) Object.prototypeOf()
d) Object.parent()
Correct Answer: a) Object.getPrototypeOf()
Explanation:
Object.getPrototypeOf(obj) is standard method for reading target object prototype.

90. What method sets prototype object of specified target object?

a) Object.setPrototypeOf()
b) Object.setProto()
c) Object.prototypeSet()
d) Object.assignProto()
Correct Answer: a) Object.setPrototypeOf()
Explanation:
Object.setPrototypeOf(obj, prototype) changes prototype of target object.

91. What is the top-level prototype at the end of JavaScript prototype chains?

a) null
b) Object.prototype
c) undefined
d) Function
Correct Answer: a) null
Explanation:
Object.prototype's internal prototype is null, marking end of prototype chain.

92. What will `console.log(Object.getPrototypeOf(Object.prototype))` output?

a) null
b) Object
c) undefined
d) TypeError
Correct Answer: a) null
Explanation:
Object.prototype lies at end of standard prototype chain, so its prototype is null.

93. What does `obj.hasOwnProperty("toString")` return for plain object `{}`?

a) false
b) true
c) undefined
d) TypeError
Correct Answer: a) false
Explanation:
toString is inherited from Object.prototype, not an own property of instance `{}`.

94. What is the difference between `in` operator and `hasOwnProperty()`?

a) 'in' operator checks prototype chain; hasOwnProperty checks own properties only.
b) 'in' operator checks own properties; hasOwnProperty checks prototype chain.
c) Both check prototype chain.
d) Both check own properties only.
Correct Answer: a) 'in' operator checks prototype chain; hasOwnProperty checks own properties only.
Explanation:
The 'in' operator returns true if property is found on object OR anywhere in prototype chain.

95. What is the output of `let obj = { x: 1, y: 2 }; console.log("toString" in obj)`?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
The 'in' operator searches prototype chain, finding inherited toString property on Object.prototype.

96. What will `console.log([1, 2] instanceof Array)` return?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
instanceof checks if constructor's prototype appears in target object prototype chain.

97. What will `console.log([1, 2] instanceof Object)` return?

a) true
b) false
c) TypeError
d) undefined
Correct Answer: a) true
Explanation:
Array.prototype inherits from Object.prototype, making array instances instances of Object as well.

98. What is getter method in JavaScript object literal `get prop() { return this.val; }`?

a) Binds object property to function evaluated when property is looked up.
b) Creates an async function.
c) Freezes property.
d) Returns object prototype.
Correct Answer: a) Binds object property to function evaluated when property is looked up.
Explanation:
Getters bind a property to a function called automatically upon property access.

99. What is setter method in JavaScript object literal `set prop(val) { this.val = val; }`?

a) Binds object property to function executed whenever attempt is made to assign value.
b) Sets prototype object.
c) Defines constant variable.
d) Deletes property.
Correct Answer: a) Binds object property to function executed whenever attempt is made to assign value.
Explanation:
Setters execute custom code when modifying property value.

100. What happens if getter property is assigned value without defining matching setter in strict mode?

a) TypeError: Cannot set property of #<Object> which has only a getter
b) Assignment succeeds quietly
c) Getter is deleted
d) Property becomes undefined
Correct Answer: a) TypeError: Cannot set property of #<Object> which has only a getter
Explanation:
Attempting to write to a getter-only property in strict mode throws a TypeError.

101. What will `console.log([1, 2, 3].with(1, 99))` output in ES2023?

a) [1, 99, 3]
b) [1, 2, 3]
c) [99, 2, 3]
d) TypeError
Correct Answer: a) [1, 99, 3]
Explanation:
ES2023 with(index, value) returns new copy of array with updated value at specified index without mutating original array.

102. What is the output of `let arr = [10, 20]; let [a, b, c = 30] = arr; console.log(c);`?

a) 30
b) undefined
c) 20
d) TypeError
Correct Answer: a) 30
Explanation:
c falls back to default value 30 because index 2 is undefined in array [10, 20].

103. What will `let { a = 1 } = { a: null }; console.log(a);` output?

a) null
b) 1
c) undefined
d) TypeError
Correct Answer: a) null
Explanation:
Destructuring default values trigger ONLY on `undefined`. Since value is `null`, variable 'a' receives `null`.

104. What will `let { a = 1 } = { a: undefined }; console.log(a);` output?

a) 1
b) undefined
c) null
d) TypeError
Correct Answer: a) 1
Explanation:
When property value is `undefined`, destructuring activates specified default fallback value 1.
← Previous: JavaScript Prototypes & Inheritance MCQs for Developer Interviews
Next →: JavaScript This Keyword & Scope MCQs
JavaScript Advanced MCQs

JavaScript Advanced MCQs

Advanced JavaScript mastery requires a deep understanding of the language’s inner runtime mechanics, memory management, and asynchronous concurrency models. Senior-level…

By MCQs Generator
Python File handlings MCQs

Python File Handling MCQs

Python provides built-in file handling capabilities through the open() function, allowing developers to seamlessly read from, write to, and manipulate…

By MCQs Generator
Python Exception / Error Handling MCQs

Python Exception / Error Handling MCQs

Exception handling in Python provides a structured mechanism to catch, handle, and manage runtime errors without abruptly crashing an application.…

By MCQs Generator