JavaScript Objects & Key/Value Manipulation Quiz

JavaScript
0 Passed
0% acceptance

Explore 75 JavaScript object questions that cover construction, property access, mutation, introspection, cloning, and immutability. Thirty questions include runnable snippets so you can reason through exact behaviour before answering.

75 Questions
~150 minutes
1

Question 1

What string is printed when the following customer object is logged?

javascript
const customer = { name: 'Nia', tier: 'gold' }
console.log(customer.name + ' - ' + customer['tier'])
A
Nia - gold
B
undefined - gold
C
Nia - undefined
D
gold - Nia
2

Question 2

What value is printed for report.priority?

javascript
const key = 'priority'
const report = { id: 7 }
report[key] = 'high'
console.log(report.priority)
A
high
B
undefined
C
priority
D
7
3

Question 3

A meeting object is cloned with spread before mutation. What tuple is logged?

javascript
const meeting = { title: 'Kickoff', owner: 'Sam' }
const copy = { ...meeting }
copy.owner = 'Dana'
console.log(meeting.owner, copy.owner)
A
Sam Dana
B
Dana Dana
C
Sam Sam
D
Dana Sam
4

Question 4

After adding and deleting properties, what keys remain on the device object?

javascript
const device = { id: 'A1', status: 'online' }
device.location = 'eu-west'
delete device.status
console.log(Object.keys(device))
A
['id', 'location']
B
['id', 'status']
C
['location']
D
['status', 'location']
5

Question 5

Consider a nested configuration. What value is printed?

javascript
const config = { feature: { enabled: true, rollout: 0.3 } }
console.log(config.feature.rollout)
A
0.3
B
true
C
undefined
D
feature.rollout
6

Question 6

What does the following log statement produce after Request is sealed?

javascript
const Request = { method: 'GET', retries: 1 }
Object.seal(Request)
Request.url = '/status'
Request.retries = 2
delete Request.method
console.log(Request)
A
{ method: 'GET', retries: 2 }
B
{ method: undefined, retries: 2 }
C
{ method: 'GET', retries: 1, url: '/status' }
D
{ method: 'GET', retries: 1 }
7

Question 7

Which value is logged when using a computed property?

javascript
const suffix = 'Id'
const record = { ['user' + suffix]: 42 }
console.log(record.userId)
A
42
B
userId
C
undefined
D
suffix
8

Question 8

What is the console output when merging objects with spread order sensitivity?

javascript
const base = { role: 'member', active: true }
const overrides = { active: false, region: 'us' }
const user = { ...base, ...overrides }
console.log(`${user.role}:${user.active}:${user.region}`)
A
member:false:us
B
member:true:us
C
member:false:undefined
D
undefined:false:us
9

Question 9

After deleting a nonexistent property, what does the log show?

javascript
const invoice = { total: 199 }
delete invoice.tax
console.log(invoice.total, 'tax' in invoice)
A
199 false
B
199 true
C
undefined false
D
undefined true
10

Question 10

How many iterations does the for…in loop perform?

javascript
const featureFlag = { id: 'beta', enabled: true, cohort: 'A' }
let count = 0
for (const key in featureFlag) {
  if (Object.prototype.hasOwnProperty.call(featureFlag, key)) count++
}
console.log(count)
A
3
B
0
C
1
D
Depends on the prototype
11

Question 11

What string is produced after Object.entries mapping?

javascript
const env = { NODE_ENV: 'prod', REGION: 'us-east-1' }
const pairs = Object.entries(env).map(([key, value]) => `${key}=${value}`)
console.log(pairs.join(';'))
A
NODE_ENV=prod;REGION=us-east-1
B
prod=NODE_ENV;us-east-1=REGION
C
NODE_ENV,REGION
D
prod;us-east-1
12

Question 12

When freezing an object, what happens to nested objects?

javascript
const settings = { theme: { dark: true }, version: 1 }
Object.freeze(settings)
settings.version = 2
settings.theme.dark = false
console.log(settings.version, settings.theme.dark)
A
1 false
B
2 true
C
1 true
D
2 false
13

Question 13

What value is returned by checking key existence with in?

javascript
const payload = Object.assign({ id: 1 }, { status: 'queued' })
console.log('status' in payload)
A
true
B
false
C
Queued
D
Throws TypeError
14

Question 14

What output is produced when iterating over Object.values?

javascript
const metrics = { open: 3, closed: 5 }
const sum = Object.values(metrics).reduce((acc, value) => acc + value, 0)
console.log(sum)
A
8
B
35
C
0
D
undefined
15

