Episode 1 — Fundamentals / 1.24 — Object Methods
1.24 — Exercise Questions: Object Methods
Practice questions for all eight subtopics in Section 1.24. Mix of short-answer, prediction, and coding tasks.
How to use this material (instructions)
- Read lessons in order —
README.md, then1.24.a->1.24.h. - Answer closed-book first — then compare to the matching lesson.
- Use the Console — run examples in DevTools or Node.js to verify your predictions.
- Interview prep —
1.24-Interview-Questions.md. - Quick review —
1.24-Quick-Revision.md.
1.24.a — Object.entries (Q1-Q8)
Q1. What does Object.entries({ x: 1, y: 2 }) return? What is the type of each inner element?
Q2. Does Object.entries() include inherited properties? What about Symbol-keyed properties?
Q3. Write code that iterates over an object using Object.entries() and destructuring in a for...of loop.
Q4. Convert the object { name: "Alice", age: 30 } into a Map using Object.entries().
Q5. Write a function toQueryString(obj) that converts { search: "hello", page: 2 } into "search=hello&page=2".
Q6. Given { a: 1, b: "", c: 0, d: "hello", e: null }, write a pipeline using entries and fromEntries to keep only properties with truthy values.
Q7. What does Object.entries("abc") return?
Q8. Predict the output:
const obj = { 2: "two", 1: "one", b: "bee", a: "ay" };
console.log(Object.entries(obj));
1.24.b — Object.assign (Q9-Q16)
Q9. What does Object.assign return? Is it a new object?
Q10. What is the problem with Object.assign(defaults, userConfig)? How do you fix it?
Q11. Predict the output:
const a = { x: 1 };
const b = { x: 2, y: 3 };
const c = { x: 3, z: 4 };
const result = Object.assign({}, a, b, c);
console.log(result);
Q12. Explain why Object.assign produces a shallow copy. What happens to nested objects?
Q13. Does Object.assign copy Symbol-keyed properties? Does Object.entries?
Q14. Write code using Object.assign to clone an object and add a new property in one step.
Q15. What happens to getters on a source object when copied via Object.assign?
Q16. Compare Object.assign({}, obj) and { ...obj }. Name one situation where they behave differently.
1.24.c — Object.freeze (Q17-Q24)
Q17. What three things does Object.freeze() prevent?
Q18. In non-strict mode, what happens when you try to modify a frozen object? In strict mode?
Q19. Predict the output:
const config = Object.freeze({ db: { host: "localhost", port: 5432 } });
config.db.port = 9999;
console.log(config.db.port);
Q20. Write a deepFreeze function that recursively freezes all nested objects.
Q21. What is the difference between const obj = {} and Object.freeze(obj) in terms of protection?
Q22. Can you freeze an array? What happens when you try push on a frozen array?
Q23. Is Object.isFrozen({}) true or false? What about Object.isFrozen(42)?
Q24. You have a frozen object. Can you spread it into a new object and add properties to that new object?
1.24.d — Object.fromEntries (Q25-Q32)
Q25. What does Object.fromEntries([["a", 1], ["b", 2]]) return?
Q26. Convert a Map with entries ("name", "Alice") and ("age", 30) into a plain object.
Q27. Write a pick(obj, keys) function that returns a new object with only the specified keys.
Q28. Write a omit(obj, keys) function that returns a new object excluding the specified keys.
Q29. What happens with duplicate keys in Object.fromEntries([["a", 1], ["a", 2]])?
Q30. Convert URLSearchParams("?q=hello&lang=en") to a plain object.
Q31. Write a pipeline to take an object of prices and return a new object with a 10% discount applied to all values.
Q32. What does Object.fromEntries(Object.entries(obj)) produce? Is it the same reference as obj?
1.24.e — Object.is (Q33-Q38)
Q33. What does Object.is(NaN, NaN) return? How does this differ from NaN === NaN?
Q34. What does Object.is(+0, -0) return? How does this differ from +0 === -0?
Q35. Fill in the table:
| Expression | === | Object.is() |
|---|---|---|
NaN, NaN | ? | ? |
+0, -0 | ? | ? |
null, undefined | ? | ? |
1, "1" | ? | ? |
Q36. Why does React use Object.is instead of === for state comparison?
Q37. Does Object.is({}, {}) return true? Why or why not?
Q38. Write a polyfill for Object.is using only === and basic arithmetic.
1.24.f — Object.keys (Q39-Q46)
Q39. What does Object.keys({ a: 1, b: 2, c: 3 }) return?
Q40. Predict the order of Object.keys({ z: 1, 5: "five", a: 2, 1: "one" }).
Q41. How do you check if an object is "empty" (has no own enumerable properties)?
Q42. What is the difference between Object.keys(obj) and for...in? Which includes inherited properties?
Q43. What does Object.keys("hello") return?
Q44. Compare Object.keys and Object.getOwnPropertyNames. Which includes non-enumerable properties?
Q45. Write a validateShape(obj, requiredKeys) function that throws if any required keys are missing.
Q46. Write code that counts how many properties an object has.
1.24.g — Object.seal (Q47-Q52)
Q47. After sealing an object, which operations are allowed and which are blocked?
Q48. Compare Object.seal and Object.freeze in a table with three rows: add, delete, modify.
Q49. Predict the output (strict mode):
"use strict";
const form = Object.seal({ name: "", email: "" });
form.name = "Alice";
form.phone = "123";
Q50. Is Object.seal shallow or deep? What happens to nested objects?
Q51. Is a frozen object also sealed? Is a sealed object also frozen? Explain.
Q52. Write a use case where Object.seal is appropriate but Object.freeze would be too strict.
1.24.h — Object.values (Q53-Q58)
Q53. What does Object.values({ a: 10, b: 20, c: 30 }) return?
Q54. Write a one-liner to sum all values in { jan: 100, feb: 200, mar: 150 }.
Q55. How would you find the maximum value in an object using Object.values?
Q56. Check if the value "admin" exists anywhere in { user1: "admin", user2: "editor" }.
Q57. Write code that extracts unique values from an object using Object.values and Set.
Q58. When should you use Object.values vs Object.entries?
Cross-topic questions (Q59-Q65)
Q59. Explain the relationship: Object.fromEntries(Object.entries(obj)). What is lost in this round-trip?
Q60. You need an object that:
- Has a fixed set of keys (no additions/deletions)
- But values can be updated
Which method do you use:
freeze,seal, orpreventExtensions?
Q61. Given an object, write code that:
- Filters out properties with
nullvalues - Transforms remaining string values to uppercase
- Returns a new frozen object
const input = { name: "alice", age: 30, city: null, role: "admin" };
// Expected: frozen { name: "ALICE", age: 30, role: "ADMIN" }
Q62. Which Object methods copy Symbol-keyed properties?
Object.keys/Object.values/Object.entries— ?Object.assign— ?{ ...obj }(spread) — ?
Q63. Create a function shallowEqual(a, b) that returns true if two objects have the same keys and Object.is-equal values.
Q64. Explain why Object.assign(target, src) triggers setters on target but { ...src } does not.
Q65. Write a deepClone function using Object.entries, recursion, and Object.fromEntries.
Answer hints
| Q | Hint |
|---|---|
| Q8 | Integer keys sorted first: [["1","one"],["2","two"],["b","bee"],["a","ay"]] |
| Q10 | Mutates defaults; fix: Object.assign({}, defaults, userConfig) |
| Q13 | Object.assign copies Symbols; Object.entries does not |
| Q19 | 9999 — shallow freeze, nested object is mutable |
| Q23 | Object.isFrozen({}) is false; Object.isFrozen(42) is true |
| Q29 | Last value wins: { a: 2 } |
| Q34 | Object.is(+0, -0) is false; +0 === -0 is true |
| Q40 | ["1", "5", "z", "a"] — integers sorted, then strings by insertion |
| Q49 | form.name = "Alice" works; form.phone = "123" throws TypeError |
| Q51 | Frozen implies sealed; sealed does NOT imply frozen (values still writable) |
| Q59 | Non-enumerable props, Symbol keys, and prototype are lost |
| Q62 | keys/values/entries: no Symbols; assign and spread: yes Symbols |
<- Back to 1.24 — Object Methods (README)