MCQs on Error Handling and Debugging | Scala

Get a deep understanding of error handling and debugging in Scala. This set of MCQs covers topics like Try, Success, Failure, catching exceptions, and debugging essentials.


Error Handling and Debugging in Scala

1. Try, Success, Failure

  1. What is the purpose of Try in Scala?
    • A) To catch and handle exceptions
    • B) To execute code in a safe way that can return a success or failure
    • C) To throw exceptions
    • D) To handle success cases only
  2. What does Success represent in Scala’s Try?
    • A) An exception was thrown
    • B) A successful computation without errors
    • C) A default value
    • D) A failed operation
  3. What does Failure represent in Scala’s Try?
    • A) A successful computation without errors
    • B) A failed operation with an exception
    • C) A successful result wrapped in a value
    • D) A non-terminating operation
  4. Which of the following is used to handle Success in Scala?
    • A) .isSuccess
    • B) .get
    • C) .foreach
    • D) .map
  5. What type of value does Failure hold in Scala?
    • A) An exception
    • B) A string message
    • C) A boolean flag
    • D) An integer
  6. Which of the following is true about the Try class in Scala?
    • A) It wraps computations that can succeed or fail
    • B) It only handles exceptions that happen during execution
    • C) It only handles RuntimeException
    • D) It cannot be used with Success and Failure
  7. What happens when Try is used to catch an exception in Scala?
    • A) The program terminates with a message
    • B) It returns Failure with the exception
    • C) It returns Success with the exception message
    • D) It ignores the exception and continues
  8. Which method can be used to get the result from Try in Scala?
    • A) get
    • B) value
    • C) result
    • D) run
  9. How would you handle a Try to extract the value without throwing an exception?
    • A) .recover
    • B) .getOrElse
    • C) .value
    • D) .map
  10. What will the get method return when applied to a Failure?
    • A) The exception that caused the failure
    • B) A success value
    • C) Null
    • D) A new instance of the exception

2. Catching Exceptions

  1. What does the catch block in a try-catch expression in Scala do?
    • A) It specifies the exceptions to be caught
    • B) It always executes after the try block
    • C) It catches exceptions and prevents further execution
    • D) It defines the return type of the try block
  2. Which of the following exceptions can be caught in Scala?
    • A) Only checked exceptions
    • B) Only unchecked exceptions
    • C) Both checked and unchecked exceptions
    • D) None of the above
  3. How do you catch multiple exceptions in a try-catch block in Scala?
    • A) Using multiple catch blocks
    • B) By using a single catch block with multiple exception types
    • C) By using a catchAll block
    • D) By chaining catch statements
  4. What is the base class for all exceptions in Scala?
    • A) Exception
    • B) Throwable
    • C) Error
    • D) RuntimeException
  5. How can you catch an exception and log its details in Scala?
    • A) Use printStackTrace() inside the catch block
    • B) Use println() to output the exception message
    • C) Both A and B
    • D) You cannot log exceptions in Scala
  6. Which of these statements will rethrow an exception after it is caught?
    • A) throw ex
    • B) return ex
    • C) ex.retry()
    • D) ex.finish()
  7. What does the finally block in Scala’s try-catch-finally do?
    • A) It executes only when no exceptions are thrown
    • B) It runs after the try and catch blocks regardless of an exception
    • C) It is optional and can be removed
    • D) It executes before try
  8. What will happen if an exception is caught but not rethrown in a catch block?
    • A) The program will stop executing
    • B) The exception will be logged only
    • C) The program continues executing normally
    • D) The program will terminate
  9. Which of the following statements is true about checked exceptions in Scala?
    • A) They must be caught or declared in the method signature
    • B) They are ignored by default
    • C) Scala does not have checked exceptions
    • D) They are never thrown in Scala
  10. In Scala, how can you specify the type of exception to catch in a catch block?
    • A) By using the catchType method
    • B) By providing the exception type directly after case
    • C) By using catchAll keyword
    • D) It is not possible to specify a specific exception

3. Debugging Basics

  1. What is the purpose of debugging in Scala?
    • A) To find and fix syntax errors
    • B) To identify and resolve runtime issues
    • C) To optimize performance
    • D) To improve the speed of compilation
  2. What is a breakpoint in Scala debugging?
    • A) A line of code where execution is paused
    • B) A marker indicating an error
    • C) A place where the program starts
    • D) A function call
  3. How do you inspect the value of variables while debugging in Scala?
    • A) By using println() statements
    • B) By setting breakpoints and examining variable values in the debugger
    • C) By using catch statements
    • D) By printing out the stack trace
  4. Which Scala feature can help you to log messages for debugging?
    • A) println()
    • B) Logger class
    • C) trace() method
    • D) assert()
  5. What is a stack trace used for in debugging?
    • A) To display a list of function calls leading to the error
    • B) To show the exception message only
    • C) To list the memory usage of the program
    • D) To list the global variables in use
  6. Which of the following is an essential tool for debugging in Scala?
    • A) Stack trace
    • B) Profilers
    • C) Debugger
    • D) All of the above
  7. What does stepping over a line of code in the debugger mean?
    • A) The debugger moves to the next line in the current method
    • B) The debugger jumps to a different method
    • C) The debugger executes the code but doesn’t step into method calls
    • D) The debugger skips over the current function entirely
  8. How can you prevent certain parts of your code from being executed during debugging?
    • A) By using assert() statements
    • B) By using conditional breakpoints
    • C) By using try-catch blocks
    • D) By commenting out the code
  9. What is a watch expression used for in Scala debugging?
    • A) To pause execution based on variable values
    • B) To execute a function only when an exception occurs
    • C) To log all variable changes during execution
    • D) To display variable values at every breakpoint
  10. How can you debug a Scala program in IntelliJ IDEA?
    • A) By setting breakpoints and running the program in debug mode
    • B) By using the println() function
    • C) By inspecting the code manually
    • D) Debugging is not possible in IntelliJ IDEA

Answers

QnoAnswer
1B) To execute code in a safe way that can return a success or failure
2B) A successful computation without errors
3B) A failed operation with an exception
4C) .foreach
5A) An exception
6A) It wraps computations that can succeed or fail
7B) It returns Failure with the exception
8A) get
9B) .getOrElse
10A) The exception that caused the failure
11A) It specifies the exceptions to be caught
12C) Both checked and unchecked exceptions
13B) By using a single catch block with multiple exception types
14B) Throwable
15C) Both A and B
16A) throw ex
17B) It runs after the try and catch blocks regardless of an exception
18C) The program continues executing normally
19C) Scala does not have checked exceptions
20B) By providing the exception type directly after case
21B) To identify and resolve runtime issues
22A) A line of code where execution is paused
23B) By setting breakpoints and examining variable values in the debugger
24B) Logger class
25A) To display a list of function calls leading to the error
26D) All of the above
27C) The debugger executes the code but doesn’t step into method calls
28B) By using conditional breakpoints
29A) To pause execution based on variable values
30A) By setting breakpoints and running the program in debug mode

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