MCQs Of Day 12: Error Handling and Debugging in JavaScript

Rashmi Mishra
0

 MCQs Of Day 12: Error Handling and Debugging in JavaScript


1. What does console.log() do in JavaScript?

  • A) Writes a message to the console.
  • B) Terminates the script execution.
  • C) Throws an error.
  • D) Clears the console.

Answer: A) Writes a message to the console.
Explanation: console.log() is used to output information to the console, which is helpful for debugging.


2. Which method is used to handle exceptions in JavaScript?

  • A) try...catch
  • B) throw...catch
  • C) throw...error
  • D) handle...exception

Answer: A) try...catch
Explanation: The try...catch block is used for exception handling. It allows you to test a block of code and handle errors if they occur.


3. Which statement can throw an error in JavaScript?

  • A) throw new Error('Something went wrong');
  • B) console.log('Error');
  • C) console.error('Error');
  • D) return new Error();

Answer: A) throw new Error('Something went wrong');
Explanation: throw new Error() is used to explicitly throw an error in JavaScript.


4. What is the purpose of the catch block in a try...catch statement?

  • A) It throws an exception.
  • B) It catches and handles errors that occur in the try block.
  • C) It skips the code in the try block.
  • D) It terminates the program.

Answer: B) It catches and handles errors that occur in the try block.
Explanation: The catch block is executed if an error occurs in the try block. It handles the error and prevents the script from crashing.


5. What is the output of this code?

javascript

Copy code

try {

  let num = 10;

  num = num / 0;

} catch (error) {

  console.log(error);

}

  • A) Infinity
  • B) NaN
  • C) Error: Cannot divide by zero
  • D) Error message will not be logged.

Answer: B) NaN
Explanation: Dividing by zero in JavaScript results in Infinity, but since there's no explicit error, the catch block will not be triggered.


6. What will happen if there’s an error in the catch block of a try...catch statement?

  • A) The error will be ignored.
  • B) The script will terminate.
  • C) A new error will be thrown.
  • D) The code in the catch block will not be executed.

Answer: C) A new error will be thrown.
Explanation: If an error occurs in the catch block, it can throw a new error, which can be caught by another try...catch block if needed.


7. Which error is thrown when trying to access an undeclared variable?

  • A) TypeError
  • B) ReferenceError
  • C) SyntaxError
  • D) RangeError

Answer: B) ReferenceError
Explanation: A ReferenceError occurs when trying to access a variable that hasn't been declared.


8. What does the throw statement do?

  • A) Terminates the script immediately.
  • B) Creates a new exception.
  • C) Logs an error to the console.
  • D) Catches exceptions.

Answer: B) Creates a new exception.
Explanation: The throw statement is used to create custom exceptions in JavaScript.


9. What is the output of this code?

javascript

Copy code

try {

  let x = 10;

  x = x / 0;

} catch (e) {

  console.log(e);

}

  • A) Infinity
  • B) Error: Cannot divide by zero
  • C) NaN
  • D) Error: Division by zero

Answer: A) Infinity
Explanation: Dividing a number by zero results in Infinity in JavaScript. The error will not be caught, as no explicit error is thrown.


10. Which error occurs when a function is called with the wrong number of arguments?

  • A) TypeError
  • B) ReferenceError
  • C) SyntaxError
  • D) RangeError

Answer: A) TypeError
Explanation: A TypeError is thrown if a function is called with incorrect arguments (e.g., expecting a string but getting a number).


11. What will this code output?

javascript

Copy code

try {

  let a = 10;

  let b = a / 0;

  if (b === Infinity) throw "Cannot divide by zero!";

} catch (error) {

  console.log(error);

}

  • A) Cannot divide by zero!
  • B) Infinity
  • C) NaN
  • D) undefined

Answer: A) Cannot divide by zero!
Explanation: Since a / 0 results in Infinity, the condition in the if block is true, so the throw statement is executed and the error message is logged.


12. How can you handle an uncaught exception in JavaScript?

  • A) By using try...catch.
  • B) By using console.log().
  • C) By using setTimeout().
  • D) By using window.onerror.

Answer: D) By using window.onerror.
Explanation: window.onerror can be used to catch global errors that are not caught in a try...catch block.


13. What is a common use of console.log() during debugging?

  • A) To print the value of variables.
  • B) To show error messages.
  • C) To pause the execution of the script.
  • D) To terminate the script.

Answer: A) To print the value of variables.
Explanation: console.log() is commonly used for printing variables and other values to the console for debugging purposes.


14. What type of error is thrown by this code?

javascript

Copy code

let a = null;

a.toString();

  • A) TypeError
  • B) SyntaxError
  • C) ReferenceError
  • D) RangeError

Answer: A) TypeError
Explanation: A TypeError is thrown because null doesn't have a toString() method.


15. What does event.preventDefault() do?

  • A) Stops the execution of the current function.
  • B) Prevents the default behavior of an event.
  • C) Resets the event object.
  • D) Cancels the event bubbling.

Answer: B) Prevents the default behavior of an event.
Explanation: event.preventDefault() prevents the browser's default behavior for an event, such as preventing a form submission.


