LearnFrenzy
Sign in Take a free test

Spread and Rest Operators · Exercise 3 of 4

Spread and Rest Operators Find the Error

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen JavaScript programs that use spread and rest: SyntaxErrors in rest parameters and patterns, TypeErrors from spreading non-iterables, null and undefined, a RangeError from a huge spread, and silent bugs from shallow copies, undefined overrides, new Set(...arr), spread strings and merge order.

Question 1 Hard

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function track(event, ...details,) {
  return `${event}: ${details.join(", ")}`;
}
console.log(track("click", "button", "home"));
A SyntaxError: a rest parameter cannot be followed by a comma, even a trailing one
B It prints click: button, home
C It prints click:
D TypeError
Question page & discussion
Question 2 Easy

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function split(...left, ...right) {
  return [left, right];
}
A It splits the arguments in half
B left gets everything and right is empty
C TypeError at call time
D SyntaxError: a function can have only one rest parameter, and it must be last
Question page & discussion
Question 3 Easy

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const nums = [1, 2, 3];
const copy = ...nums;
console.log(copy);
A It prints [ 1, 2, 3 ]
B It prints 1
C SyntaxError: spread is only allowed inside an array literal, an object literal or a function call – write [...nums]
D It prints 1 2 3
Question page & discussion
Question 4 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const count = ...items => items.length;
console.log(count(1, 2, 3));
A It prints 3
B It prints 1
C TypeError
D SyntaxError: a rest parameter in an arrow function needs parentheses – (...items) => items.length
Question page & discussion
Question 5 Hard

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const user = { id: 1, name: "Asha", city: "Pune" };
const { id, ...{ name } } = user;
console.log(id, name);
A It prints 1 Asha
B It prints 1 undefined
C SyntaxError: in an object pattern, ... must be followed by a plain variable name, not another pattern
D TypeError
Question page & discussion
Question 6 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const prices = { pen: 20, book: 250 };
const total = [...prices].reduce((a, b) => a + b, 0);
console.log(total);
A It prints 270
B It prints 0
C TypeError: prices is not iterable – spread Object.values(prices) instead
D It prints NaN
Question page & discussion
Question 7 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const defaultTags = ["new"];
const userTags = null;                   // the user chose no tags
const tags = [...defaultTags, ...userTags];
console.log(tags);
A It prints [ 'new' ]
B TypeError: null is not iterable – use ...(userTags ?? [])
C It prints [ 'new', null ]
D It prints []
Question page & discussion
Question 8 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const result = { student: "Asha" };      // no scores property
console.log(Math.max(...result.scores));
A It prints -Infinity
B It prints undefined
C TypeError: result.scores is undefined and cannot be spread – use ...(result.scores ?? [])
D It prints NaN
Question page & discussion
Question 9 Hard

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const readings = new Array(5_000_000).fill(1);
readings[123] = 99;
console.log(Math.max(...readings));
A It prints 99
B It prints 1
C RangeError: Maximum call stack size exceeded – five million arguments are too many; use reduce or a loop
D It prints Infinity
Question page & discussion
Question 10 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function stripId(record) {
  const { id, ...rest } = record;
  return rest;
}
console.log(stripId({ id: 1, name: "Pen" }));
console.log(stripId(null));
A It prints { name: 'Pen' } and {}
B It prints { name: 'Pen' } and null
C It prints { name: 'Pen' }, then TypeError: null cannot be destructured – use record ?? {}
D SyntaxError
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links