Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: Valid Declaration

Which line correctly declares a function?

Options
β€’ function heal() {
β€’ function heal {
β€’ heal() function {
β€’ def heal() {

Hint : Keyword, name, parentheses, block: in that order.

Exercise 2 β€” Quiz: Never Called

What does this code print?

function roar() { console.log("Roar!"); }
Options
β€’ Nothing
β€’ Roar!
β€’ undefined
β€’ An error

Hint : Declaring and running are two different steps.

Exercise 3 β€” Quiz: Count the Lines

How many lines are printed?

function step() { console.log("Step"); } for (let i = 0; i < 2; i++) { step(); } step(); step();
Options
β€’ 4
β€’ 2
β€’ 3
β€’ 1

Hint : Add the calls inside the loop to the ones outside.

Exercise 4 β€” Quiz: Missing Parentheses

What does this code print?

function hello() { console.log("Hello!"); } hello; hello();
Options
β€’ Hello! once
β€’ Hello! twice
β€’ Nothing
β€’ An error

Hint : Only one of the two lines is an actual call.

Exercise 5 β€” Quiz: Parameter or Argument

In `function attack(power)`, what is `power`?

Options
β€’ A parameter
β€’ An argument
β€’ A return value
β€’ A global variable

Hint : This word appears in the declaration.

Exercise 6 β€” Quiz: Pass the Name

Complete the function so it greets whoever is passed to it.

Hint : The same name is declared in the parentheses and used in the body.

Exercise 7 β€” Quiz: Argument Order

What does this code print?

function show(first, second) { console.log(second + " / " + first); } show("A", "B");
Options
β€’ B / A
β€’ A / B
β€’ first / second
β€’ undefined / undefined

Hint : Map each argument to its position, then read the body carefully.

Exercise 8 β€” Quiz: Nothing Passed

What does this code print?

function tag(name) { console.log("[" + name + "]"); } tag();
Options
β€’ [undefined]
β€’ []
β€’ [name]
β€’ An error

Hint : A parameter without an argument still exists, with a default value.

Exercise 9 β€” Quiz: Too Many

What does this code print?

function one(x) { console.log(x); } one(7, 8, 9);
Options
β€’ 7
β€’ 9
β€’ 7 8 9
β€’ An error

Hint : Extra arguments have nowhere to go.

Exercise 10 β€” Quiz: Stored Result

What does this code print?

function triple(n) { return n * 3; } let r = triple(4); console.log(r);
Options
β€’ 12
β€’ undefined
β€’ n * 3
β€’ 4

Hint : The call is replaced by what the function returns.

Exercise 11 β€” Quiz: Give It Back

Complete the function so that the call returns the sum.

Hint : Printing and returning are two different things.

Exercise 12 β€” Quiz: No return

What does this code print?

function shout(word) { console.log(word + "!"); } let result = shout("Go"); console.log(result);
Options
β€’ Go! then undefined
β€’ Go! twice
β€’ Go! only
β€’ undefined only

Hint : The function prints, but what does it return?

Exercise 13 β€” Quiz: Early Exit

What does this code print?

function check(hp) { if (hp <= 0) { return "KO"; } return "OK"; } console.log(check(0));
Options
β€’ KO
β€’ OK
β€’ KO then OK
β€’ undefined

Hint : The first return that runs ends everything.

Exercise 14 β€” Quiz: Unreachable

What does this code print?

function value() { return 1; console.log("after"); } console.log(value());
Options
β€’ 1
β€’ after then 1
β€’ after
β€’ An error

Hint : What happens to lines placed after a return?

Exercise 15 β€” Quiz: Is It Ready?

Complete the function so it answers whether the level is at least 5, then use it.

Hint : "At least 5" includes 5, and the answer must come back to the condition.

Exercise 16 β€” Quiz: Inside Out

What does this code print?

function twice(n) { return n * 2; } console.log(twice(twice(3)));
Options
β€’ 12
β€’ 6
β€’ 9
β€’ 3

Hint : Solve the inner call first.

Exercise 17 β€” Quiz: Local Variable

What happens on the last line?

function pick() { let choice = "Pyrochu"; } pick(); console.log(choice);
Options
β€’ An error
β€’ Pyrochu
β€’ undefined
β€’ Nothing

Hint : Where does a variable declared in a function live?

Exercise 18 β€” Quiz: Reading a Global

What does this code print?

let hero = "Zap"; function show() { console.log(hero); } show();
Options
β€’ Zap
β€’ undefined
β€’ An error
β€’ hero

Hint : Functions can look outward.

Exercise 19 β€” Quiz: Shadowed

What does the last line print?

let count = 1; function bump() { let count = 100; } bump(); console.log(count);
Options
β€’ 1
β€’ 100
β€’ undefined
β€’ An error

Hint : A let inside the function creates its own variable.

Exercise 20 β€” Quiz: Really Modified

What does the last line print?

let count = 1; function bump() { count = 100; } bump(); console.log(count);
Options
β€’ 100
β€’ 1
β€’ undefined
β€’ An error

Hint : No let this time: the assignment targets an existing variable.

Exercise 21 β€” Quiz: Call It Three Times

Make the function run once per turn of the loop.

Hint : Name, then the sign that triggers the call.

Exercise 22 β€” Quiz: Parameter Scope

What happens with this code?

function heal(amount) { console.log(amount); } heal(10); console.log(amount);
Options
β€’ 10 then an error
β€’ 10 twice
β€’ 10 then undefined
β€’ Nothing

Hint : A parameter only exists while its function runs.

Exercise 23 β€” Quiz: Which Runs First?

What does this code print?

function inner() { console.log("inner"); } function outer() { console.log("outer"); inner(); } outer();
Options
β€’ outer then inner
β€’ inner then outer
β€’ outer only
β€’ inner only

Hint : Follow the body of the called function line by line.

Exercise 24 β€” Quiz: Called Early

What does this code print?

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

Hint : JavaScript reads declarations before running anything.

Exercise 25 β€” Quiz: Using the Result

What does this code print?

function bonus(level) { return level * 10; } console.log(bonus(2) + bonus(3));
Options
β€’ 50
β€’ 5
β€’ 2030
β€’ undefined

Hint : Each call becomes a number before the addition.

Back to the course