C# Interview Questions for written test

  .Net interview questions, C#

here we will learn top most basic c# Interview Questions for written test this questions are most useful and commonly very interviewer aked questions

Write a program for finding a given number is even or odd numbers?

 class Program
    {
        static void EvenOrOdd(int n)
        {
            if (n % 2 == 0)
                Console.WriteLine("Even");
            else
                Console.WriteLine("Odd");
        }

        public static void Main()
        {
            Console.WriteLine("Enter an integer");
            int n = int.Parse(Console.ReadLine());
            Program.EvenOrOdd(n);
        }
    }

Program to check whether the input alphabet is a vowel or not

class Program
    {
        public static void Main()
        {
            char ch;
            Console.WriteLine("Enter your character letter ");
            ch = char.Parse(Console.ReadLine());
            if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' ||
        ch == 'O' || ch == 'u' || ch == 'U')
                Console.WriteLine("{0} is a vowel.", ch);
            else
                Console.WriteLine("{0} is not a vowel.", ch);
        }
    }

Write a program for entering year leap year or not?

class Program
    {
        public static void Main()
        {
            int year;
            Console.WriteLine("Enter a year");
            year = int.Parse(Console.ReadLine());
            if (year % 400 == 0)
                Console.WriteLine("{0} is a leap year.", year);
            else if (year % 100 == 0)
                Console.WriteLine("{0} is not a leap year.", year);
            else if (year % 4 == 0)
                Console.WriteLine("{0} is a leap year.", year);
            else
                Console.WriteLine("{0} is not a leap year.", year);
        }
    }

Find a Sum of digits of the entered number?

class Program
    {
        public static void Main()
        {
            int n, sum = 0, remainder;
            Console.WriteLine("Enter an integer");
            n = int.Parse(Console.ReadLine());
            while (n != 0)
            {
                remainder = n % 10;
                sum = sum + remainder;
                n = n / 10;
            }
            Console.WriteLine("Sum of entered number = {0}", sum);
        }

Swapping of two numbers without third variable

class Program
    {
        public static void Main()
        {
            int x, y;
            Console.WriteLine("Enter two numbers to swap ");
            x = int.Parse(Console.ReadLine());
            y = int.Parse(Console.ReadLine());
            x = x + y;
            y = x - y;
            x = x - y;
            Console.WriteLine("a = {0},b = {1}", x, y);
        }
    }

Swapping two numbers with using temp variable

class Program
    {
        static void Swap(ref int val1, ref int val2)
        {
            int tempval;
            tempval = val2;
            val2 = val1;
            val1 = tempval;
            Console.WriteLine("After swaping a={0},b={1}", val1, val2);
        }
        public static void Main()
        {
            int x = 4; int y = 7;
            Program.Swap(ref x, ref y);
        }
    }

Write a program to reverse a number?

class Program
    {
       
        public static void Main()
        {
            int n, reverse = 0;
            Console.WriteLine("Enter a number to reverse");
            n = int.Parse(Console.ReadLine());
            while (n != 0)
            {
                reverse = reverse * 10;
                reverse = reverse + n % 10;
                n = n / 10;
            }
            Console.WriteLine("Reverse of entered number is = {0}", reverse);
        }
    }

Write a program for finding Palindrome Numbers?

class Program
    {
        public static void Main()
        {
            int n, reversevariable = 0, tempvalue;
            Console.WriteLine("Enter a number to check if it is a palindrome or not");
            n = int.Parse(Console.ReadLine());
            tempvalue = n;
            while (tempvalue != 0)
            {
                reversevariable = reversevariable * 10;
                reversevariable = reversevariable + tempvalue % 10;
                tempvalue = tempvalue / 10;
            }
            if (n == reversevariable)
                Console.WriteLine("{0} is a palindrome number.", n);
            else
                Console.WriteLine("{0} is not a palindrome number.", n);
        }
    }

How do you find the maximum element in an array?

class Program
    {
        public static void Main()
        {
            int[] arrayvalues = new int[100]; int maximumvalue, size, c, position = 1;
            Console.WriteLine("Enter the number of elements in array");
            size = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter {0} integers", size);
            for (c = 0; c < size; c++)
                arrayvalues[c] = int.Parse(Console.ReadLine());
            maximumvalue = arrayvalues[0];
            for (c = 1; c < size; c++)
            {
                if (arrayvalues[c] > maximumvalue)
                {
                    maximumvalue = arrayvalues[c];
                    position = c + 1;
                }
            }
            Console.WriteLine("Maximum element is present at position number {0} and it's value is {0}.",
        position, maximumvalue);

        }
    }

How do you reverse an array example?

class Program
    {
        public static void Main()
        {
            int xvalue, yvalue, zvalue; int[] a = new int[100], b = new int[100];
            Console.WriteLine("Enter the number of elements in array");
            xvalue = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the array elements");
            for (yvalue = 0; yvalue < xvalue; yvalue++)
                a[yvalue] = int.Parse(Console.ReadLine());
            for (yvalue = xvalue - 1, zvalue = 0; yvalue >= 0; yvalue--, zvalue++)
                b[zvalue] = a[yvalue];
            for (yvalue = 0; yvalue < xvalue; yvalue++)
                a[yvalue] = b[yvalue];
            Console.WriteLine("Reverse array is");
            for (yvalue = 0; yvalue < xvalue; yvalue++)
                Console.WriteLine("{0}", a[yvalue]);
        }
    }

Write a program for the prime numbers using function?

class Program
    {
        static Boolean IsPrime(int a)
        {
            int c;
            bool result = false;
            for (c = 2; c <= a - 1; c++)
            {
                if (a % c == 0)
                    result = false;
            }
            if (c == a)
                result = true;
            return result;
        }
        public static void Main()
        {
            Console.WriteLine("Enter a number for checking is it prime or not");
            int c = int.Parse(Console.ReadLine());
            if (Program.IsPrime(c))
            {
                Console.WriteLine("{0} number is prime", c);
            }
            else
            {
                Console.WriteLine("{0} number is not prime", c);
            }
        }
    }

What is an interface ? Give one example of it

An interface is nothing but contractor between two class, once we will inherit that class should be implemented All members in the derived class and is not possible to create an object of class because of incomplete behavior

class Program
    {
        interface tutorialsHelperInterface
        {
            void SetTutorial(int pID, string pName);
            String GetTutorial();
        }

        class tutorialsHelper : tutorialsHelperInterface
        {
            protected int TutorialID;
            protected string TutorialName;

            public void SetTutorial(int pID, string pName)
            {
                TutorialID = pID;
                TutorialName = pName;
            }

            public String GetTutorial()
            {
                return TutorialName;
            }

            static void Main(string[] args)
            {
                tutorialsHelper pTutor = new tutorialsHelper();

                pTutor.SetTutorial(1, ".Net by Tutorials");

                Console.WriteLine(pTutor.GetTutorial());

                Console.ReadKey();
            }
        }
    }

Build your subject with real-time examples

Encapsulation in c# with real time example

Inheritance in c# with real time example

C sharp real time projects