Question 15

A dynamic property is deleted after it is added. What does the log show?

javascript
const session = {}
session['user-' + 42] = 'active'
delete session['user-' + 42]
console.log(Object.keys(session).length)
A
0
B
1
C
undefined
D
Throws ReferenceError
16

Question 16

How does property shadowing behave in the following snippet?

javascript
const base = { status: 'pending' }
const draft = Object.create(base)
draft.status = 'ready'
console.log(draft.status, base.status)
A
ready pending
B
pending pending
C
ready ready
D
pending ready
17

Question 17

What happens when using Object.assign to merge into the first argument?

javascript
const defaults = { retries: 3, timeout: 1000 }
const params = Object.assign({}, defaults, { timeout: 2000 })
console.log(defaults.timeout, params.timeout)
A
1000 2000
B
2000 2000
C
1000 1000
D
undefined undefined
18

Question 18

What does hasOwnProperty report for an inherited property?

javascript
const proto = { shared: true }
const obj = Object.create(proto)
console.log(obj.hasOwnProperty('shared'))
A
false
B
true
C
undefined
D
Throws TypeError
19

Question 19

What tuple is printed after cloning a nested object with shallow spread?

javascript
const state = { user: { name: 'Lee' }, loaded: false }
const clone = { ...state }
clone.user.name = 'Jo'
console.log(state.user.name, clone.loaded)
A
Jo false
B
Lee false
C
Jo true
D
Lee true
20

Question 20

What is logged after sealing and attempting to delete a property?

javascript
const policy = { active: true, tier: 'pro' }
Object.seal(policy)
delete policy.tier
console.log('tier' in policy)
A
true
B
false
C
undefined
D
Throws TypeError
21

Question 21

What string is produced when mixing dot and bracket notation with numeric keys?

javascript
const logs = { '200': 'ok', 404: 'missing' }
console.log(logs[200] + '/' + logs['404'])
A
ok/missing
B
undefined/missing
C
ok/undefined
D
undefined/undefined
22

Question 22

How many properties are reported by Object.keys after using Object.defineProperty with enumerable false?

javascript
const entity = { id: 1 }
Object.defineProperty(entity, 'secret', { value: 99, enumerable: false })
console.log(Object.keys(entity).length)
A
1
B
2
C
0
D
Throws TypeError
23

Question 23

What happens when destructuring with default values?

javascript
const response = { status: 200 }
const { status, data = null } = response
console.log(status, data)
A
200 null
B
200 undefined
C
undefined null
D
Throws TypeError
24

Question 24

What value does hasOwnProperty return after deleting a key defined on the prototype?

javascript
function Service() {}
Service.prototype.mode = 'sync'
const instance = new Service()
delete instance.mode
console.log(instance.hasOwnProperty('mode'))
A
false
B
true
C
undefined
D
Throws TypeError
25

Question 25

What is the final value of score after using the in operator to guard updates?

javascript
const scorecard = { alice: 10 }
if ('alice' in scorecard) scorecard.alice += 5
if (!('bob' in scorecard)) scorecard.bob = 3
console.log(scorecard.alice + scorecard.bob)
A
18
B
15
C
13
D
3
26

Question 26

A product object is updated using Object.assign to merge metadata. What string is logged?

javascript
const product = { sku: 'S-1', stock: 10 }
Object.assign(product, { stock: 5, discontinued: false })
console.log(`${product.stock}:${product.discontinued}`)
A
5:false
B
10:false
C
5:undefined
D
10:undefined
27

Question 27

Which value is returned when safely reading a nested property with optional chaining?

javascript
const profile = { info: { email: '[email protected]' } }
console.log(profile.preferences?.theme ?? 'light')
A
light
B
theme
C
undefined
D
Throws TypeError
28

Question 28

What is the effect of using delete on a property from a frozen object?

javascript
const attrs = Object.freeze({ kind: 'task', owner: 'Jo' })
const result = delete attrs.owner
console.log(result, attrs.owner)
A
false Jo
B
true Jo
C
false undefined
D
true undefined
29

Question 29

How does Object.entries behave when properties are added in a different order?

javascript
const summary = {}
summary.user = 'Ada'
summary.total = 5
const entries = Object.entries(summary)
console.log(entries[0][0], entries.length)
A
user 2
B
total 2
C
user 1
D
total 1
30

Question 30

What result is printed when using Object.assign for shallow merging of nested settings?

