Exception Handling Questions

Practice Exception Handling MCQs with answers and explanations. Page 1 of 1.

Category
C# Programming
Topic
Exception Handling
Page
1 / 1
Mode
Practice

Questions

Open any question to view the answer and explanation.

C#.NET — Object-Oriented way to handle run-time errors: choose the correct mechanism.
Open
View answer
C#.NET — When do exceptions occur? Choose the correct phase.
Open
View answer
C#.NET — Predict output with try/catch for invalid numeric input. Program reads an index and stores val = 55 in an array element. Input provided: "ABCD". What is printed?
Open
View answer
C#.NET — Predict output with general exception handling and out-of-range index. Input provided: "6". What is printed?
Open
View answer
C#.NET — What will this program print if the user types the non-numeric text "ABCD"? using System; namespace CuriousTabConsoleApplication { class MyProgram { static void Main(string[] args) { int index; int val = 88; int[] a = new int[5]; try { Console.Write("Enter a number: "); index = Convert.ToInt32(Console.ReadLine()); a[index] = val; } catch (Exception e) { Console.Write("Exception occurred"); } Console.Write("Remaining program"); } } }
Open
View answer
C#.NET — What will this program print when it tries to write a[6] (array size 5)? using System; namespace CuriousTabConsoleApplication { class MyProgram { static void Main(string[] args) { int index = 6; int val = 44; int[] a = new int[5]; try { a[index] = val; } catch (IndexOutOfRangeException e) { Console.Write("Index out of bounds "); } Console.Write("Remaining program"); } } }
Open
View answer
C#.NET — Is all code in a finally block guaranteed to run, regardless of whether an exception occurs in the associated try block?
Open
View answer
C#.NET — Interpreting an unhandled exception report: Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at CuriousTabConsoleApplication.Program.Main(String[] args) in D:\ConsoleApplication\Program.cs:line 14 Which statements are correct?
Open
View answer
C#.NET — Which of the following is NOT an exception (i.e., not a standard .NET exception condition/class)?
Open
View answer
C#.NET — If an exception is not caught by your code, who catches it?
Open
View answer
C#.NET — True or False: A constructor can throw exceptions, but it cannot return an error code.
Open
View answer
C#.NET — Which of the following is NOT a valid .NET exception class?
Open
View answer
C#.NET — What will this program print if the user enters "6" for the index? using System; namespace CuriousTabConsoleApplication { class MyProgram { static void Main(string[] args) { int index; int val = 44; int[] a = new int[5]; try { Console.Write("Enter a number:"); index = Convert.ToInt32(Console.ReadLine()); a[index] = val; } catch (FormatException e) { Console.Write("Bad Format"); } catch (IndexOutOfRangeException e) { Console.Write("Index out of bounds"); } Console.Write("Remaining program"); } } }
Open
View answer
C#.NET — Which statements about exception handling are correct? 1) If our program does not catch an exception then the .NET CLR catches it. 2) It is possible to create user-defined exceptions. 3) All types of exceptions can be caught using the Exception class. 4) CLRExceptions is the base class for all exception classes. 5) For every try block there must be a corresponding finally block.
Open
View answer
C#.NET — Exception handling basics: identify the correct facts about try/catch/finally. Which statements are correct? If an exception occurs, the program always terminates abruptly with no chance to recover. Whether or not an exception occurs, the statements in the finally block (if present) will execute. A program can contain multiple finally blocks for the same try. A finally block is written after the try (and optional catch) blocks, not inside the try body. A finally block is commonly used for cleanup such as closing files, network sockets, or database connections.
Open
View answer
C#.NET — Read a stack trace: what does this “IndexOutOfRangeException” report tell you? Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array at CuriousTabConsoleApplication.MyProgram.SetVal(Int32 index, Int32 val) in D:\Sample\CuriousTabConsoleApplication\MyProgram.cs:line 26 at CuriousTabConsoleApplication.MyProgram.Main(String[] args) in D:\Sample\CuriousTabConsoleApplication\MyProgram.cs:line 20
Open
View answer
C#.NET — Exception handling rules: pick the true statements. try blocks cannot be nested. Only one try block is allowed per function. An exception must be caught in the same function where it is thrown. Values set in the exception object are available in the catch block. When throwing a user-defined exception, multiple data values can be stored in the exception object (for retrieval in catch).
Open
View answer
C#.NET — True or False: Any class whose instances are thrown using throw must derive (directly or indirectly) from System.Exception.
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.