Javascript · Objects
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 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.
const zap = { name: "Zap", level: 3 };
console.log(zap____name);
console.log(zap____"level"____);
Options: . [ ] ( )
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"`.
const zap = { name: "Zap" };
zap.____ ____ "Zappy";
console.log(zap.nickname);
Options: nickname = "nickname" === :
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.
const guide = {
____() {
console.log("Hi!");
}
};
guide.____();
Options: greet greet function "greet"
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.
const zap = {
hp: 40,
heal(amount) {
____.hp += amount;
}
};
zap.heal(10);
console.log(zap.hp);
Options: this zap self object
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`.
const stats = { hp: 45, attack: 20 };
let total = 0;
for (let key ____ stats) {
total += stats[____];
}
console.log(total);
Options: in key of "key"
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