Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: Valid Object

Which line creates a valid object?

Options
β€’ const c = { name: "Zap", level: 3 };
β€’ const c = [ name: "Zap", level: 3 ];
β€’ const c = { name = "Zap", level = 3 };
β€’ const c = { "Zap", 3 };

Hint : Curly braces, and a colon between each key and value.

Exercise 2 β€” Quiz: Dot Read

What does this code print?

const zap = { name: "Zap", level: 3 }; console.log(zap.level);
Options
β€’ 3
β€’ "Zap"
β€’ "level"
β€’ undefined

Hint : The key after the dot selects the value.

Exercise 3 β€” Quiz: Bracket Read

What does this code print?

const zap = { name: "Zap", level: 3 }; console.log(zap["name"]);
Options
β€’ "Zap"
β€’ undefined
β€’ "name"
β€’ An error

Hint : Brackets with a string do the same as the dot.

Exercise 4 β€” Quiz: Variable Key

What does this code print?

const zap = { hp: 45, speed: 30 }; let stat = "speed"; console.log(zap[stat]);
Options
β€’ 30
β€’ 45
β€’ undefined
β€’ "speed"

Hint : The brackets look inside the variable to find the key.

Exercise 5 β€” Quiz: Dot and Variable

What does this code print?

const zap = { hp: 45, speed: 30 }; let stat = "speed"; console.log(zap.stat);
Options
β€’ undefined
β€’ 30
β€’ 45
β€’ An error

Hint : The dot takes the word literally.

Exercise 6 β€” Quiz: Read Both Ways

Print the name with the dot and the level with the brackets.

Hint : Two notations, same result.

Exercise 7 β€” Quiz: Missing Property

What does this code print?

const zap = { name: "Zap" }; console.log(zap.hp);
Options
β€’ undefined
β€’ 0
β€’ null
β€’ An error

Hint : A key that isn't there gives the usual "nothing" value.

Exercise 8 β€” Quiz: Modified

What does this code print?

const zap = { level: 3 }; zap.level += 2; console.log(zap.level);
Options
β€’ 5
β€’ 3
β€’ 2
β€’ undefined

Hint : The property is updated in place.

Exercise 9 β€” Quiz: Created

What does this code print?

const zap = { name: "Zap" }; zap.hp = 45; console.log(zap.hp);
Options
β€’ 45
β€’ undefined
β€’ An error
β€’ "hp"

Hint : Assigning to a new key adds it.

Exercise 10 β€” Quiz: Add a Nickname

Add a `nickname` property worth `"Zappy"`.

Hint : Same syntax as changing an existing property.

Exercise 11 β€” Quiz: Deleted

What does this code print?

const zap = { name: "Zap", bonus: 10 }; delete zap.bonus; console.log(zap.bonus);
Options
β€’ undefined
β€’ 10
β€’ 0
β€’ An error

Hint : After deletion, the key is simply absent.

Exercise 12 β€” Quiz: Key Exists?

What does this code print?

const zap = { name: "Zap", bonus: 10 }; delete zap.bonus; console.log("bonus" in zap);
Options
β€’ false
β€’ true
β€’ undefined
β€’ 10

Hint : This operator answers whether the key is still there.

Exercise 13 β€” Quiz: Method Call

Which line runs the `speak` method of `aquachu`?

Options
β€’ aquachu.speak();
β€’ aquachu.speak;
β€’ speak(aquachu);
β€’ aquachu[speak];

Hint : Through the object, with parentheses.

Exercise 14 β€” Quiz: Method Result

What does this code print?

const calc = { add(a, b) { return a + b; } }; console.log(calc.add(4, 6));
Options
β€’ 10
β€’ undefined
β€’ 46
β€’ a + b

Hint : A method returns like any function.

Exercise 15 β€” Quiz: Declare a Method

Give the object a `greet` method that prints `Hi!`, then call it.

Hint : Short syntax: just the name and parentheses in the object.

Exercise 16 β€” Quiz: this Reads

What does this code print?

const zap = { name: "Zap", intro() { return "I am " + this.name; } }; console.log(zap.intro());
Options
β€’ I am Zap
β€’ I am this.name
β€’ I am undefined
β€’ An error

Hint : Inside the method, this is the object before the dot.

Exercise 17 β€” Quiz: this Writes

What does the last line print?

const zap = { level: 1, up() { this.level += 3; } }; zap.up(); console.log(zap.level);
Options
β€’ 4
β€’ 1
β€’ 3
β€’ undefined

Hint : The method changes the object's own property.

Exercise 18 β€” Quiz: Use this

Complete the method so it works for any object it belongs to.

Hint : Don't name the variable inside the method.

Exercise 19 β€” Quiz: for...in Gives

What does this code print?

const stats = { hp: 45, speed: 30 }; for (let k in stats) { console.log(k); }
Options
β€’ hp then speed
β€’ 45 then 30
β€’ 0 then 1
β€’ undefined

Hint : This loop visits names, not values.

Exercise 20 β€” Quiz: Value in the Loop

Inside `for (let k in stats)`, which expression gives the value of the current property?

Options
β€’ stats[k]
β€’ stats.k
β€’ stats["k"]
β€’ k.stats

Hint : The key is in a variable.

Exercise 21 β€” Quiz: Sum the Stats

Add every value of the object into `total`.

Hint : The object loop keyword, then the loop variable as the key.

Exercise 22 β€” Quiz: Nested Read

What does this code print?

const trainer = { starter: { name: "Zap", level: 3 } }; console.log(trainer.starter.name);
Options
β€’ "Zap"
β€’ undefined
β€’ "starter"
β€’ An error

Hint : One dot per level.

Exercise 23 β€” Quiz: Object in Array

What does this code print?

const team = [ { name: "Aquachu" }, { name: "Pyrochu" } ]; console.log(team[1].name);
Options
β€’ "Pyrochu"
β€’ "Aquachu"
β€’ undefined
β€’ 1

Hint : First the index, then the property.

Exercise 24 β€” Quiz: Broken Chain

What happens on the last line?

const trainer = { name: "Sam" }; console.log(trainer.rival.name);
Options
β€’ An error
β€’ undefined
β€’ "Sam"
β€’ Nothing

Hint : Reading a property of undefined is not allowed.

Exercise 25 β€” Quiz: typeof Object

What does this code print?

const zap = { name: "Zap" }; console.log(typeof zap);
Options
β€’ "object"
β€’ "zap"
β€’ "array"
β€’ "string"

Hint : Same family as arrays.

Back to the course