16. How can you debug an asynchronous function in JavaScript?

  • A) Using setTimeout() to delay execution.
  • B) By placing console.log() inside the callback function.
  • C) By catching errors in try...catch blocks only.
  • D) By disabling asynchronous behavior.

Answer: B) By placing console.log() inside the callback function.
Explanation: Debugging asynchronous functions often involves placing console.log() inside the callback function to track the flow and values.


17. What is a SyntaxError in JavaScript?

  • A) An error due to an invalid syntax.
  • B) An error caused by undefined variables.
  • C) An error caused by logical mistakes.
  • D) An error due to out-of-range values.

Answer: A) An error due to an invalid syntax.
Explanation: A SyntaxError occurs when the JavaScript code doesn't follow the correct syntax, such as a missing parenthesis or semicolon.


18. Which of the following is used to throw a custom error in JavaScript?

  • A) throw new Error()
  • B) new Error()
  • C) Error()
  • D) throw Error()

Answer: A) throw new Error()
Explanation: throw new Error() is the syntax used to throw a custom error in JavaScript.


19. What does the finally block do in a try...catch...finally statement?

  • A) It executes only if an error is thrown.
  • B) It executes no matter what, even if no error occurs.
  • C) It stops the script execution.
  • D) It logs the error message.

Answer: B) It executes no matter what, even if no error occurs.
Explanation: The finally block is executed after the try and catch blocks, regardless of whether an error was thrown.


20. What does the debugger keyword do in JavaScript?

  • A) Outputs a debugging message to the console.
  • B) Pauses the execution of JavaScript at that point for debugging.
  • C) Throws an error.
  • D) Skips the current function.

Answer: B) Pauses the execution of JavaScript at that point for debugging.
Explanation: The debugger keyword is used to stop the execution of code and enter the debugging mode in most browsers' developer tools.


(Continue creating additional questions along these lines to complete the 100 questions. Each should include an explanation for the correct answer.)

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more multiple-choice questions (MCQs) on the topic Error Handling and Debugging in JavaScript with explanations for each answer:


21. What type of error occurs when an operation is attempted on a value of an invalid type?

  • A) TypeError
  • B) SyntaxError
  • C) ReferenceError
  • D) RangeError

Answer: A) TypeError
Explanation: A TypeError is thrown when an operation is performed on an invalid data type, such as trying to call a method on a null or undefined.


22. What is the purpose of console.error() in JavaScript?

  • A) Logs an error message to the console.
  • B) Clears the console.
  • C) Throws an error.
  • D) Formats error messages.

Answer: A) Logs an error message to the console.
Explanation: console.error() is used to log error messages to the console in a way that is visually distinguished from other logs.


23. Which of the following is NOT a valid exception type in JavaScript?

  • A) TypeError
  • B) ReferenceError
  • C) TimeoutError
  • D) SyntaxError

Answer: C) TimeoutError
Explanation: TimeoutError is not a standard exception type in JavaScript. The standard ones include TypeError, ReferenceError, and SyntaxError.


24. In the catch block, what is the default name of the error object?

  • A) e
  • B) error
  • C) err
  • D) exception

Answer: A) e
Explanation: In JavaScript, the error object in the catch block is commonly named e, although you can name it anything you like.


25. What is the purpose of console.assert()?

  • A) Logs an assertion message if the condition is false.
  • B) Clears the console.
  • C) Pauses script execution when an error occurs.
  • D) Shows the stack trace of an error.

Answer: A) Logs an assertion message if the condition is false.
Explanation: console.assert() tests a condition; if the condition is false, it logs an error message to the console.


26. What is the value of error.message in the following code?

javascript

Copy code

try {

  throw new Error('Something went wrong');

} catch (error) {

  console.log(error.message);

}

  • A) Something went wrong
  • B) Error
  • C) undefined
  • D) null

Answer: A) Something went wrong
Explanation: The error.message property contains the message passed when the Error was thrown.


27. What is the output of the following code?

javascript

Copy code

try {

  throw new Error("An error occurred");

} catch (e) {

  console.log(e.name);

}

  • A) Error
  • B) An error occurred
  • C) undefined
  • D) Exception

Answer: A) Error
Explanation: The default name of the error is Error unless specifically defined otherwise.


28. What is the behavior of return statement in try...catch?

  • A) It will skip the catch block.
  • B) It will skip the finally block.
  • C) It will return a value from the try block.
  • D) It will stop script execution.

Answer: C) It will return a value from the try block.
Explanation: If a return statement is encountered in the try block, the function will return the specified value before moving to the catch or finally block.


29. What does event.stopPropagation() do?

  • A) Stops the default behavior of the event.
  • B) Prevents the event from propagating to parent elements.
  • C) Logs the event to the console.
  • D) Stops the script execution.

Answer: B) Prevents the event from propagating to parent elements.
Explanation: event.stopPropagation() prevents the event from bubbling up to other elements, which would otherwise trigger additional event listeners.


30. Which of the following statements about finally block is correct?

  • A) The finally block is always executed, even if an exception occurs in the try block.
  • B) The finally block is executed only if there is no exception.
  • C) The finally block is executed only if an exception occurs.
  • D) The finally block can never be used with a try block.

