C# Exception Handling – 35 Questions Programmers Should not Miss

Exceptions are runtime errors that can make a program behave abruptly if not handled in the right way. Hence, most programming languages add exception handling capabilities. C# too has a robust object-oriented exception handling framework. And a good C# programmer should know how to use C# exception handling techniques to ensure a good user-experience. Interviewers often ask questions on exception handling during interviews.

Here we are laying down 35 C# interview questions on exception handling for C# programmers to identify rough edges. A programmer must be careful while handling exceptions in the application code. It could be a background service where you need to log errors in a file or an interactive user application expecting for a proper error message. There might even cases when you have to catch and rethrow an exception. For example, rethrowing an exception could be useful in rolling back a database transaction.

C# Exception Handling – Quick Review.

Following are some of the key facts that you should know about exception handling in C#.

  • In C# language, Exception (<System.Exception>) is the base class of all exceptions. And it itself is derived from the Object (<System.Object>) class.
  • Other exception classes like <System.ApplicationException> and <System.SystemException> derive directly or indirectly from the Exception class.
  • You can also create a user-defined or a custom exception in C#. With custom exceptions, you can decode a complex error into a meaningful message.
  • Exception handling in C# mainly revolves around the four keywords.

  • A try block can cause more than one exceptions. By having a catch block without brackets, you can handle them all within the same catch block.
  • To rethrow an exception, you can use the <throw> keyword.
  • User-defined catch block must appear before any generic catch block. Otherwise, the runtime will ignore it from execution.
  • In the absence of an exception handler, the program will abort with an error message.

If you are a real C# coding buff, then you may like to try the C# interview questions with 50 coding snippets we recently added.

Let’s now explore the C# Exception Handling Q&A section.

C# Exception Handling Questions for Programmers.

C# Exception Handling Questions for Programmers

C# Exception Handling Questions And Answers.

Q-1. Which of the following statements applies to the situation where Exception is not handled in the program:

a) The Compiler will not allow the program to run the code.
b) CLR will terminate the program execution at the point it encounters an exception.
c) CLR will not show any output. However, the code will execute successfully.
d) The Code executes successfully and an error message gets printed.

Check correct option.
Answer. b)

 

Q-2. Which of the following statements correctly defines the usage of <try-catch> blocks in exception handling:

a) Error prone code is written inside the try block and the exception handling code is written inside the catch block.
b) Error prone code is written inside the try block and the error free code is written inside the catch block.
c) Exception handling code is written inside the try block and error code inside the catch block.
d) None of the above.

Check correct option.
Answer. a)

 

Q-3. Which of the following is NOT a valid DOT NET Exception class:

a) System.Exception
b) System.DivideByZeroException
c) System.OutOfMemoryException
d) System.InvalidOperationException
e) System.StackMemoryException

Check correct option.
Answer. e)

 

Q-4. Which of the following statements correctly defines the usage of the <finally> block in exception handling:

a) Code under the <finally> block gets executed only when try and catch block get executed.
b) Code under the <finally> block gets executed only if the catch block is not executed.
c) Code under the <finally> block is always executed irrespective of the <try-catch> block is executed or not.
d) Code under the <finally> block gets executed only if the catch block is executed.

Check correct option.
Answer. c)

 

Q-5. Which of the following statements correctly defines the purpose of return statements in a <try-catch> and <finally> blocks:

a) The return statement can be used in all to return data.
b) The return statement cannot be used in catch and <finally> blocks.
c) The usage of a return statement is not supported in the <finally> block.
d) The return statement cannot be used in the catch block.

Check correct option.
Answer. c)

 

Q-6. Which of the following gives the number of catch blocks that can be defined for a single try block:

a) One
b) Two
c) Many
d) The try block can have any number of catch blocks. Just the most specific exception should precede the most generic one i.e. all child catch blocks to be defined first followed by the parent catch block.

Check correct option.
Answer. d)

 

Q-7. Which of the following keyword can be used to rethrow an exception in C#:

a) return
b) throw
c) re-throw
d) create

Check correct option.
Answer. b)

 

Q-8. Which of the following statements correctly justifies the exception given below:

a) The program did not handle an exception called IndexOutOfRangeException.

b) The class MyProgram belongs to the namespace TechBeamers.
c) The function Assign() was called from Main() in line number 25.
d) The exception occurred in line number 30 in the function Assign().
e) All of the above

Check correct option.
Answer. e)

 

Q-9. What will be the output of the following code snippet:

a) Runtime error occurs
b) assigns 100 to arr[6]
c) Outputs: Index out of bounds occurred
d) Outputs: Program execution continued after Exception Handling
e) Outputs: Index out of bounds occurred
   Program execution continued after Exception Handling

Check correct option.
Answer. e)

 

Q-10. What will be the output of the following code snippet, if the value entered for the index is a string like “ab”:

a) output: Bad Format
b) output: Remaining program
c) output: Index out of bounds
d) output: Bad Format Remaining program
e) output: Index out of bounds Remaining program

Check correct option.
Answer. d)

 

Q-11. What will be the result of the following code snippet, if the value entered for the index is 9:

a) Runtime exception is thrown
b) output: Exception occurred
                   Remaining program
c) output: Remaining program
d) output: Index out of bounds
                    Remaining program
e) The value 100 will get assigned to <a[9]>

Check correct option.
Answer. c) & e)

 

