Objects and Arrays
1. Which method is used to add one or more elements to the end of an array?
push()
pop()
shift()
unshift()
Answer
1. `push()`
2. What will const arr = [1, 2, 3]; arr.pop(); console.log(arr); output?
[1, 2]
[1, 2, 3]
[2, 3]
[ ]
Answer
1. `[1, 2]`
3. How do you remove the first element from an array in JavaScript?
arr.pop()
arr.shift()
arr.unshift()
arr.splice(0, 1)
Answer
2. `arr.shift()`
4. What will const arr = [1, 2, 3]; arr.unshift(0); console.log(arr); output?
[1, 2, 3, 0]
[0, 1, 2, 3]
[1, 2, 3]
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?
join()
split()
concat()
slice()
Answer
1. `join()`
6. How can you create a new array with all elements that pass a test provided by a function?
forEach()
map()
filter()
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, 2, 3]
[2, 4, 6]
[0, 1, 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?
every()
find()
some()
includes()
Answer
3. `some()`
9. What will the following code output? const arr = [1, 2, 3, 4]; console.log(arr.slice(1, 3));
[1, 2, 3]
[2, 3, 4]
[2, 3]
[3, 4]
Answer
3. `[2, 3]`
10. Which method would you use to flatten a nested array in JavaScript?
flat()
reduce()
concat()
splice()
Answer
1. `flat()`
11. How do you create an object in JavaScript?
let obj = { };
let obj = ( );
let obj = [ ];
let obj = newObject();
Answer
1. `let obj = { };`
12. What will const obj = {a: 1, b: 2}; console.log(obj.a); output?
undefined
1
2
null
Answer
2. `1`
13. Which method can you use to convert an object’s properties to an array of key-value pairs?
Object.values()
Object.keys()
Object.entries()
Object.assign()
Answer
3. `Object.entries()`
14. What will const obj = {a: 1, b: 2}; delete obj.a; console.log(obj); output?
{a: 1, b: 2}
{a: null, b: 2}
{b: 2}
undefined
Answer
3. `{b: 2}`
15. What is the correct way to check if a property exists in an object?
obj.hasOwnProperty('key')
obj.contains('key')
obj.exists('key')
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); }
let i in obj
const key in obj
key in obj
for obj[key]
Answer
2. `const key in obj`
17. Which method merges properties of two objects into one?
Object.join()
Object.merge()
Object.assign()
Object.push()
Answer
3. `Object.assign()`
18. How would you retrieve all keys of an object as an array?
Object.keys()
Object.values()
Object.entries()
Object.get()
Answer
1. `Object.keys()`
19. What will const obj = {a: 1, b: {c: 3}}; console.log(obj.b.c); output?
undefined
3
{c: 3}
1
Answer
2. `3`
20. Which method creates a shallow copy of an object?
Object.clone()
Object.assign()
Object.spread()
Object.create()
Answer
2. `Object.assign()`