MCQs on Error Handling and Debugging | JavaScript Intermediate

Mastering error handling and debugging is crucial for developers to build reliable applications. This chapter covers try-catch statements, error types, debugging with consoles, and browser debugging tools.


Error Handling and Debugging


Topic: Try-Catch Statements

  1. What does the try block in a try...catch statement do?
    • A) Executes code and logs errors automatically
    • B) Defines a block of code to be tested for errors
    • C) Suppresses all errors in the code
    • D) Executes only if no errors occur
  2. Which block handles the error in a try...catch statement?
    • A) Try
    • B) Catch
    • C) Finally
    • D) Error
  3. What happens if an error occurs in the try block?
    • A) The error is ignored
    • B) The program stops executing
    • C) The catch block is executed
    • D) The finally block executes
  4. Which of the following is a valid syntax for try...catch in JavaScript?
    • A) try { code } catch { error }
    • B) try { code } catch (error) { handleError }
    • C) try (code) catch { error }
    • D) try { code } error (catch)
  5. What is the purpose of the finally block in try...catch?
    • A) To execute if no error occurs
    • B) To clean up resources regardless of errors
    • C) To suppress errors
    • D) To log errors only
  6. Can a try...catch statement have only a try block?
    • A) Yes, it is mandatory
    • B) No, a catch or finally block is required
    • C) Yes, but only in strict mode
    • D) No, it requires an error object
  7. In a try...catch statement, what type of errors does the catch block handle?
    • A) Logical errors
    • B) Syntax errors
    • C) Runtime errors
    • D) All errors
  8. What will happen if there is an error in the catch block?
    • A) It is ignored silently
    • B) It stops program execution
    • C) It triggers another error
    • D) It is caught by the browser’s error handler
  9. Which keyword is used to manually throw an error in JavaScript?
    • A) throw
    • B) raise
    • C) error
    • D) halt
  10. What should you do if you want to log a custom error message inside a catch block?
    • A) Use console.log with the error message
    • B) Add a finally block with the message
    • C) Use the throw statement
    • D) Write the message in the try block

Topic: Error Types (SyntaxError, ReferenceError, TypeError)

  1. What is a SyntaxError in JavaScript?
    • A) An error due to an invalid syntax in the code
    • B) An error occurring during runtime
    • C) An error caused by incorrect data types
    • D) A logical error in the code
  2. When does a ReferenceError typically occur?
    • A) When a variable is not declared or out of scope
    • B) When there is a syntax issue
    • C) When an operation fails
    • D) When accessing invalid object properties
  3. Which of these is an example of a TypeError?
    • A) let x = ;
    • B) const a = undefined.a;
    • C) console.log(variable);
    • D) if (x = 10) {}
  4. What type of error occurs if you try to assign a value to a constant variable?
    • A) ReferenceError
    • B) SyntaxError
    • C) TypeError
    • D) LogicError
  5. What is a common cause of SyntaxError?
    • A) Using a reserved keyword incorrectly
    • B) Using an undefined variable
    • C) Calling a function with wrong parameters
    • D) Accessing a null property
  6. Which error will this code throw? console.log(undefinedVariable);
    • A) SyntaxError
    • B) TypeError
    • C) ReferenceError
    • D) None
  7. Which error type is associated with attempting to use an object like a function?
    • A) ReferenceError
    • B) SyntaxError
    • C) TypeError
    • D) None
  8. Which of the following code snippets will result in a SyntaxError?
    • A) let x = 5;
    • B) if (true {});
    • C) console.log('Hello');
    • D) var y = [];
  9. How can you handle a ReferenceError effectively?
    • A) By using the typeof operator to check variable existence
    • B) By rewriting the code
    • C) By adding a finally block
    • D) By avoiding global variables
  10. What is the output of console.log(5 + "five");?
    • A) 10
    • B) SyntaxError
    • C) ReferenceError
    • D) 5five

Topic: Using Console for Debugging

  1. Which method logs an object in a table format?
    • A) console.debug
    • B) console.table
    • C) console.log
    • D) console.group
  2. What is the use of console.error() in debugging?
    • A) To log errors with stack traces
    • B) To clear the console
    • C) To display information
    • D) To log only syntax errors
  3. What is the default log level in console.log()?
    • A) Error
    • B) Debug
    • C) Info
    • D) Warn
  4. Which console method stops logging messages until console.groupEnd() is called?
    • A) console.groupCollapsed
    • B) console.pause
    • C) console.error
    • D) console.warn
  5. How do you measure the time taken for a block of code using the console?
    • A) console.time and console.timeEnd
    • B) console.start and console.stop
    • C) console.group and console.groupEnd
    • D) console.profile

Topic: Using Debugger Tools in Browsers

  1. What is the purpose of the debugger keyword in JavaScript?
    • A) To throw a debugging exception
    • B) To pause code execution during debugging
    • C) To highlight code errors
    • D) To log variables
  2. Which of the following is a browser debugging tool?
    • A) Visual Studio Code
    • B) Chrome DevTools
    • C) Postman
    • D) npm
  3. In Chrome DevTools, which tab allows you to set breakpoints?
    • A) Console
    • B) Sources
    • C) Elements
    • D) Network
  4. What is the use of a breakpoint in debugging?
    • A) To highlight errors
    • B) To pause execution at a specific line
    • C) To fix runtime issues
    • D) To analyze stack traces
  5. How do you inspect variables in real-time using browser debugging tools?
    • A) By using the Network tab
    • B) By hovering over the variable in the Sources tab
    • C) By writing the variable name in the Console tab
    • D) Both B and C

Answers Table

QnoAnswer
1B) Defines a block of code to be tested for errors
2B) Catch
3C) The catch block is executed
4B) try { code } catch (error) { handleError }
5B) To clean up resources regardless of errors
6B) No, a catch or finally block is required
7C) Runtime errors
8C) It triggers another error
9A) throw
10A) Use console.log with the error message
11A) An error due to an invalid syntax in the code
12A) When a variable is not declared or out of scope
13B) const a = undefined.a;
14C) TypeError
15A) Using a reserved keyword incorrectly
16C) ReferenceError
17C) TypeError
18B) if (true {});
19A) By using the typeof operator to check variable existence
20D) 5five
21B) console.table
22A) To log errors with stack traces
23C) Info
24A) console.groupCollapsed
25A) console.time and console.timeEnd
26B) To pause code execution during debugging
27B) Chrome DevTools
28B) Sources
29B) To pause execution at a specific line
30D) Both B and C

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top