2) What is the correct output of the given code snippet?
using System;
namespace my_namespace
{
class program
{
static void Main(string[] args)
{
int a = 0;
int b = 10;
int c = 0;
try
{
c = b / a;
}
catch (Exception d)
{
Console.Write("Divide by zero exception occurred, ");
}
finally
{
Console.WriteLine("Finally executed");
}
}
}
}
- Divide by zero exception occurred
- Divide by zero exception occurred, Finally executed
- Syntax error
- Linker error
Correct answer: 2
Divide by zero exception occurred, Finally executed
The above code will print "Divide by zero exception occurred, Finally executed" on the console screen.
4) There are following statements are given below, which of them are correct about the checked keyword in C#.NET?
- The checked is not any keyword in C#.NET
- We can use the unchecked keyword, but we cannot use the checked keyword in C#.NET
- The checked keyword is used to check the overflow and conversion of integral type values
- The checked keyword is used in older versions of the .NET framework
Correct answer: 3
The checked keyword is used to check the overflow and conversion of integral type values
The 3rd option is correct about the checked keyword.
5) What is the correct output of the given code snippet?
using System;
namespace my_namespace
{
class program
{
static void Main(string[] args)
{
checked
{
int my_val = int.MaxValue;
Console.WriteLine(my_val + 3);
}
}
}
}
- -2147483646
- Syntax error
- Linker error
- Overflow exception
Correct answer: 4
Overflow exception
If we do not use the checked keyword, then it will print "-2147483646” on screen, but here, we used checked keyword then it generates overflow exception.
The output would be,
Unhandled Exception:
System.OverflowException: Arithmetic operation resulted in an overflow.
at my_namespace.program.Main (System.String[] args) [0x00006] in <dba6bf97f5624fd1851df0bff7e738a3>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Arithmetic operation resulted in an overflow.
at my_namespace.program.Main (System.String[] args) [0x00006] in <dba6bf97f5624fd1851df0bff7e738a3>:0