Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Quiz: Constructor Property

What does this code print?

class Item { constructor(name) { this.name = name; } } const p = new Item("Potion"); console.log(p.name);
Options
β€’ Potion
β€’ name
β€’ undefined
β€’ An error

Hint : Follow the parameter to the property it fills.

Exercise 2 β€” Quiz: Computed in the Constructor

What does this code print?

class Codemon { constructor(level) { this.hp = level * 10; } } console.log(new Codemon(4).hp);
Options
β€’ 40
β€’ 4
β€’ 10
β€’ undefined

Hint : The constructor can compute a value from its parameters.

Exercise 3 β€” Quiz: Write the Constructor

Complete the class so each instance gets a name.

Hint : One special method runs at creation, one keyword names the object being built.

Exercise 4 β€” Quiz: Separate Instances

What does the last line print?

class C { constructor() { this.n = 0; } } const a = new C(); const b = new C(); a.n = 9; console.log(b.n);
Options
β€’ 0
β€’ 9
β€’ undefined
β€’ An error

Hint : Each new builds its own object.

Exercise 5 β€” Quiz: Method Result

What does this code print?

class Item { constructor(price) { this.price = price; } double() { return this.price * 2; } } console.log(new Item(15).double());
Options
β€’ 30
β€’ 15
β€’ undefined
β€’ An error

Hint : The method reads the instance's property and computes.

Exercise 6 β€” Quiz: No Commas

How are two methods separated inside a class body?

Options
β€’ Nothing, they just follow each other
β€’ A comma
β€’ A semicolon
β€’ The keyword and

Hint : Unlike an object literal, a class body has no separators between methods.

Exercise 7 β€” Quiz: Add a Method

Give the class a `shout` method that returns the name in uppercase.

Hint : The method name matches the call, and the instance is reached through one keyword.

Exercise 8 β€” Quiz: Method Changes the Object

What does the last line print?

class C { constructor() { this.n = 0; } add() { this.n = this.n + 5; } } const c = new C(); c.add(); c.add(); console.log(c.n);
Options
β€’ 10
β€’ 5
β€’ 0
β€’ undefined

Hint : Each call updates the same instance.

Exercise 9 β€” Quiz: Missing new

What happens when this runs?

class C {} const x = C();
Options
β€’ An error
β€’ x holds an instance
β€’ undefined
β€’ Nothing

Hint : A class cannot be called like a plain function.

Exercise 10 β€” Quiz: instanceof

What does this code print?

class A {} class B {} const x = new A(); console.log(x instanceof B);
Options
β€’ false
β€’ true
β€’ undefined
β€’ An error

Hint : An instance remembers exactly which class built it.

Exercise 11 β€” Quiz: Build and Check

Create an instance and check its class.

Hint : One keyword presses the stamp, one operator asks which stamp was used.

Exercise 12 β€” Quiz: Instances From map

What does this code print?

class Item { constructor(n) { this.n = n; } } const items = ["a", "b"].map((n) => new Item(n)); console.log(items.length + items[1].n);
Options
β€’ 2b
β€’ 2a
β€’ ab
β€’ An error

Hint : One instance per source element, then read the second one's property.

Exercise 13 β€” Quiz: this in a Method

What does this code print?

class Codemon { constructor(name) { this.name = name; } who() { return this.name; } } const a = new Codemon("Zap"); const b = new Codemon("Aquachu"); console.log(b.who());
Options
β€’ Aquachu
β€’ Zap
β€’ undefined
β€’ An error

Hint : this is the object before the dot in the call.

Exercise 14 β€” Quiz: Chained Methods

What does the last line print?

class C { constructor() { this.n = 0; } add() { this.n++; return this; } } const c = new C(); c.add().add(); console.log(c.n);
Options
β€’ 2
β€’ 1
β€’ 0
β€’ undefined

Hint : Each call returns the same instance for the next call.

Exercise 15 β€” Quiz: Return the Instance

Make the method chainable.

Hint : Hand the instance itself back so the next call lands on it.

Exercise 16 β€” Quiz: Arrow Callback

Inside a class method, which kind of callback keeps `this` pointing at the instance?

Options
β€’ An arrow function
β€’ A classic function()
β€’ Both do
β€’ Neither does

Hint : One kind of function takes the this of the place it was written in.

Exercise 17 β€” Quiz: Inherited Method

What does this code print?

class A { hi() { return "hi"; } } class B extends A {} console.log(new B().hi());
Options
β€’ hi
β€’ undefined
β€’ An error
β€’ B

Hint : The child keeps every method of the parent.

Exercise 18 β€” Quiz: super in the Constructor

What does this code print?

class A { constructor(x) { this.x = x; } } class B extends A { constructor(x, y) { super(x); this.y = y; } } const b = new B(1, 2); console.log(b.x + b.y);
Options
β€’ 3
β€’ 1
β€’ 2
β€’ undefined

Hint : The parent sets one property, the child adds the other.

Exercise 19 β€” Quiz: Extend and Call

Make `B` inherit from `A` and run the parent's constructor first.

Hint : One keyword links the classes, another runs the parent's constructor.

Exercise 20 β€” Quiz: Override

What does this code print?

class A { hi() { return "A"; } } class B extends A { hi() { return "B"; } } console.log(new B().hi());
Options
β€’ B
β€’ A
β€’ AB
β€’ An error

Hint : The child's version wins for the child's instances.

Exercise 21 β€” Quiz: super.method()

What does this code print?

class A { hi() { return "A"; } } class B extends A { hi() { return super.hi() + "B"; } } console.log(new B().hi());
Options
β€’ AB
β€’ B
β€’ A
β€’ BA

Hint : The child runs the parent's version first, then adds its own.

Exercise 22 β€” Quiz: instanceof the Parent

What does this code print?

class A {} class B extends A {} const b = new B(); console.log(b instanceof A);
Options
β€’ true
β€’ false
β€’ undefined
β€’ An error

Hint : A child instance is also an instance of the parent.

Exercise 23 β€” Quiz: super Before this

In a child constructor, what must happen before using `this`?

Options
β€’ Calling super(...)
β€’ Calling new
β€’ Returning this
β€’ Nothing special

Hint : The object isn't ready until the parent has built its part.

Exercise 24 β€” Quiz: Why a Class?

What is the main reason to use a class instead of writing object literals by hand?

Options
β€’ One blueprint, many consistent objects
β€’ Classes run faster
β€’ Objects can't hold methods
β€’ It avoids using this

Hint : Think about repeating the same shape many times.

Exercise 25 β€” Quiz: Capital Letter

Why are class names written with a capital letter?

Options
β€’ A convention that signals 'use new with this'
β€’ JavaScript requires it
β€’ It makes them global
β€’ It enables inheritance

Hint : It's about what the reader expects, not what the language enforces.

Back to the course