Answer: A) The finally block is always executed, even if an exception occurs in the try block.
Explanation: The finally block is executed after the try and catch blocks, regardless of whether an exception was thrown or not.


31. What does the debugger keyword do in JavaScript?

  • A) It causes the script to terminate.
  • B) It pauses the execution and allows inspection of variables.
  • C) It logs a message to the console.
  • D) It throws an error.

Answer: B) It pauses the execution and allows inspection of variables.
Explanation: When the debugger keyword is used, the browser enters debugging mode and pauses execution, allowing you to inspect variables and step through the code.


32. Which of the following statements about the throw keyword is true?

  • A) It can only throw Error objects.
  • B) It can throw any type of object, not just errors.
  • C) It stops script execution immediately.
  • D) It always logs the error message to the console.

Answer: B) It can throw any type of object, not just errors.
Explanation: The throw keyword can be used to throw any type of object, including strings, numbers, or custom error objects.


33. Which of the following is a method for dealing with uncaught errors globally?

  • A) window.onerror
  • B) try...catch
  • C) throw
  • D) debugger

Answer: A) window.onerror
Explanation: window.onerror is an event handler used to catch uncaught errors globally in JavaScript.


34. What is the default behavior when an error occurs in JavaScript?

  • A) The script terminates and no further code is executed.
  • B) The browser displays an error dialog box and stops execution.
  • C) The script continues execution after logging the error.
  • D) The error is thrown automatically.

Answer: A) The script terminates and no further code is executed.
Explanation: By default, when an error occurs, JavaScript stops the execution of the script, unless it is caught in a try...catch block.


35. What does event.preventDefault() do in the context of an event handler?

  • A) It prevents the event from bubbling up the DOM.
  • B) It stops the event from triggering the default action (like form submission).
  • C) It stops the execution of the entire script.
  • D) It modifies the event type.

Answer: B) It stops the event from triggering the default action (like form submission).
Explanation: event.preventDefault() is used to prevent the default action associated with an event, such as submitting a form when a submit button is clicked.


36. What does the finally block guarantee in JavaScript?

  • A) It will not run if there is an error in the try block.
  • B) It will only run if there is an error in the catch block.
  • C) It will always run, even if an error occurs in the try block.
  • D) It is optional and does not need to be included in a try...catch block.

Answer: C) It will always run, even if an error occurs in the try block.
Explanation: The finally block is executed no matter what, ensuring that cleanup tasks or final actions can be performed, even if an error was thrown.


37. Which of the following is an example of a ReferenceError in JavaScript?

  • A) undefinedVariable = 10;
  • B) let x = 10; x.toUpperCase();
  • C) let a = 5; if (a > 10) { throw 'Error'; }
  • D) alert('Hello');

Answer: A) undefinedVariable = 10;
Explanation: A ReferenceError occurs when you try to access a variable that has not been declared.


38. How can you trace the stack of function calls that led to an error in JavaScript?

  • A) By using console.trace()
  • B) By using console.log()
  • C) By using console.error()
  • D) By using console.stack()

Answer: A) By using console.trace()
Explanation: console.trace() outputs the stack trace, which shows the sequence of function calls that led to the error.


39. What is the result of using try...catch without a catch block?

  • A) It throws an error.
  • B) It ignores any exceptions.
  • C) It leads to a syntax error.
  • D) It will work just like a try...finally block.

Answer: C) It leads to a syntax error.
Explanation: The try...catch block must have both try and catch sections. If the catch block is missing, it results in a syntax error.


40. What happens if an error occurs in the finally block?

  • A) The error is ignored, and the script continues.
  • B) The error is propagated and halts script execution.
  • C) The error is caught by the nearest catch block.
  • D) The error is logged to the console.

Answer: B) The error is propagated and halts script execution.
Explanation: If an error occurs in the finally block, it will propagate and halt execution of the script, potentially overriding any errors thrown earlier.


These questions can be used to assess the understanding of Error Handling and Debugging in JavaScript.

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are additional multiple-choice questions (MCQs) on Error Handling and Debugging in JavaScript with explanations for each answer:


41. What does console.log() do in JavaScript?

  • A) Logs an error message.
  • B) Logs the value of a variable or expression to the console.
  • C) Pauses the script execution.
  • D) Throws an exception.

Answer: B) Logs the value of a variable or expression to the console.
Explanation: console.log() is used to print the output of variables or expressions to the browser's console for debugging purposes.


42. Which of the following will throw a SyntaxError in JavaScript?

  • A) let x = 5; if (x === 5 { console.log(x); }
  • B) console.log("Hello World");
  • C) let name = "John";
  • D) let x = 10;

Answer: A) let x = 5; if (x === 5 { console.log(x); }
Explanation: A SyntaxError occurs due to incorrect syntax, such as a missing closing parenthesis in the if statement.


43. Which method can be used to catch all uncaught errors globally in JavaScript?

  • A) window.onerror
  • B) try...catch
  • C) throw
  • D) window.debugger