javascript
const baseSettings = { theme: { color: 'blue' }, mode: 'lite' }
const finalSettings = Object.assign({}, baseSettings, { mode: 'dark' })
finalSettings.theme.color = 'green'
console.log(baseSettings.theme.color, finalSettings.mode)
A
green dark
B
blue dark
C
green lite
D
blue lite
31

Question 31

When using object literals, which statement best describes how duplicate keys are handled?

A
The last declaration of a duplicate key overwrites earlier values in the literal.
B
JavaScript throws a SyntaxError for duplicate keys.
C
All duplicate keys are stored in an array automatically.
D
Duplicate keys are ignored and the first value wins.
32

Question 32

Why is bracket notation required when accessing a property whose name is stored in a variable?

A
Dot notation requires literal identifiers; bracket notation evaluates expressions to determine the key.
B
Dot notation is forbidden in strict mode.
C
Bracket notation is faster than dot notation.
D
Dot notation automatically lowercases keys.
33

Question 33

Why might deleting properties inside a for…in loop produce unexpected iteration results?

A
Deleting keys mutates the collection mid-iteration, potentially skipping remaining properties or altering order.
B
for…in automatically recreates deleted keys.
C
delete throws inside loops.
D
for…in iterates only over prototype properties.
34

Question 34

How does the in operator differ from hasOwnProperty?

A
in checks both own and inherited properties, whereas hasOwnProperty considers only own properties.
B
in only works on arrays.
C
hasOwnProperty does not work in strict mode.
D
They are identical when objects lack prototypes.
35

Question 35

Why is Object.keys preferable to for…in when you only care about an object’s own enumerable properties?

A
Object.keys returns an explicit list that excludes inherited keys, simplifying deterministic loops.
B
Object.keys includes prototype properties automatically.
C
for…in is deprecated.
D
for…in throws on frozen objects.
36

Question 36

Why does JSON.stringify ignore functions on objects?

A
JSON is a data-only format; functions are skipped because they are not valid JSON values.
B
JSON.stringify cannot read object keys.
C
Functions throw during serialization.
D
Functions are converted to null by specification.
37

Question 37

When using Object.values on a dictionary, why must you be mindful of numeric strings ordering?

A
Object property enumeration orders numeric-like keys first in ascending order before string keys.
B
Object.values sorts alphabetically by default.
C
Object.values is random.
D
Object.values only works with integers.
38

Question 38

Why is Object.assign unsuitable for deep cloning nested objects?

A
assign copies property references; nested objects remain linked to the source.
B
assign only works on arrays.
C
assign ignores enumerable properties.
D
assign freezes nested objects automatically.
39

Question 39

Why might Object.freeze be preferred over const when preventing configuration changes?

A
const blocks reassignment of the identifier but not mutation; freeze prevents property modifications.
B
Freeze makes the object immutable and deletes the identifier.
C
const automatically deep freezes objects.
D
Freeze only works with primitives.
40

Question 40

What is the main limitation of using hasOwnProperty directly on objects created with Object.create(null)?

A
Objects without prototypes lack the method; you must borrow it from Object.prototype.
B
Object.create(null) prevents property lookup altogether.
C
hasOwnProperty throws when prototypes are missing.
D
Object.create(null) automatically freezes the object.
41

Question 41

Why is using Object.keys(obj).length to check emptiness usually safe for plain objects?

A
Keys length reflects enumerable own properties; empty objects produce length zero.
B
Object.keys includes inherited properties.
C
Length throws on empty objects.
D
It mutates the object.
42

Question 42

When migrating data from arrays to objects, why might you prefer Object.fromEntries?

A
fromEntries consumes [key, value] pairs and outputs an object without manual loops.
B
fromEntries sorts keys automatically.
C
fromEntries deep clones nested objects.
D
fromEntries only works with Maps.
43

Question 43

Why is it risky to rely on JSON.parse(JSON.stringify(obj)) for cloning when objects contain Date instances?

A
Dates serialize to strings, losing their original type and behaviour when parsed back.
B
It throws when encountering Date.
C
Date values become numbers automatically.
D
JSON.stringify ignores Date entirely.
44

Question 44

What advantage does Object.entries provide over Object.keys for transforming objects into arrays of label/value pairs?

A
Entries deliver both keys and values together, avoiding manual lookups while mapping.
B
Entries returns non-enumerable properties.
C
Entries sorts values automatically.
D
Entries excludes numeric keys.
45

Question 45

