LearnFrenzy
Sign in Take a free test

ES6 Features · Exercise 3 of 4

ES6 Features Find the Error

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

Debug fifteen ES6 programs: SyntaxErrors in class bodies, super, private fields, generators and imports; runtime errors from calling classes without new, this before super, weak keys, new Symbol and non-iterables; and silent bugs with Map, Set, exhausted generators and spread class instances.

Question 1 Easy

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

main.js
class Cart {
  function add(item) {
    return item;
  }
}
A SyntaxError: class methods are written without the function keyword – add(item) { … }
B It defines a method called add
C It defines a static function
D ReferenceError
Question page & discussion
Question 2 Medium

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

main.js
class Logger {
  constructor(prefix) {
    super();
    this.prefix = prefix;
  }
}
A SyntaxError: super() is only allowed in the constructor of a class that uses extends
B It works; super() calls Object
C ReferenceError at run time
D TypeError at run time
Question page & discussion
Question 3 Medium

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

main.js
class Card {
  #pin = 1234;
}
const card = new Card();
console.log(card.#pin);
A SyntaxError: the private name #pin can only be used inside the Card class body
B It prints 1234
C It prints undefined
D TypeError at run time
Question page & discussion
Question 4 Medium

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

main.js
function numbers() {
  yield 1;
  yield 2;
}
console.log([...numbers()]);
A It prints [ 1, 2 ]
B SyntaxError: yield only works inside a generator – declare it as function* numbers()
C It prints []
D TypeError: numbers() is not iterable
Question page & discussion
Question 5 Medium

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

main.js
class Point {
  constructor() { this.x = 0; this.y = 0; }
  constructor(x, y) { this.x = x; this.y = y; }
}
A The second constructor overrides the first
B SyntaxError: a class may only have one constructor – use default parameters (x = 0, y = 0)
C JavaScript picks the constructor by argument count
D It works only with new
Question page & discussion
Question 6 Hard

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

main.js
import { readFile } from "node:fs/promises";
console.log(typeof readFile);
A SyntaxError: import statements are only allowed in ES modules – use .mjs / "type": "module" (or type="module" in the browser)
B It prints function
C It prints undefined
D ReferenceError: readFile is not defined
Question page & discussion
Question 7 Easy

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

main.js
class User {
  constructor(name) { this.name = name; }
}
const u = User("Asha");
console.log(u.name);
A TypeError: Class constructor User cannot be invoked without 'new'
B It prints Asha
C It prints undefined
D ReferenceError
Question page & discussion
Question 8 Medium

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

main.js
class Vehicle {
  constructor(wheels) { this.wheels = wheels; }
}
class Bike extends Vehicle {
  constructor(brand) {
    this.brand = brand;
    super(2);
  }
}
console.log(new Bike("Hero").brand);
A ReferenceError: super() must be called before this is used in a derived constructor
B It prints Hero
C TypeError
D SyntaxError
Question page & discussion
Question 9 Hard

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

main.js
const cache = new WeakMap();
cache.set("user:7", { name: "Asha" });
console.log(cache.get("user:7"));
A It prints { name: 'Asha' }
B It prints undefined
C SyntaxError
D TypeError: Invalid value used as weak map key – WeakMap keys must be objects; use a Map for string keys
Question page & discussion
Question 10 Medium

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

main.js
const key = new Symbol("session");
console.log(key.toString());
A It prints Symbol(session)
B TypeError: Symbol is not a constructor – call Symbol("session") without new
C It prints session
D SyntaxError
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links