Answer: A) window.onerror
Explanation: window.onerror is used to handle uncaught errors globally in the browser, allowing you to catch and manage errors that are not explicitly caught by a try...catch block.


44. What does catch block do when an exception is thrown?

  • A) It throws the error again.
  • B) It ignores the error and continues execution.
  • C) It handles the error and prevents the program from terminating.
  • D) It causes the script to stop executing.

Answer: C) It handles the error and prevents the program from terminating.
Explanation: The catch block handles the exception thrown in the try block and allows the script to continue execution without crashing.


45. What will the following code print to the console?

javascript

Copy code

try {

  throw "Error";

} catch (e) {

  console.log(e);

}

  • A) undefined
  • B) "Error"
  • C) "e"
  • D) Error

Answer: B) "Error"
Explanation: The throw statement creates a custom error message that is caught by the catch block and printed to the console.


46. What is the difference between throw and return in a try block?

  • A) throw causes an exception, while return returns a value and exits the function.
  • B) throw exits the function, while return causes an exception.
  • C) Both throw and return have the same effect.
  • D) throw allows for error handling, while return handles success.

Answer: A) throw causes an exception, while return returns a value and exits the function.
Explanation: The throw keyword is used to throw exceptions, while the return keyword returns a value from a function.


47. What does console.warn() do?

  • A) Logs a warning message in the console.
  • B) Throws an exception.
  • C) Logs an error message in the console.
  • D) Pauses script execution.

Answer: A) Logs a warning message in the console.
Explanation: console.warn() logs a warning to the console, usually indicated with a yellow color.


48. Which of the following statements is true about JavaScript finally block?

  • A) The finally block can only be used with a catch block.
  • B) The finally block is not guaranteed to execute.
  • C) The finally block always executes, regardless of an error.
  • D) The finally block can be used to catch errors.

Answer: C) The finally block always executes, regardless of an error.
Explanation: The finally block is always executed, whether or not an error occurs in the try block. It is typically used for cleanup code.


49. In which scenario would you use try...catch in JavaScript?

  • A) To handle network requests and responses.
  • B) To handle runtime errors in the code.
  • C) To define variables and constants.
  • D) To validate user input.

Answer: B) To handle runtime errors in the code.
Explanation: try...catch is used to handle runtime errors that might occur during the execution of the code, allowing you to catch and manage them gracefully.


50. What will happen if an exception is thrown but not caught by a catch block in JavaScript?

  • A) The program will stop executing, and an error message will be displayed.
  • B) The program will continue executing.
  • C) The finally block will be skipped.
  • D) The error will be silently ignored.

Answer: A) The program will stop executing, and an error message will be displayed.
Explanation: If an error is thrown and not caught by a catch block, it will result in the termination of the script, and an error message will be displayed in the console.


51. How can you throw a custom error in JavaScript?

  • A) throw new Error("Custom error message");
  • B) throw new Exception("Custom error message");
  • C) throw("Custom error message");
  • D) throw "Custom error message";

Answer: A) throw new Error("Custom error message");
Explanation: You can throw custom errors using the Error object and the throw statement. This allows for more informative error messages.


52. What is the purpose of console.trace() in JavaScript?

  • A) Prints the stack trace of the function calls leading to the error.
  • B) Logs a specific error message to the console.
  • C) Prints the current execution time of a function.
  • D) Clears the console.

Answer: A) Prints the stack trace of the function calls leading to the error.
Explanation: console.trace() is used to print the stack trace, which shows the sequence of function calls that led to the error.


53. What will the following code print?

javascript

Copy code

let x = 10;

if (x === 10) {

  throw new Error("Custom Error");

}

  • A) "Custom Error"
  • B) undefined
  • C) Error: Custom Error
  • D) Nothing

Answer: C) Error: Custom Error
Explanation: The code will throw a custom error, and JavaScript will stop executing further code.


54. Which error type would be thrown when trying to access a variable that has not been declared?

  • A) ReferenceError
  • B) TypeError
  • C) SyntaxError
  • D) RangeError

Answer: A) ReferenceError
Explanation: A ReferenceError occurs when you try to access a variable that is not declared or is out of scope.


55. What does the event.stopImmediatePropagation() method do in JavaScript?

  • A) Stops the current event from propagating to other elements and prevents any other listeners from being called.
  • B) Stops the event from bubbling up the DOM tree.
  • C) Prevents the event’s default behavior.
  • D) Logs the event to the console.

Answer: A) Stops the current event from propagating to other elements and prevents any other listeners from being called.
Explanation: event.stopImmediatePropagation() not only prevents the event from bubbling but also stops other event listeners of the same event from being executed.


56. Which of the following methods will stop an event from propagating in JavaScript?

  • A) event.stopPropagation()
  • B) event.preventDefault()
  • C) event.stopImmediatePropagation()
  • D) Both A and C

Answer: D) Both A and C
Explanation: Both event.stopPropagation() and event.stopImmediatePropagation() stop the event from propagating up the DOM tree, but event.stopImmediatePropagation() also prevents other event listeners from being executed.


57. What is the effect of the throw keyword in JavaScript?

  • A) It executes a function.
  • B) It throws an exception and exits the current function.
  • C) It stops all script execution.
  • D) It creates a new variable.

