Objects and Arrays


1. Which method is used to add one or more elements to the end of an array?
  1. push()
  2. pop()
  3. shift()
  4. unshift()
Answer 1. `push()`

2. What will const arr = [1, 2, 3]; arr.pop(); console.log(arr); output?
  1. [1, 2]
  2. [1, 2, 3]
  3. [2, 3]
  4. [ ]
Answer 1. `[1, 2]`

3. How do you remove the first element from an array in JavaScript?
  1. arr.pop()
  2. arr.shift()
  3. arr.unshift()
  4. arr.splice(0, 1)
Answer 2. `arr.shift()`

4. What will const arr = [1, 2, 3]; arr.unshift(0); console.log(arr); output?
  1. [1, 2, 3, 0]
  2. [0, 1, 2, 3]
  3. [1, 2, 3]
  4. undefined
Answer 2. `[0, 1, 2, 3]`

5. Which method would you use to join all elements of an array into a string, separated by a specified separator?
  1. join()
  2. split()
  3. concat()
  4. slice()
Answer 1. `join()`

6. How can you create a new array with all elements that pass a test provided by a function?
  1. forEach()
  2. map()
  3. filter()
  4. reduce()
Answer 3. `filter()`

7. What is the output of const arr = [1, 2, 3]; const doubled = arr.map(x => x * 2); console.log(doubled);?
  1. [1, 2, 3]
  2. [2, 4, 6]
  3. [0, 1, 4]
  4. [4, 6, 8]
Answer 2. `[2, 4, 6]`

8. Which method is used to check if at least one element in an array meets a condition?
  1. every()
  2. find()
  3. some()
  4. includes()
Answer 3. `some()`

9. What will the following code output? const arr = [1, 2, 3, 4]; console.log(arr.slice(1, 3));
  1. [1, 2, 3]
  2. [2, 3, 4]
  3. [2, 3]
  4. [3, 4]
Answer 3. `[2, 3]`

10. Which method would you use to flatten a nested array in JavaScript?
  1. flat()
  2. reduce()
  3. concat()
  4. splice()
Answer 1. `flat()`

11. How do you create an object in JavaScript?
  1. let obj = { };
  2. let obj = ( );
  3. let obj = [ ];
  4. let obj = newObject();
Answer 1. `let obj = { };`

12. What will const obj = {a: 1, b: 2}; console.log(obj.a); output?
  1. undefined
  2. 1
  3. 2
  4. null
Answer 2. `1`

13. Which method can you use to convert an object’s properties to an array of key-value pairs?
  1. Object.values()
  2. Object.keys()
  3. Object.entries()
  4. Object.assign()
Answer 3. `Object.entries()`

14. What will const obj = {a: 1, b: 2}; delete obj.a; console.log(obj); output?
  1. {a: 1, b: 2}
  2. {a: null, b: 2}
  3. {b: 2}
  4. undefined
Answer 3. `{b: 2}`

15. What is the correct way to check if a property exists in an object?
  1. obj.hasOwnProperty('key')
  2. obj.contains('key')
  3. obj.exists('key')
  4. obj.has('key')
Answer 1. `obj.hasOwnProperty('key')`

16. How would you iterate over all keys in an object using for...in?
let obj = { a: 1, b: 2 };
for (/* what goes here? */) { console.log(key); }
  1. let i in obj
  2. const key in obj
  3. key in obj
  4. for obj[key]
Answer 2. `const key in obj`

17. Which method merges properties of two objects into one?
  1. Object.join()
  2. Object.merge()
  3. Object.assign()
  4. Object.push()
Answer 3. `Object.assign()`

18. How would you retrieve all keys of an object as an array?
  1. Object.keys()
  2. Object.values()
  3. Object.entries()
  4. Object.get()
Answer 1. `Object.keys()`

19. What will const obj = {a: 1, b: {c: 3}}; console.log(obj.b.c); output?
  1. undefined
  2. 3
  3. {c: 3}
  4. 1
Answer 2. `3`

20. Which method creates a shallow copy of an object?
  1. Object.clone()
  2. Object.assign()
  3. Object.spread()
  4. Object.create()
Answer 2. `Object.assign()`
On this page