Destructuring & Spread — Pulling Data Apart — Concept Check
Questions on Destructuring & Spread — Pulling Data Apart. Destructuring extracts values from objects and arrays into variables; spread and rest do the reverse. Together they remove most of the boilerplate around passing data around.
Questions in this quiz
8 questions on Destructuring & Spread — Pulling Data Apart — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have an object `user` with properties `name`, `email`, and `role`. How can you extract `name` and `role` with a default value of 'guest' for role?
- Aconst { name, role = 'guest' } = user
- Bconst { name, role: 'guest' } = user
- Cconst { name, role: 'student' } = user
- Dconst { name: userName, role: 'guest' } = user
2.In a React component, you want to pass all props except `label` to an `<input>` element. Which pattern achieves this?
- Afunction Input({ ...props }) { return <input {...props} />; }
- Bfunction Input({ label, inputProps }) { return <input {...inputProps} />; }
- Cfunction Input({ label, ...inputProps }) { return <input {...inputProps} />; }
- Dfunction Input({ label, ...props }) { return <input {...label} />; }
3.Given an array `items = [1, 2, 3, 4]`, what will `const [first, ...rest] = items;` result in?
- Afirst = 1, rest = [1, 2, 3]
- Bfirst = 1, rest = [2, 3, 4]
- Cfirst = 1, rest = [2, 3]
- Dfirst = 1, rest = [3, 4]
4.A developer uses `function log(level, ...messages) {}` to log messages. What does `...messages` represent?
- AIt renames `messages` to `level`.
- BIt expands the `messages` array into individual arguments.
- CIt collects all arguments after `level` into an array.
- DIt sets default values for `messages`.
5.You want to merge two objects `defaults` and `overrides`. Which syntax correctly achieves this?
- Aconst merged = { defaults, overrides };
- Bconst merged = { defaults: overrides };
- Cconst merged = { ...defaults, ...overrides };
- Dconst merged = { ...overrides, ...defaults };
6.What happens if you destructure an object with a missing property and no default value?
- AThe variable is assigned `null`.
- BThe variable retains its previous value.
- CAn error is thrown.
- DThe variable is assigned `undefined`.
7.A developer mistakenly believes `const { a, b } = obj` renames properties. What is the correct behavior?
- AIt renames properties `a` and `b` in `obj`.
- BIt creates new properties `a` and `b` in `obj`.
- CIt extracts properties `a` and `b` by name from `obj`.
- DIt sets default values for `a` and `b`.
8.Which of the following correctly swaps two variables `x` and `y` without a temporary variable?
- Ax, y = y, x;
- B[x, y] = [y, x];
- Cx = y; y = x;
- Dswap(x, y);