Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: Nothing Ran

A program prints nothing at all, not even its first line. Which kind of error is it?

Options
β€’ Syntax error
β€’ Runtime error
β€’ Logic error
β€’ No error

Hint : One kind stops the program before the first instruction.

Exercise 2 β€” Quiz: Crashed Halfway

A program prints three lines, then stops with red text. Which kind of error is it?

Options
β€’ Runtime error
β€’ Syntax error
β€’ Logic error
β€’ No error

Hint : Something ran before the crash.

Exercise 3 β€” Quiz: Wrong but Silent

A program runs to the end with no message, but the final score is wrong. Which kind of error is it?

Options
β€’ Logic error
β€’ Runtime error
β€’ Syntax error
β€’ Range error

Hint : No red text does not mean correct code.

Exercise 4 β€” Quiz: What Is undefined?

The message says `Cannot read properties of undefined (reading 'hp')`. What is `undefined`?

console.log(team[4].hp);
Options
β€’ team[4]
β€’ hp
β€’ team
β€’ console

Hint : Look at what sits right before the property named in the message.

Exercise 5 β€” Quiz: Not Defined

You see `levl is not defined`. What is the most likely cause?

Options
β€’ A misspelled variable name
β€’ A negative number
β€’ A missing bracket
β€’ An empty array

Hint : The message names something that was searched for and never found.

Exercise 6 β€” Quiz: TypeError

Which error type does this produce?

const n = 5; n();
Options
β€’ TypeError
β€’ ReferenceError
β€’ RangeError
β€’ SyntaxError

Hint : The variable exists but is used as something it isn't.

Exercise 7 β€” Quiz: ReferenceError

Which error type does this produce?

console.log(missingName);
Options
β€’ ReferenceError
β€’ TypeError
β€’ RangeError
β€’ SyntaxError

Hint : The name was never declared anywhere.

Exercise 8 β€” Quiz: RangeError

Which error type does this produce?

"#".repeat(-5);
Options
β€’ RangeError
β€’ TypeError
β€’ ReferenceError
β€’ SyntaxError

Hint : The number is valid, just outside what the method accepts.

Exercise 9 β€” Quiz: SyntaxError

Which line is a syntax error?

Options
β€’ const x = ;
β€’ const x = y;
β€’ const x = null.z;
β€’ const x = [].pop();

Hint : One line cannot even be read.

Exercise 10 β€” Quiz: Clamp It

Stop the count from going negative before drawing.

Hint : Catch the value when it drops too low and bring it back to the minimum.

Exercise 11 β€” Quiz: try Then Crash

What does this code print?

try { console.log("A"); undefined.x; console.log("B"); } catch (e) { console.log("C"); } console.log("D");
Options
β€’ A, C, D
β€’ A, B, C, D
β€’ A, D
β€’ C, D

Hint : A crash skips the rest of try and jumps to catch.

Exercise 12 β€” Quiz: try With No Crash

What does this code print?

try { console.log("A"); } catch (e) { console.log("B"); } console.log("C");
Options
β€’ A, C
β€’ A, B, C
β€’ B, C
β€’ A only

Hint : catch only runs when something went wrong.

Exercise 13 β€” Quiz: Protect It

Wrap the risky line so the program survives.

Hint : One keyword opens the protected area, the other names what runs on failure.

Exercise 14 β€” Quiz: The Error Object

Inside `catch (error)`, which property holds the type of the error?

Options
β€’ error.name
β€’ error.message
β€’ error.type
β€’ error.kind

Hint : One property is the family, the other the explanation.

Exercise 15 β€” Quiz: finally After a Crash

What does this code print?

try { undefined.x; } catch (e) { console.log("B"); } finally { console.log("C"); }
Options
β€’ B, C
β€’ B only
β€’ C only
β€’ Nothing

Hint : Both remaining blocks run after a crash.

Exercise 16 β€” Quiz: finally Without a Crash

What does this code print?

try { console.log("A"); } catch (e) { console.log("B"); } finally { console.log("C"); }
Options
β€’ A, C
β€’ A, B, C
β€’ A only
β€’ C only

Hint : One block is skipped when all goes well, the other never is.

Exercise 17 β€” Quiz: finally vs return

What does this code print?

function f() { try { return "done"; } finally { console.log("cleanup"); } } console.log(f());
Options
β€’ cleanup then done
β€’ done then cleanup
β€’ done only
β€’ cleanup only

Hint : The cleanup runs on the way out, before the value reaches the caller.

Exercise 18 β€” Quiz: Always Close

Make the closing line run whether the write works or not.

Hint : One keyword introduces the block that always runs, and the slot must end up shut.

Exercise 19 β€” Quiz: throw Caught

What does this code print?

function f() { throw new Error("boom"); } try { f(); } catch (e) { console.log(e.message); }
Options
β€’ boom
β€’ Error
β€’ undefined
β€’ A crash

Hint : The thrown message travels to the nearest catch.

Exercise 20 β€” Quiz: After a throw

What does this code print?

function f() { console.log("A"); throw new Error("x"); console.log("B"); } try { f(); } catch (e) { console.log("C"); }
Options
β€’ A, C
β€’ A, B, C
β€’ A only
β€’ C only

Hint : A throw ends the function on the spot.

Exercise 21 β€” Quiz: Raise the Alarm

Refuse a negative amount with an error.

Hint : One keyword launches the alarm, and the built-in object carrying a message has a short name.

Exercise 22 β€” Quiz: Uncaught

A `throw` happens and no `catch` exists anywhere. What happens?

Options
β€’ The program crashes with the message
β€’ The error is ignored
β€’ The function returns null
β€’ The program loops

Hint : An error keeps travelling up until something catches it, or nothing does.

Exercise 23 β€” Quiz: Where to Validate

Where is the best place to check inputs and throw?

Options
β€’ At the very top of the function
β€’ At the very end
β€’ In a separate global variable
β€’ Inside the return statement

Hint : Refuse bad data before doing any real work with it.

Exercise 24 β€” Quiz: Small try Blocks

Why keep a `try` block small?

Options
β€’ So you know which line failed and don't skip the rest
β€’ Because big blocks are slower
β€’ Because catch only accepts one line
β€’ It makes no difference

Hint : Think about what happens to the lines after a crash inside try.

Exercise 25 β€” Quiz: Reading Order

What is the first thing to read in an error message?

Options
β€’ Its type, to know the family
β€’ The line number only
β€’ Nothing, just restart
β€’ The last word

Hint : The first word of the message tells you which kind of problem it is.

Back to the course