Debugging basics and error handling JavaScript

Last Updated :
Discuss
Comments

Question 1

Which of the following is the correct way to log a variable in JavaScript?

  • A

    debug(x);


  • B

    console.print(x);

  • C

    log(x);

  • D

    console.log(x);

Question 2

What kind of error is this?

JavaScript
console.log("Hello);


  • A

    Runtime Error

  • B

    Syntax Error

  • C

    Logical Error

  • D

    Reference Error

Question 3

What will the following code log?

JavaScript
let x = 0;
if (x = 10) {
  console.log("x is 10");
}


  • A

    SyntaxError

  • B

    "x is 10"

  • C

    Nothing

  • D

    ReferenceError

Question 4

What will the following code output?

JavaScript
let name;
console.log(name.length);


  • A

    undefined

  • B

    0

  • C

    TypeError

  • D

    NaN

Question 5

What is the output of this code?

JavaScript
try {
  throw new Error("Something went wrong");
} catch (e) {
  console.log(e.message);
}


  • A

    Error

  • B

    false

  • C

    undefined

  • D

    Something went wrong

Question 6

What does the finally block do in a try-catch-finally structure?


  • A

    Always runs

  • B

    Runs only if catch executes

  • C

    Only runs if there's an error

  • D

    Only runs if no error occurs

Question 7

Which is a common mistake with switch-case statements in JS?

  • A

    Using var inside case

  • B

    Forgetting default

  • C

    Using == instead of ===

  • D

    Using numbers in cases

Question 8

How can you avoid floating point precision errors in JavaScript?

  • A

    Round every result

  • B

    Multiply and divide integers

  • C

    Use BigInt

  • D

    Avoid using decimals

Question 9

What is wrong with the following code?

JavaScript
let obj = {};
if (obj === null && typeof obj !== "undefined") {
  console.log("Valid");
}


  • A

    Nothing

  • B

    obj is null

  • C

    Order of conditions is incorrect

  • D

    obj is undefined

Question 10

What is the output?

JavaScript
try {
  console.log("try");
} catch (e) {
  console.log("catch");
} finally {
  console.log("finally");
}


  • A

    try

  • B

    try finally

  • C

    catch

  • D

    finally only

There are 10 questions to complete.

Take a part in the ongoing discussion