Sponsored Ad

Saturday, May 12, 2012

Interview Questions: Implement Same Name Method of two Interfaces in a Class using C#

This is most famous question in OOPs programming. if you have two interfaces and driving a class and these two interfaces have the same name method then how will you implement those same name methods.

The below program is an example of the above problem.

Here we have two interfaces Sparrow and Parrot, both have same method to print the BirdType.

So to implement the BirdType method in class Bird, use the interface name along with interface method name. This will fully qualify the method name. Program will execute without any error.

The example of the program is given below.

Interview Questions: Implement Same Name Method of two Interfaces in a Class using C#

using System;

namespace CSharpInterview
{
    interface Sparrow
    {
        void BirdType();
    }

    interface Parrot
    {
        void BirdType();
    }

    class Bird : Parrot, Sparrow
    {
        void Parrot.BirdType()
        {
            Console.WriteLine("Bird is Parrot.");
        }

        void Sparrow.BirdType()
        {
            Console.WriteLine("Bird is Sparrow.");
        }
    }

    class Programs
    {       
        static void Main(string[] args)
            {

                Parrot ObjParrot = new Bird();
                ObjParrot.BirdType();

                Sparrow ObjSparrow = new Bird();
                ObjSparrow.BirdType();
                Console.ReadLine();
            }
    }   
}

Inheriting and Method using with Multiple Interfaces in C#

The multiple inheritance can be implemented in C# by using the interfaces only. You can inherit a class from multiple interfaces but can inherit a class with only one class.

The below program inherit a class from two interfaces Laptop and PersonalComputer. Both the interfaces have a print method. The main function create an object of each interface and call the corresponding method.

While if we create a object of drive class then the both methods are accessible.

Inheriting and Method using with Multiple Interfaces in C#

using System;

namespace CSharpInterview
{
    interface PersonalComputer
    {
        void PCPrint();
    }

    interface Laptop
    {
        void LaptopPrint();
    }

    class Computer : Laptop, PersonalComputer
    {
        public void PCPrint()
        {
            Console.WriteLine("PC Print Method.");
        }

        public void LaptopPrint()
        {
            Console.WriteLine("Laptop Print Method.");
        }
    }

    class Programs
    {       
        static void Main(string[] args)
            {

                Laptop ObjLaptop = new Computer();
                ObjLaptop.LaptopPrint();

                PersonalComputer ObjPersonalComputer = new Computer();
                ObjPersonalComputer.PCPrint();

                Console.WriteLine("Computer Class...");
                Computer ObjComputer = new Computer();
                ObjComputer.LaptopPrint();
                ObjComputer.PCPrint();

                Console.ReadLine();
            }
    }   
}

Interview Questions: Handle the Exception and Execute Sequential Statement after the Exception in C#

If you want to execute the sequential statement even if a exception occur, use try and catch block and handle the exception or even do not write anything in catch block, it will automatically execute the next statement.

The below sample program is example of executing the next statement after the exception.

Interview Questions: Handle the Exception and Execute Sequential Statement after the Exception in C#

using System;

namespace CSharpInterview
{
    class Programs
    {       
        static void Main(string[] args)
            {

                Console.WriteLine("Example to execute the code after exception");
                Console.WriteLine("");
            Console.WriteLine("Before Exception");
                try
                {                  
                    throw new NullReferenceException();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Handled: {0}", ex.Message);
                }
                Console.WriteLine("After Exception");

                Console.ReadLine();
            }
    }   
}

Interview Questions: How to Print 1 to 20 Numbers without using any loop in C#

One popular question in interview is that how to print a sequence with out using any loop. The simplest way is use the recursion to print the sequence. The below program is an example of this question. Which is using the recursion function to call and print the series.

Interview Questions: How to Print 1 to 20 Numbers without using any loop in C#

using System;

namespace ConsoleInterview
{
    class Programs
    {       
        static void Main(string[] args)
            {

                Console.WriteLine("Printing 1 to 20...");
                string strOutPut = "";
                PrintSequence(1, ref strOutPut);
                Console.WriteLine(strOutPut.Trim().TrimStart(','));               

                Console.ReadLine();
            }

        private static void PrintSequence(int Sequence, ref string FinalString)
        {
            if (Sequence <= 20)
            {
                FinalString += " ," + Sequence;
                Sequence += 1;              

                PrintSequence(Sequence, ref FinalString);
            }
        }
    }   
}

Sponsored Ad

Followers

Follow Us