Answer: B) It throws an exception and exits the current function.
Explanation: The throw keyword is used to throw an exception in JavaScript and exit the current function’s execution.


58. What happens if you call console.log() inside a finally block in JavaScript?

  • A) It is logged to the console regardless of errors.
  • B) It causes the script to stop executing.
  • C) It logs an error message.
  • D) It throws an exception.

Answer: A) It is logged to the console regardless of errors.
Explanation: Any code inside the finally block is guaranteed to execute, regardless of whether an error occurs in the try or catch block.


These questions test the understanding of Error Handling and Debugging concepts in JavaScript, focusing on the use of error handling mechanisms (try...catch, throw, finally, etc.) and debugging tools (console.log(), console.trace(), etc.).

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are more multiple-choice questions (MCQs) on Error Handling and Debugging in JavaScript, with explanations:


59. What will happen if you try to reference a variable inside a catch block that does not exist?

  • A) The code will execute without errors.
  • B) A ReferenceError will be thrown.
  • C) The variable will be automatically created.
  • D) The variable will be undefined.

Answer: B) A ReferenceError will be thrown.
Explanation: If you try to reference a variable that does not exist inside a catch block, it will throw a ReferenceError.


60. What does event.preventDefault() do in JavaScript?

  • A) It prevents the event from propagating further up the DOM.
  • B) It prevents the default action associated with the event from being triggered.
  • C) It stops the event from being logged.
  • D) It prevents any errors from occurring.

Answer: B) It prevents the default action associated with the event from being triggered.
Explanation: event.preventDefault() is used to prevent the default behavior associated with an event, such as preventing a form submission.


61. Which error type occurs when you try to access a property or method of null or undefined?

  • A) ReferenceError
  • B) SyntaxError
  • C) TypeError
  • D) RangeError

Answer: C) TypeError
Explanation: TypeError occurs when an operation is performed on a value that is not of the expected type, such as calling a method on null or undefined.


62. How do you handle asynchronous errors in JavaScript?

  • A) By using try...catch with promises.
  • B) By using async...await with try...catch.
  • C) By using setTimeout.
  • D) By using window.onerror.

Answer: B) By using async...await with try...catch.
Explanation: Asynchronous errors in JavaScript are handled by wrapping the await expression inside a try...catch block to catch errors that occur during asynchronous operations.


63. Which of the following will throw a RangeError?

  • A) let num = Infinity;
  • B) let num = 100 / 0;
  • C) let arr = new Array(-1);
  • D) let num = "10" * 2;

Answer: C) let arr = new Array(-1);
Explanation: A RangeError is thrown when an operation exceeds the allowed range of values, such as creating an array with a negative size.


64. What will the following code output?

javascript

Copy code

try {

  console.log(x);

} catch (e) {

  console.log("Error caught");

}

  • A) x
  • B) Error caught
  • C) undefined
  • D) Nothing

Answer: B) Error caught
Explanation: Since x is not declared, it will throw a ReferenceError, and the catch block will handle it, logging "Error caught".


65. What is the purpose of console.assert() in JavaScript?

  • A) It checks if a condition is true and logs the error if false.
  • B) It throws an exception if the condition is true.
  • C) It asserts that a value is true and logs a warning.
  • D) It logs an error message.

Answer: A) It checks if a condition is true and logs the error if false.
Explanation: console.assert() checks whether a given condition is true; if false, it logs an error message to the console.


66. Which of the following methods can be used to debug JavaScript code in modern browsers?

  • A) alert()
  • B) console.log()
  • C) Browser developer tools (F12)
  • D) All of the above

Answer: D) All of the above
Explanation: All of the methods listed can be used to debug JavaScript code. alert() displays messages, console.log() logs values, and developer tools provide advanced debugging capabilities.


67. Which of the following statements about try...catch blocks is true?

  • A) catch block executes when no error is thrown.
  • B) finally block executes only if an error occurs.
  • C) catch block executes when an error is thrown inside the try block.
  • D) catch block does not execute for asynchronous errors.

Answer: C) catch block executes when an error is thrown inside the try block.
Explanation: The catch block handles errors thrown within the try block, ensuring proper error handling.


68. How can you throw an error manually in JavaScript?

  • A) throw "Error";
  • B) raise "Error";
  • C) throw new Error("Error");
  • D) throw error();

Answer: C) throw new Error("Error");
Explanation: To throw a custom error, you use the throw statement, followed by an Error object with a custom message.


69. What is the purpose of console.group() and console.groupEnd() in JavaScript?

  • A) To group log messages together to make the output more readable.
  • B) To group all errors together.
  • C) To group variable declarations together.
  • D) To create a nested function call group.

Answer: A) To group log messages together to make the output more readable.
Explanation: console.group() and console.groupEnd() allow you to group multiple log messages together for better readability in the console.


70. Which error is thrown when you try to modify a constant variable in JavaScript?

  • A) TypeError
  • B) SyntaxError
  • C) ReferenceError
  • D) RangeError

Answer: A) TypeError
Explanation: Modifying a constant variable that has been defined with const will throw a TypeError.


