MCQs on Exception Handling | Kotlin

Learn Kotlin Exception Handling through this quiz! Understand the concepts of try-catch-finally, custom exceptions, and best practices for error handling to write robust, error-resistant Kotlin code.


MCQs on Exception Handling in Kotlin

try-catch-finally

  1. In Kotlin, which block is used to handle exceptions?
    a) catch
    b) try
    c) finally
    d) both b and a
  2. What is the purpose of the finally block in Kotlin exception handling?
    a) To handle the exception
    b) To throw the exception
    c) To execute code regardless of whether an exception occurs
    d) To catch exceptions
  3. Which of the following is the correct syntax for using try-catch in Kotlin?
    a) try { } catch(Exception e) { }
    b) try { } catch(e: Exception) { }
    c) try { } catch(Exception: e) { }
    d) try { } catch{Exception} { }
  4. What happens if no catch block is provided after try in Kotlin?
    a) The program throws an error
    b) The exception is passed to the finally block
    c) The exception is ignored
    d) The exception is propagated to the calling function
  5. In Kotlin, can you have multiple catch blocks for a single try?
    a) Yes, you can handle different types of exceptions separately
    b) No, only one catch block is allowed
    c) Yes, but only for IOException
    d) No, catch is used only once

Custom Exceptions

  1. How do you define a custom exception in Kotlin?
    a) class MyException: Exception()
    b) class MyException extends Exception()
    c) custom class MyException: Throwable()
    d) class MyException implements Exception()
  2. Which keyword is used to throw an exception in Kotlin?
    a) raise
    b) throw
    c) error
    d) catch
  3. What is the correct way to throw a custom exception in Kotlin?
    a) throw MyException()
    b) throw new MyException()
    c) MyException.throw()
    d) throw error MyException
  4. Which class must be inherited to create a custom exception in Kotlin?
    a) Throwable
    b) Exception
    c) RuntimeException
    d) Error
  5. What is the default constructor for Exception in Kotlin?
    a) Exception(message: String)
    b) Exception(String, Throwable)
    c) Exception()
    d) Exception(Throwable)

Best Practices for Error Handling

  1. Which of the following is considered a good practice in Kotlin for exception handling?
    a) Catching Throwable to handle all errors
    b) Throwing custom exceptions for every error
    c) Handling exceptions only where they occur
    d) Avoiding exception handling altogether
  2. When should you avoid using exceptions in Kotlin?
    a) For handling control flow situations
    b) For catching specific error cases
    c) When you want to terminate the program
    d) For general runtime exceptions
  3. What is the purpose of catching specific exceptions in Kotlin?
    a) To improve error debugging and handling
    b) To avoid using finally
    c) To ensure all exceptions are handled by try
    d) To prevent exception propagation
  4. What is the recommended action if a catch block handles an exception in Kotlin?
    a) Log the exception and rethrow it
    b) Do nothing and continue
    c) Only print the exception message
    d) Ignore the exception completely
  5. Should you catch RuntimeException in Kotlin?
    a) Yes, if it’s a potential issue
    b) No, it’s a type of unchecked exception
    c) Yes, only in specific cases
    d) Only in production environments
  6. In Kotlin, which type of exceptions should ideally be caught in try-catch blocks?
    a) Checked exceptions
    b) Runtime exceptions
    c) Errors
    d) Both checked and unchecked exceptions
  7. Which of these methods can help prevent exception handling from becoming cumbersome in Kotlin?
    a) Using custom exceptions with descriptive messages
    b) Catching all exceptions with a generic catch block
    c) Ignoring all exceptions
    d) Using finally for every error
  8. How can you ensure that resources are always cleaned up in Kotlin after an exception occurs?
    a) Use finally block
    b) Catch and rethrow the exception
    c) Use throw for every exception
    d) Avoid using exceptions altogether
  9. What is the most common use case for custom exceptions in Kotlin?
    a) To add extra functionality
    b) To represent domain-specific error cases
    c) To replace built-in exceptions
    d) To throw errors without context
  10. What is the correct way to handle multiple exceptions in a single catch block?
    a) By using when statements
    b) By using || operator
    c) By using multiple catch blocks
    d) By catching Throwable
  11. Which of these exception types should not be caught in Kotlin?
    a) RuntimeException
    b) Error
    c) IOException
    d) NullPointerException
  12. How can you ensure your custom exceptions are informative in Kotlin?
    a) By adding relevant information in the exception message
    b) By using Throwable only
    c) By only catching IOException
    d) By using default exception names
  13. What does the catch block do when no exception is thrown?
    a) It throws an error
    b) It executes the code inside the catch block
    c) It skips the catch block entirely
    d) It prints a warning
  14. Should exceptions be used to control the flow of a program in Kotlin?
    a) Yes, exceptions are ideal for controlling flow
    b) No, exceptions should be used for error handling only
    c) Only for fatal errors
    d) Yes, when performance is not a concern
  15. What should be done if an exception is thrown inside a finally block in Kotlin?
    a) It gets ignored
    b) The exception is propagated
    c) It is caught by another catch block
    d) It terminates the program immediately
  16. Which is the best practice when handling exceptions in Kotlin for performance?
    a) Avoid throwing exceptions in performance-critical code
    b) Throw exceptions to handle errors faster
    c) Catch all exceptions to prevent crashes
    d) Use exceptions for flow control
  17. How can you handle multiple exceptions with a single catch block in Kotlin?
    a) Use catch (ExceptionType1 | ExceptionType2)
    b) Use a when block inside the catch
    c) Use try-catch for each exception separately
    d) It is not possible
  18. How do you define a custom exception with additional parameters in Kotlin?
    a) class MyException(message: String, code: Int): Exception(message)
    b) class MyException(message: String, code: Int): Throwable(message)
    c) custom MyException(message: String, code: Int)
    d) class MyException: Throwable(message, code)
  19. How does Kotlin handle exceptions by default?
    a) It throws all exceptions automatically
    b) It does not handle exceptions
    c) It propagates exceptions up the call stack
    d) It catches all exceptions silently
  20. What type of exceptions are considered unchecked exceptions in Kotlin?
    a) All exceptions
    b) Runtime exceptions
    c) Checked exceptions
    d) System errors

Answers Table

QnoAnswer
1d) both b and a
2c) To execute code regardless of whether an exception occurs
3b) try { } catch(e: Exception) { }
4d) The exception is propagated to the calling function
5a) Yes, you can handle different types of exceptions separately
6a) class MyException: Exception()
7b) throw
8a) throw MyException()
9a) Throwable
10c) Exception()
11a) Catching Throwable to handle all errors
12a) For handling control flow situations
13a) To improve error debugging and handling
14a) Log the exception and rethrow it
15b) No, it’s a type of unchecked exception
16a) Checked exceptions
17a) Using custom exceptions with descriptive messages
18a) Use finally block
19b) To represent domain-specific error cases
20a) By using when statements
21b) Error
22a) By adding relevant information in the exception message
23c) It skips the catch block entirely
24b) No, exceptions should be used for error handling only
25b) The exception is propagated
26a) Avoid throwing exceptions in performance-critical code
27b) Use a when block inside the catch
28a) class MyException(message: String, code: Int): Exception(message)
29c) It propagates exceptions up the call stack
30b) Runtime exceptions

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