Objects — Keys, Spread and Immutable Updates — Concept Check
Questions on Objects — Keys, Spread and Immutable Updates. Objects are the workhorse structure. Reading, iterating and — most importantly — updating them without mutation is the skill that carries straight into React state.
Questions in this quiz
8 questions on Objects — Keys, Spread and Immutable Updates — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have an object `const user = { name: 'Asha', role: 'student' }`. How would you access the `role` property using a computed key?
- Auser.role
- Buser[role]
- Cuser['role']
- Duser['name']
2.In a React application, why is it important to use immutable updates when modifying objects?
- ATo prevent syntax errors
- BTo avoid memory leaks
- CTo ensure components re-render correctly
- DTo improve initial render speed
3.Given `const state = { user: { name: 'Asha' } }`, how do you update the `name` to 'Ravi' immutably?
- Astate.user.name = 'Ravi'
- Bconst newState = { ...state, name: 'Ravi' }
- Cstate.name = 'Ravi'
- Dconst newState = { ...state, user: { ...state.user, name: 'Ravi' } }
4.What happens if you try to access a deeply nested property that might not exist using optional chaining?
- AIt throws a TypeError
- BIt logs a warning to the console
- CIt returns `null`
- DIt returns `undefined` without throwing an error
5.You have `const obj = { a: 1, b: 2 }`. Which method converts it to an array of key-value pairs?
- AObject.entries(obj)
- BObject.keys(obj)
- CObject.values(obj)
- DObject.fromEntries(obj)
6.Why might `const a = { x: 1 }` and `const b = { x: 1 }` return `false` when compared with `a === b`?
- AThey have different keys
- BThey are different references
- CThey are not objects
- DThey have different values
7.A developer uses `const config = { ...defaults, ...userConfig }`. What is the effect of this operation?
- AIt merges arrays within the objects
- B`defaults` properties overwrite `userConfig`
- CBoth objects are mutated
- D`userConfig` properties overwrite `defaults`
8.You mistakenly believe that `Object.keys()` returns key-value pairs. What does it actually return?
- AAn object of keys
- BAn array of keys
- CAn array of values
- DAn array of key-value pairs