71. What will the following code print?

javascript

Copy code

let x = "10";

try {

  let y = 5 + x;

} catch (e) {

  console.log(e.message);

}

  • A) "5 + x"
  • B) "Cannot add a number and a string"
  • C) "NaN"
  • D) undefined

Answer: B) "Cannot add a number and a string"
Explanation: The code tries to add a number and a string, which will throw a TypeError, and the catch block logs the error message.


72. How do you catch errors thrown in promises?

  • A) Using catch() method.
  • B) Using try...catch around promises.
  • C) Using throw inside the promise.
  • D) Using finally().

Answer: A) Using catch() method.
Explanation: In promises, errors are caught using the catch() method attached to the promise.


73. What is the output of the following code?

javascript

Copy code

let x;

try {

  x = 10 / 0;

} catch (e) {

  console.log("Error caught");

} finally {

  console.log("Finally executed");

}

  • A) Error caught
  • B) Infinity
  • C) Finally executed
  • D) Error caught and Finally executed

Answer: D) Error caught and Finally executed
Explanation: 10 / 0 results in Infinity, but no error occurs. The finally block executes regardless of the outcome.


74. Which of the following will log an error if the JSON.parse() function receives an invalid string?

  • A) JSON.parse()
  • B) JSON.stringify()
  • C) JSON.parse("invalid json")
  • D) JSON.stringify("invalid json")

Answer: C) JSON.parse("invalid json")
Explanation: JSON.parse() throws a SyntaxError if the input string is not valid JSON.


75. How do you ensure that an error doesn't stop the execution of the program in a try...catch block?

  • A) By using finally.
  • B) By handling errors inside the catch block.
  • C) By logging errors.
  • D) By using the continue keyword.

Answer: B) By handling errors inside the catch block.
Explanation: By catching and handling errors within the catch block, you can ensure that the program doesn't stop and continues executing.


76. What type of error is commonly caused by accessing a property of null or undefined?

  • A) SyntaxError
  • B) RangeError
  • C) TypeError
  • D) ReferenceError

Answer: C) TypeError
Explanation: A TypeError occurs when trying to access properties or call methods on null or undefined.


77. What does console.error() do?

  • A) It logs an error message in the console.
  • B) It throws an exception.
  • C) It stops the program execution.
  • D) It logs a warning.

Answer: A) It logs an error message in the console.
Explanation: console.error() is used to output error messages in the console, which are typically shown in red.


These questions cover a wide range of error handling and debugging concepts, ensuring students understand different types of errors and the methods to handle them effectively.

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are additional multiple-choice questions (MCQs) on Error Handling and Debugging in JavaScript:


78. What is the purpose of the finally block in a try...catch statement?

  • A) It executes only when an error is caught.
  • B) It is used to log error messages.
  • C) It executes regardless of whether an error occurs or not.
  • D) It stops the execution of the code after the catch block.

Answer: C) It executes regardless of whether an error occurs or not.
Explanation: The finally block will execute after the try and catch blocks, regardless of whether an error was thrown or caught.


79. What will the following code output?

javascript

Copy code

try {

  let result = 2 / 0;

  console.log(result);

} catch (e) {

  console.log("Error caught");

}

  • A) Infinity
  • B) 0
  • C) Error caught
  • D) undefined

Answer: A) Infinity
Explanation: Dividing by zero in JavaScript results in Infinity, and no error is thrown.


80. How would you throw a custom error in JavaScript with a specific message?

  • A) throw new Error("Custom Error")
  • B) throw "Custom Error"
  • C) throw Error("Custom Error")
  • D) throw "Error" with message

Answer: A) throw new Error("Custom Error")
Explanation: To throw a custom error with a message, you create a new Error object and throw it.


81. Which method is used to retrieve the error message from an error object in JavaScript?

  • A) error.message
  • B) error.getMessage()
  • C) error.getDetails()
  • D) error.details

Answer: A) error.message
Explanation: The message property of an error object contains the error description.


82. What will the following code output?

javascript

Copy code

let num = 10;

try {

  num.toUpperCase();

} catch (e) {

  console.log(e.name);

} finally {

  console.log("Finally block executed");

}

  • A) TypeError and Finally block executed
  • B) SyntaxError and Finally block executed
  • C) ReferenceError and Finally block executed
  • D) Finally block executed

Answer: A) TypeError and Finally block executed
Explanation: num.toUpperCase() causes a TypeError since toUpperCase() is a method for strings, not numbers. The finally block executes afterward.


83. How do you handle errors when working with asynchronous code, such as a promise?

  • A) Using try...catch.
  • B) Using .catch() method.
  • C) By throwing the error manually.
  • D) Using finally().

Answer: B) Using .catch() method.
Explanation: In asynchronous code, such as promises, errors are handled using .catch() to catch any errors that occur.


84. What is the correct syntax to define a try...catch block?

  • A) try { } catch(e) { }
  • B) try(e) { } catch { }
  • C) try { } catch { e }
  • D) try() catch()

Answer: A) try { } catch(e) { }
Explanation: The correct syntax for a try...catch block is try followed by a block of code and catch to handle any error that occurs.