Why might you prefer using Object.assign({}, source) over spread when targeting legacy environments?

A
Spread syntax requires newer language support, whereas Object.assign is widely polyfillable.
B
Spread causes deep clones automatically.
C
Object.assign ignores symbol keys.
D
Spread throws on frozen objects.
46

Question 46

Why is it considered best practice to use Object.create(null) for dictionary-like objects that store untrusted keys?

A
It creates objects without prototypes, preventing prototype pollution or collisions with inherited keys.
B
It automatically freezes the dictionary.
C
It sorts keys alphabetically.
D
It enforces strict typing on keys.
47

Question 47

Why does using delete on arrays (e.g., delete arr[0]) differ from using splice?

A
delete leaves sparse holes with undefined values, whereas splice shifts elements and updates length.
B
delete removes multiple elements at once.
C
splice only removes from the end.
D
delete is faster and preferred for cleanup.
48

Question 48

When converting objects to Map instances, what key difference should you account for?

A
Maps preserve insertion order and allow non-string keys, unlike plain objects.
B
Maps automatically deep clone values.
C
Maps ignore symbol keys.
D
Maps disallow iteration.
49

Question 49

What is the primary difference between Object.seal and Object.freeze?

A
Seal prevents adding/removing properties but allows value changes; freeze prevents both structural changes and value mutations.
B
Freeze is only for arrays.
C
Seal enforces deep immutability.
D
Freeze allows delete; seal does not.
50

Question 50

Why is it beneficial to document whether an API returns plain objects versus instances of custom classes?

A
Consumers need to know which properties are own versus inherited and whether prototype methods are available.
B
Classes automatically freeze returned objects.
C
Plain objects cannot hold methods.
D
Custom instances are always immutable.
51

Question 51

Why might you wrap property updates in Object.hasOwn (or hasOwnProperty) when merging user-supplied payloads?

A
It ensures only expected own properties are processed, preventing prototype pollution or overrides from crafted payloads.
B
It speeds up iteration by skipping values.
C
It throws when encountering nested objects.
D
It silently removes prototype methods.
52

Question 52

How can you create a shallow immutable snapshot of an object for logging purposes without affecting future mutations?

A
Use Object.freeze({ ...obj }) so the snapshot cannot be changed.
B
Assign obj directly to a new variable.
C
Use delete on all properties after logging.
D
Call Object.seal on the original object.
53

Question 53

What benefit does Object.entries combined with array destructuring provide in transformation pipelines?

A
It allows concise extraction of key and value variables without manual indexing inside loops.
B
It automatically filters out undefined values.
C
It deep clones the object.
D
It sorts the keys numerically.
54

Question 54

Why should you prefer Object.hasOwn over obj.hasOwnProperty when obj might come from untrusted sources?

A
Object.hasOwn works even if hasOwnProperty is shadowed or removed on the instance.
B
Object.hasOwn is slower but safer.
C
obj.hasOwnProperty is deprecated.
D
Object.hasOwn automatically deep checks nested objects.
55

Question 55

When should you prefer spread syntax over Object.assign for cloning?

A
When immutability libraries expect literal syntax and readability matters more than legacy support.
B
When you need deep clones.
C
When your object has symbol keys that you need to drop.
D
When you want to mutate the original object.
56

Question 56

Why does using Object.freeze on configuration data improve reliability in multi-team environments?

A
It prevents accidental property changes, enforcing contracts agreed upon across teams.
B
It makes objects enumerable.
C
It automatically serializes data.
D
It removes prototype chains.
57

Question 57

What risk arises when iterating an object with for…in without guarding with hasOwnProperty?

A
Prototype properties may be processed, leading to unintended logic or security issues.
B
The loop skips all own properties.
C
for…in automatically freezes the object.
D
It converts the object into an array.
58

Question 58

Why is Object.defineProperty useful when you need to hide metadata from typical iteration?

A
It allows defining non-enumerable properties that remain accessible but invisible to Object.keys.
B
It clones the object.
C
It sorts properties alphabetically.
D
It freezes the property.
59

Question 59

When composing objects from smaller fragments, why is Object.assign({}, ...sources) safer than mutating the first source directly?

A
It preserves immutability of source fragments, preventing accidental side effects.
B
It performs deep cloning automatically.
C
It converts arrays into objects.
D
It removes undefined values.
60

Question 60

Why is it important to document whether an object uses symbol keys?

