Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: Local Variable

What happens on the last line?

function f() { const x = 1; } f(); console.log(x);
Options
β€’ An error
β€’ 1
β€’ undefined
β€’ Nothing

Hint : Nothing outside can see inside a function.

Exercise 2 β€” Quiz: Reading a Global

What does this code print?

const x = 1; function f() { console.log(x); } f();
Options
β€’ 1
β€’ undefined
β€’ An error
β€’ x

Hint : Functions can look outward.

Exercise 3 β€” Quiz: Two Locals

What does this code print?

function a() { const x = 1; console.log(x); } function b() { const x = 2; console.log(x); } a(); b();
Options
β€’ 1 then 2
β€’ 1 twice
β€’ 2 twice
β€’ An error

Hint : Each function has its own private area.

Exercise 4 β€” Quiz: Declare It Locally

Declare a local `bonus` worth 10 and print it.

Hint : Declaring needs a keyword, and printing needs the variable itself.

Exercise 5 β€” Quiz: After the Block

What happens on the last line?

if (true) { const b = 10; } console.log(b);
Options
β€’ An error
β€’ 10
β€’ undefined
β€’ Nothing

Hint : Braces mark the borders of the variable's life.

Exercise 6 β€” Quiz: The Loop Counter

What happens on the last line?

for (let i = 0; i < 3; i++) { } console.log(i);
Options
β€’ An error
β€’ 3
β€’ 2
β€’ undefined

Hint : The counter belongs to the loop's block.

Exercise 7 β€” Quiz: Hidden by a let

What does the last line print?

let hp = 100; if (true) { let hp = 20; } console.log(hp);
Options
β€’ 100
β€’ 20
β€’ undefined
β€’ An error

Hint : A declaration inside creates a separate variable.

Exercise 8 β€” Quiz: Without a Keyword

What does the last line print?

let hp = 100; if (true) { hp = 20; } console.log(hp);
Options
β€’ 20
β€’ 100
β€’ undefined
β€’ An error

Hint : With no declaration, the assignment reaches the outer variable.

Exercise 9 β€” Quiz: Shadow It

Make the block use its own `hp`, leaving the outer one at 100.

Hint : One keyword creates a brand-new variable, and the print uses that same name.

Exercise 10 β€” Quiz: var Escapes

What does this code print?

if (true) { var x = 5; } console.log(x);
Options
β€’ 5
β€’ An error
β€’ undefined
β€’ Nothing

Hint : This keyword ignores blocks entirely.

Exercise 11 β€” Quiz: Hoisted var

What does this code print?

console.log(hp); var hp = 50;
Options
β€’ undefined
β€’ 50
β€’ An error
β€’ 0

Hint : The declaration is noted early, but not the value.

Exercise 12 β€” Quiz: let Read Too Early

What happens when this runs?

console.log(hp); let hp = 50;
Options
β€’ An error
β€’ undefined
β€’ 50
β€’ Nothing

Hint : The modern keywords refuse to be read before their line.

Exercise 13 β€” Quiz: Hoisted Function

What does this code print?

go(); function go() { console.log("ready"); }
Options
β€’ ready
β€’ An error
β€’ undefined
β€’ Nothing

Hint : A function declaration is noted before the code runs.

Exercise 14 β€” Quiz: Fresh Locals

What does this code print?

function f() { let n = 0; n++; console.log(n); } f(); f();
Options
β€’ 1 then 1
β€’ 1 then 2
β€’ 0 then 1
β€’ An error

Hint : Each call rebuilds the local variables.

Exercise 15 β€” Quiz: The Outer Counter

What does the last line print?

let n = 0; function f() { n++; } f(); f(); console.log(n);
Options
β€’ 2
β€’ 1
β€’ 0
β€’ An error

Hint : This variable is not destroyed when a call ends.

Exercise 16 β€” Quiz: The Inner Function

What happens on the last line?

function outer() { function inner() {} inner(); } outer(); inner();
Options
β€’ An error
β€’ Nothing
β€’ undefined
β€’ It runs fine

Hint : An inner function is local to the one holding it.

Exercise 17 β€” Quiz: The Closest Wins

What does this code print?

const x = "outer"; function f() { const x = "inner"; console.log(x); } f();
Options
β€’ inner
β€’ outer
β€’ undefined
β€’ An error

Hint : The search stops at the first declaration it meets.

Exercise 18 β€” Quiz: Three Levels

What does this code print?

const v = 1; function outer() { const v = 2; function inner() { console.log(v); } inner(); } outer();
Options
β€’ 2
β€’ 1
β€’ undefined
β€’ An error

Hint : Start inside and walk outward until a match appears.

Exercise 19 β€” Quiz: Nothing Found

What happens when a name exists nowhere along the chain?

Options
β€’ An error
β€’ undefined
β€’ null
β€’ Nothing

Hint : The program cannot invent a variable.

Exercise 20 β€” Quiz: Remembered Value

What does this code print?

function make(x) { return function () { return x * 2; }; } const d = make(5); console.log(d());
Options
β€’ 10
β€’ 5
β€’ undefined
β€’ An error

Hint : The returned function kept a link to the value it was built with.

Exercise 21 β€” Quiz: The Closure Counter

What does this code print?

function make() { let n = 0; return function () { n++; return n; }; } const next = make(); next(); next(); console.log(next());
Options
β€’ 3
β€’ 1
β€’ 2
β€’ 0

Hint : The remembered variable is not rebuilt at each call.

Exercise 22 β€” Quiz: Two Independent Ones

What does this code print?

function make() { let n = 0; return function () { n++; return n; }; } const a = make(); const b = make(); a(); a(); console.log(b());
Options
β€’ 1
β€’ 3
β€’ 2
β€’ 0

Hint : Each call to the factory builds its own memory.

Exercise 23 β€” Quiz: Build the Closure

Complete the factory so the built function remembers the factor.

Hint : The factory hands the inner function back, and that function uses the remembered setting.

Exercise 24 β€” Quiz: Hidden Data

What does the last line print?

function make() { let gold = 100; return { total() { return gold; } }; } const w = make(); console.log(w.gold);
Options
β€’ undefined
β€’ 100
β€’ An error
β€’ 0

Hint : Is the remembered variable a property of the returned object?

Exercise 25 β€” Quiz: Why Use a Closure?

What is the main reason to hide a value inside a closure?

Options
β€’ It can only be changed through the functions you provide
β€’ It makes the program run faster
β€’ It saves memory
β€’ It turns the value into a global

Hint : Think about who is allowed to touch the value.

Back to the course