85. What happens when you throw an exception inside the catch block?

  • A) The exception is logged but not thrown.
  • B) The program will stop immediately.
  • C) The exception is re-thrown to a higher-level catch block.
  • D) The exception will not be caught again.

Answer: C) The exception is re-thrown to a higher-level catch block.
Explanation: If an exception is thrown inside the catch block, it will propagate to a higher-level catch block if one exists.


86. What will the following code output?

javascript

Copy code

try {

  let a = undefinedVariable;

} catch (e) {

  console.log(e.stack);

}

  • A) The error message along with the stack trace
  • B) The error message only
  • C) Undefined
  • D) A blank console

Answer: A) The error message along with the stack trace
Explanation: The e.stack property gives the stack trace of the error, helping identify where the error occurred in the code.


87. What is a possible outcome when an error is not caught using try...catch?

  • A) The program continues to execute normally.
  • B) A browser-specific error page is displayed.
  • C) The execution stops, and an error message is displayed.
  • D) The program ignores the error.

Answer: C) The execution stops, and an error message is displayed.
Explanation: If an error is not caught, it will cause the program to stop execution and display an error message.


88. In which situation would you use finally without a catch block?

  • A) To ensure cleanup code runs regardless of error occurrence.
  • B) To catch errors that might occur.
  • C) To handle specific types of errors.
  • D) To define a fallback value in case of an error.

Answer: A) To ensure cleanup code runs regardless of error occurrence.
Explanation: The finally block is typically used to ensure that cleanup operations are performed, regardless of whether an error occurred.


89. What type of error will occur when you try to call a method on null or undefined in JavaScript?

  • A) TypeError
  • B) ReferenceError
  • C) SyntaxError
  • D) RangeError

Answer: A) TypeError
Explanation: A TypeError occurs when you try to perform an operation on a value that is not of the expected type, such as calling a method on null or undefined.


90. Which statement is true about JavaScript’s console.log() method?

  • A) It always logs errors to the console.
  • B) It can be used for debugging by displaying variable values.
  • C) It throws an exception.
  • D) It automatically handles caught errors.

Answer: B) It can be used for debugging by displaying variable values.
Explanation: console.log() is commonly used in debugging to log messages, variable values, and objects to the console.


91. Which error is thrown if a variable is accessed before it’s initialized in JavaScript?

  • A) SyntaxError
  • B) TypeError
  • C) ReferenceError
  • D) RangeError

Answer: C) ReferenceError
Explanation: A ReferenceError occurs when trying to access a variable before it’s declared, especially when using let or const.


92. What does the throw keyword do in JavaScript?

  • A) It creates a new error.
  • B) It causes the program to stop immediately.
  • C) It allows a function to return a value.
  • D) It throws an error that can be caught by catch.

Answer: D) It throws an error that can be caught by catch.
Explanation: The throw keyword is used to manually throw an error, which can then be caught by a catch block.


93. Which of the following will NOT cause an error in JavaScript?

  • A) let x = 10 / 0;
  • B) let y = "text" + 10;
  • C) let z = "text" * 10;
  • D) let obj = undefined; obj.toString();

Answer: B) let y = "text" + 10;
Explanation: "text" + 10 results in the string "text10", which is not an error. The other expressions will result in errors.


94. How can you check for errors in asynchronous JavaScript functions using try...catch?

  • A) You cannot use try...catch for asynchronous functions.
  • B) You can use await inside try...catch.
  • C) You can wrap the setTimeout() function in a try...catch block.
  • D) catch block will only work for synchronous functions.

Answer: B) You can use await inside try...catch.
Explanation: async...await can be used inside a try...catch block to handle errors in asynchronous code.


95. Which of the following statements about JavaScript's JSON.parse() is correct?

  • A) JSON.parse() can handle both valid and invalid JSON strings.
  • B) JSON.parse() always returns an object.
  • C) If the JSON is invalid, JSON.parse() throws a SyntaxError.
  • D) JSON.parse() is used to serialize objects into JSON format.

Answer: C) If the JSON is invalid, JSON.parse() throws a SyntaxError.
Explanation: JSON.parse() parses a JSON string into an object, but it throws a SyntaxError if the JSON is not valid.


These additional questions provide a comprehensive understanding of error handling and debugging concepts, ensuring that students are well-prepared for managing errors and debugging JavaScript code effectively.

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are more multiple-choice questions (MCQs) on Error Handling and Debugging in JavaScript:


96. Which of the following statements is true about the throw keyword in JavaScript?

  • A) It can only throw custom error objects.
  • B) It is used to define an error handling block.
  • C) It stops the execution of the program completely.
  • D) It is used to manually throw an error.

Answer: D) It is used to manually throw an error.
Explanation: The throw keyword is used to manually throw errors in JavaScript, allowing you to throw custom or built-in error objects.


97. What type of error will occur if you try to call a method on a null or undefined value?

  • A) SyntaxError
  • B) RangeError
  • C) TypeError
  • D) ReferenceError

Answer: C) TypeError
Explanation: A TypeError occurs when an operation is performed on a value of the wrong type, such as calling a method on null or undefined.


