April 24, 2015

C# Oops constructor related question

What will be the output of below program

    class ConstructorConcept
    {
       
static void Main()
        {
           
ExtendedSample objExtendedSample = new ExtendedSample("Oops is very interesting");
           
Console.ReadKey();
        }

       
public class Sample
        {
         
           
public Sample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => Sample"); 
            }
        }

       
public class  ExtendedSample:Sample
        {
           
public ExtendedSample()
            {
               
Console.WriteLine("I am in zero argument constructor => ExtendedSample"); 
            }

           
public ExtendedSample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => ExtendedSample"); 
            }
        }
    }

This program will not compile because in base class we don’t have zero argument constructor.

April 23, 2015

C# Oops constructor related question part 2

I have modified base class. Now we have not defined any constructor inside base class.
Is this program will compile?

    class ConstructorConcept
    {
       
static void Main()
        {
           
ExtendedSample objExtendedSample = new ExtendedSample("Oops is very interesting");
           
Console.ReadKey();
        }

       
public class Sample
        {
         
           
public Sample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => Sample"); 
            }
        }

       
public class  ExtendedSample:Sample
        {
           
public ExtendedSample()
            {
               
Console.WriteLine("I am in zero argument constructor => ExtendedSample"); 
            }

           
public ExtendedSample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => ExtendedSample"); 
            }
        }
    }

Yes this program will compile because we have default constructor in base class.

April 22, 2015

C# Oops constructor related question 3

What should be the output of this program?

public class Example
        {
           
public static Example()
            {

            }
           
public static void Show()
            {
               
Console.WriteLine("Oops ");
            }
        }

       
static void Main()
        {
           
Example obj = new Example();
        }


This program will not compile because we have used access specifier with static constructor.

April 21, 2015

Try catch in SQL Server

What will be the output of below script?

BEGIN TRY
       SELECT 1/0
       SELECT 'Try Catch Test'
END TRY
BEGIN CATCH
              SELECT ERROR_NUMBER() AS ErrorNumber
     ,ERROR_SEVERITY() AS ErrorSeverity
     ,ERROR_STATE() AS ErrorState
     ,ERROR_LINE() AS ErrorLine
     ,ERROR_MESSAGE() AS ErrorMessage;
END CATCH


After exception it will go to catch block.
And Output:

image

April 15, 2015

Exception in catch block

Consider below program if there will be exception inside catch block then what will be the output of this program?

static void Main()
        {
           
int A = 0;
           
int Y = 5;
           
int X = 0;

           
try
            {
                A = 0;
                Y = 5;
                X = Y / A;
            }

           
catch (Exception ex)
            {
                X = Y / A;
            }

           
finally
            {
               
Console.WriteLine("I am in finally block");
               
Console.ReadKey();
            }
        }


This program will generate run time exception and it will get terminated.

April 14, 2015

C# try catch tricky question

What will be the output of below program?

static void Main()
        {
           
try
            {
               
int A = 0;
               
int Y = 5;
               
int X = Y / A;
            }

           
finally
            {
               
Console.WriteLine("I am in finally block");
               
Console.ReadKey();
            }
        }

This program will generate run time exception and Application will get terminated.