Q-12. What will be the output of the following code snippet:

a) Exception: Divide by Zero error
b) Divide by Zero error
c) Exception: 0
d) Runtime error

Check correct option.
Answer. b)

 

Q-13. Which of the following statements are correct about the exception handling <try-catch> blocks:

a) The try blocks cannot be nested.
b) In one function, there can be only one try block.
c) An exception must be caught in the same function in which it is thrown.
d) All values set up in the exception object are available in the catch block.
e) While throwing a user-defined exception multiple values can be set in the exception object.

Check correct option.
Answer. d), and e)

 

Q-14. What will be the output of the following code snippet:

a) Runtime Error
b) Unhandled Exception
c) Exception: DivideByZeroException
d) Divide by Zero error

Check correct option.
Answer. b), c) & d)

 

Q-15. What will be the output of the following code snippet:

a) 0
b) 0 10
c) 0 -10
d) 5

Check correct option.
Answer. c)

 

Q-16. What will be the output of the following code snippet:

a) 1234
b) Runtime Error
c) 12345A
d) 12345B

Check correct option.
Answer. d)

 

Q-17. What is the correct order of the catch blocks to allow the code to execute successfully:

a) First catch block to be moved to the end.
b) First catch block to be moved to the end and the last catch block to be moved to the first place.
c) Last catch block to be moved to first place.
d) None of the above.

Check correct option.
Answer. b)

 

Q-18. What will be the output of the following code snippet:

a) 12345
b) 12345A
c) 12345AA
d) Run time error

Check correct option.
Answer. b)

 

Q-19. What will be the output of the following code snippet:

a) Runtime error
b) ABC
c) B
d) BC

Check correct option.
Answer. d)

 

Q-20. What will be the output of the following code snippet:

a) Runtime error
b) Compile time error
c) 0
d) Exception Handled

Check correct option.
Answer. c)

 

Q-21. What will be the output of the following code snippet:

a) Runtime error
b) Hello
c) Exception Occurred
d) Exception Occurred
     Hello

Check correct option.
Answer. c)

 

Q-22. What will be the output of the following code snippet:

a) Exception Occurred
b) Exception Caught
c) Runtime Error
d) Compile time Error

Check correct option.
Answer. c)

 

Q-23. What will be the output of the following code snippet:

a) Unhandled Exception: System.Exception:Random = 0 at MyProgram.Main()
     Flow reaches Finally
b) Flow reaches Finally
c) Random = 0
    Random = 1
    Flow reaches Finally
d) Random = 0
     Random = 1
     Random = 2
     Flow reaches Finally

Check correct option.
Answer. a)

 

Q-24. What will be the output of the following code snippet:

a) Finally Block
b) Exception Caught
c) Exception Caught
    Finally Block
d) Runtime Error

Check correct option.
Answer. a)

 

Q-25. What will be the output of the following code snippet:

a) Runtime Error
b) Index Out Of Range Exception
c) Arithmetic Exception
d) Code compiles successfully and nothing gets printed

Check correct option.
Answer. c)

 

Q-26. What will be the output of the following code snippet:

a) Runtime error
b) Compile time error
c) This is My Exception
d) Code compiles successfully and nothing gets printed

Check correct option.
Answer. c)

 

Q-27. Which of the following keywords does the calling function use to guard against the exception thrown by the called function:

a) try
b) throw
c) throws
d) catch

Check correct option.
Answer. c)

 

Q-28. What will be the output of the following code snippet:

a) Runtime error
b) Compile time error
c) Rest of the Code
d)

Check correct option.
Answer. d)

 

Q-29. What will be the output of the following code snippet:

a) Inner Catch
b) Outer Catch
c) Runtime Error
d) Compile time Error

Check correct option.
Answer. b)

 

Q-30. What will be the output of the following code snippet:

a) Runtime Error
b) Compile time Error
c)

d) Program compiles successfully and nothing is printed.

Check correct option.
Answer. c)

 

Q-31. What will be the output of the following code snippet:

a) Runtime Error
b) Compile time Error
c)

d) The program compiles successfully and nothing is printed.

Check correct option.
Answer. c)

 

Q-32. What will be the output of the following code snippet:

a) Runtime Error
b) Compile time Error
c)

d) The program compiles successfully and nothing is printed.

Check correct option.
Answer. c)

 

Q-33. What will be the output of the following code snippet:

a) Runtime Error
b) Compile time Error
c) The program compiles successfully and nothing is printed.
d) Int32 Parse(System.String)

Check correct option.
Answer. d)

 

Q-34. What will be the output of the following code snippet:

a) Runtime Error
b) Compile time Error
c) The program compiles successfully and nothing is printed.
d)

Check correct option.
Answer. c)

 

Q-35. What will be the output of the following code snippet:

a) Runtime Error
b) Compile time Error
c) The program compiles successfully and nothing is printed.
d)

Check correct option.
Answer. d)

Summary – C# Exception Handling Interview Questions.

Writing error free code could be a challenge for many. However, with a proper exception handling approach, you can still secure a good user-experience.

We hope that our efforts above with a brief review of C# exception handling concepts and a set of C# interview questions would accelerate your endeavor to become a successful C# programmer.

Best,
TechBeamers.

 

2 Comments

  1. Fazal Reply
    • Meenakshi Agarwal Reply

Leave a Reply

*