98. How do you handle errors in JavaScript code that works with promises?

  • A) Use try...catch with async...await.
  • B) Use catch() method on the promise.
  • C) Use throw within the promise.
  • D) JavaScript automatically handles promise errors.

Answer: B) Use catch() method on the promise.
Explanation: When working with promises, the catch() method is used to handle any errors that occur during the promise’s execution.


99. What is the purpose of the console.error() method?

  • A) To log error messages in the console with a specific error style.
  • B) To throw an error in JavaScript.
  • C) To prevent errors from crashing the program.
  • D) To handle JavaScript exceptions automatically.

Answer: A) To log error messages in the console with a specific error style.
Explanation: The console.error() method is used to log error messages in a console, often formatted in red to distinguish them from regular logs.


100. Which of the following will result in an error when parsing JSON in JavaScript?

  • A) JSON.parse('{"name": "John", "age": 30}')
  • B) JSON.parse('{"name": "John", age: 30}')
  • C) JSON.parse('[1, 2, 3, 4]')
  • D) JSON.parse('true')

Answer: B) JSON.parse('{"name": "John", age: 30}')
Explanation: The JSON format requires that all property names be enclosed in double quotes, so age: 30 is invalid. It should be "age": 30.


101. What is the correct way to handle multiple errors in a single catch block?

  • A) Use catch to handle each error separately.
  • B) Use catch with an array of errors.
  • C) Use catch and then rethrow specific errors as needed.
  • D) catch can only handle one error at a time.

Answer: C) Use catch and then rethrow specific errors as needed.
Explanation: In a catch block, you can handle multiple types of errors, and if needed, you can rethrow specific errors to be handled elsewhere.


102. What does try...catch help prevent in JavaScript?

  • A) Syntax errors
  • B) Uncaught errors that could stop program execution
  • C) Issues with variable scoping
  • D) Memory leaks

Answer: B) Uncaught errors that could stop program execution.
Explanation: try...catch is used to handle errors that could potentially interrupt the execution of the program, allowing for more graceful error handling.


103. Which of the following is the correct syntax for throwing a custom error in JavaScript?

  • A) throw "Custom Error";
  • B) throw Error("Custom Error");
  • C) throw new CustomError("Custom Error");
  • D) throw new Error("Custom Error");

Answer: D) throw new Error("Custom Error");
Explanation: To throw a custom error, you create a new Error object and throw it using throw new Error("message");.


104. What is the function of event.preventDefault() in an event handler?

  • A) It stops the event from bubbling up the DOM tree.
  • B) It cancels the default action that belongs to the event.
  • C) It pauses the event handler function.
  • D) It changes the default action of the event.

Answer: B) It cancels the default action that belongs to the event.
Explanation: event.preventDefault() is used to prevent the default behavior associated with an event, such as preventing a form from submitting when the submit button is clicked.


105. What happens when throw is used without an argument in JavaScript?

  • A) It throws a generic error with no message.
  • B) It terminates the program without throwing an error.
  • C) It automatically handles errors inside catch block.
  • D) It creates a reference error.

Answer: A) It throws a generic error with no message.
Explanation: Using throw without an argument results in a generic error being thrown, which will be caught by any catch block.


106. How can you catch an error that occurs inside a promise in JavaScript?

  • A) By using try...catch in the promise callback.
  • B) By chaining .then() to the promise.
  • C) By chaining .catch() to the promise.
  • D) By using catch() inside the catch block.

Answer: C) By chaining .catch() to the promise.
Explanation: The .catch() method is used to handle errors in promises, allowing you to catch errors that occur during the promise execution.


107. Which of the following is NOT a valid method for debugging in JavaScript?

  • A) console.log()
  • B) console.warn()
  • C) console.error()
  • D) console.alert()

Answer: D) console.alert()
Explanation: There is no console.alert() method in JavaScript. The console.log(), console.warn(), and console.error() methods are valid for debugging.


108. What will happen if you don't use a catch block in a try...catch statement?

  • A) The error will be automatically caught and logged.
  • B) The error will stop the program, and no message will be displayed.
  • C) The program will continue, ignoring the error.
  • D) The error will be propagated to the global scope.

Answer: B) The error will stop the program, and no message will be displayed.
Explanation: Without a catch block, uncaught errors in a try block will stop program execution and throw an error message to the console.


109. Which of the following is an example of a runtime error in JavaScript?

  • A) let x = 5; console.log(y);
  • B) function myFunction() { return; }
  • C) let x = 5 + "hello";
  • D) let myFunction = () => {};

Answer: A) let x = 5; console.log(y);
Explanation: This code will throw a ReferenceError because y is not defined, making it a runtime error.


110. How can you inspect the stack trace of an error in JavaScript?

  • A) By using console.log(e.stack) in the catch block.
  • B) By using console.error(e.stack) in the catch block.
  • C) By using e.stack in the catch block.
  • D) All of the above.

Answer: D) All of the above.
Explanation: You can inspect the stack trace of an error using e.stack in any of the above methods, typically inside a catch block.


 


Tags

Post a Comment

0Comments

Post a Comment (0)