MCQs
Total Questions : 10
Answer: Option D. -> Runtime error
try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
Answer: Option A. -> try
Answer: Option C. -> B C
finally keyword is used to execute before catch and try block is executed.
Output : B C
Answer: Option C. -> thrown
Exception handling is managed via 5 keywords “ try, catch, throws, throw and finally.
Question 5. What will be the output of the given code snippet?
class Program { public static void Main(string[] args) { try { int a = 1; int b = 10 / a; try { if (a == 1) a = a / a - a; if (a == 2) { int[] c = { 1 }; c[8] = 9; } } finally { Console.WriteLine("A"); } } catch (IndexOutOfRangeException e) { Console.WriteLine("B"); } Console.ReadLine(); } }
class Program { public static void Main(string[] args) { try { int a = 1; int b = 10 / a; try { if (a == 1) a = a / a - a; if (a == 2) { int[] c = { 1 }; c[8] = 9; } } finally { Console.WriteLine("A"); } } catch (IndexOutOfRangeException e) { Console.WriteLine("B"); } Console.ReadLine(); } }
Answer: Option A. -> A
Question 6. What would be the output of given code snippet?
class Program { static void Main(string[] args) { int i; int v = 40; int[] x = new int[5]; try { Console.WriteLine(" Enter the number: "); index = Convert.ToInt32(Console.ReadLine()); x[index] = v; } catch(Exception e) { Console.WriteLine("Exception occured"); } Console.WriteLine("Program executed"); } }
class Program { static void Main(string[] args) { int i; int v = 40; int[] x = new int[5]; try { Console.WriteLine(" Enter the number: "); index = Convert.ToInt32(Console.ReadLine()); x[index] = v; } catch(Exception e) { Console.WriteLine("Exception occured"); } Console.WriteLine("Program executed"); } }
Answer: Option C. -> Exception occured
Program executed
Answer: Option D. -> All of the mentioned
Answer: Option C. -> Declare sum property with get, set and normal accessors
Answer: Option B. -> catch
Answer: Option B. -> Declares sum property with only set accessor