A
Symbol keys are invisible to Object.keys/values/entries, so consumers must know to use Object.getOwnPropertySymbols.
B
Symbols are deprecated in modern JavaScript.
C
Symbol keys cannot be iterated at all.
D
Symbol keys automatically stringify.
61

Question 61

When cloning configuration with spread, why must you still be cautious with nested arrays inside the object?

A
Spread copies array references; nested arrays will still point to the same instance.
B
Spread throws when arrays are encountered.
C
Spread turns arrays into objects.
D
Spread deep clones arrays automatically.
62

Question 62

Why is Object.keys preferable to Object.values when you simply need the count of properties?

A
Keys and values arrays share length, but keys communicates intent explicitly and avoids iterating values lazily.
B
Values excludes enumerable properties.
C
Values is deprecated.
D
Keys returns faster than values in all cases.
63

Question 63

Why is it beneficial to avoid mutating global configuration objects directly inside functions?

A
Direct mutation introduces hidden coupling and makes reasoning about changes difficult across modules.
B
Functions cannot modify objects.
C
Configuration objects are read-only by default.
D
All objects in JavaScript are passed by value.
64

Question 64

Why might you convert objects to arrays using Object.entries before filtering by values?

A
Entries let you leverage array methods like filter/map while preserving both key and value context.
B
Entries sorts keys alphabetically.
C
Entries deep clones objects automatically.
D
Entries removes prototype properties.
65

Question 65

What benefit does Object.freeze provide when sharing configuration across worker threads?

A
Immutability guarantees threads cannot diverge the shared configuration, avoiding race conditions on state.
B
Freeze speeds up serialization.
C
Freeze forces deep cloning.
D
Freeze sorts all keys.
66

Question 66

When should you choose hasOwnProperty over checking for undefined?

A
When the property could legitimately exist with a value of undefined, making direct comparison ambiguous.
B
When you want to read prototype properties.
C
When the property names are numeric.
D
When the object is frozen.
67

Question 67

Why is it useful to convert configuration objects to Maps when keys may not be strings?

A
Maps accept any type as key without coercion, preserving semantic identity (e.g., objects or functions).
B
Maps sort keys automatically.
C
Maps freeze values.
D
Maps deep clone nested objects.
68

Question 68

What advantage does destructuring assignment provide when extracting multiple properties in function parameters?

A
Destructuring clarifies which props are used while providing defaults and renaming in one expression.
B
Destructuring clones nested objects.
C
Destructuring prevents mutation.
D
Destructuring enforces property order.
69

Question 69

Why does the order of spread operations matter when composing configuration objects?

A
Later spreads overwrite earlier values for duplicate keys, so precedence is determined by order.
B
Spread order is random.
C
Spread always deep clones earlier values.
D
Spread converts keys to symbols.
70

Question 70

Why is Object.preventExtensions less strict than Object.seal?

A
preventExtensions only blocks adding new properties, while seal also blocks deletions.
B
preventExtensions deep clones the object.
C
seal allows adding properties.
D
preventExtensions freezes existing properties.
71

Question 71

When iterating nested objects, why should you check whether a property is itself an object before recursing?

A
To avoid treating primitives as objects and to handle null values safely.
B
Because recursion only works on arrays.
C
Because JSON.stringify requires it.
D
Because objects cannot contain primitives.
72

Question 72

Why is it risky to rely on enumeration order of object keys for logic such as caching or scheduling?

A
Although predictable for most cases, order semantics differ for numeric-like keys and should not replace ordered structures like arrays or maps.
B
Key order is always random and non-deterministic.
C
Objects automatically sort keys descending.
D
Key order resets after every mutation.
73

Question 73

Why might using Object.assign to add defaults inadvertently overwrite existing properties?

A
assign copies properties unconditionally; defaults should be first, overrides last to avoid overwriting user data.
B
assign ignores undefined values.
C
assign cannot merge booleans.
D
assign is read-only.
74

Question 74

When flattening nested objects for logging, why should you track the path (e.g., parent.child) alongside the value?

A
It preserves context, making logs actionable and preventing collisions when keys repeat at different depths.
B
It speeds up JSON.stringify.
C
It automatically deep clones values.
D
It removes the need for recursion.
75

Question 75

Why is it important to clarify whether an API returns sealed objects?

A
Consumers need to know whether they can extend or delete properties; sealed objects reject structural changes.
B
Sealed objects automatically deep clone data.
C
Sealed objects cannot be iterated.
D
Sealed objects auto-convert to Maps.

QUIZZES IN JavaScript