Blog

C# Programming interview questions

Top 20 C# Programming Interview Questions For SDET

Share:

The following set of frequently asked <C#> SDET interview questions have been compiled to help students and professionals preparing for Certified SDET professional courses, SDET Foundation Training, Certified SDET Architect courses. Refer to full set of Software Development Engineer in Test (SDET) Interview questions at the end of this question – answer.

1. Write a program in C# to find the second most frequent character in string “DevLabsAlliance”.

using System;

namespace DLA
{
    public class SecondFrequentCharacter
    {

        static int NO_OF_CHARS = 256;
        public static void Main(String[] args)
        {
            //String to find second frequent character
            String str = "DevLabsAlliance";
            char res = getSecondMostFreq(str);
            if (res != '\0')
                Console.Write("Second most frequent char is " + res);
            else
                Console.Write("No second most frequent character");
        }

        static char getSecondMostFreq(String str)
        {
            //count number of occurences of every character
            int[] count = new int[NO_OF_CHARS];
            int i;
            for (i = 0; i < str.Length; i++)
                (count[str[i]])++;

            // Traverse through the count[] and find second highest element. 
            int first = 0, second = 0;
            for (i = 0; i < NO_OF_CHARS; i++) { /* If current element is smaller than first then update both first and second */ if (count[i] > count[first])
                {
                    second = first;
                    first = i;
                }

                /* If count[i] is in between first and 
                           second then update second  */
                else if (count[i] > count[second] &&
                       count[i] != count[first])
                    second = i;
            }
            return (char)second;
        }
    }
}

// Output
Second most frequent char is L

2. Write a program in C# to Reverse a String.

using System;
namespace DLA
{
    public class ReverseAString
    {
        public static void Main(String[] args)
        {
            String s = "DevLabsAlliance";
            String revS = "";

            for (int i = s.Length - 1; i >= 0; i--)
            {
                revS = revS + s[i];
            }
            Console.Write("The reversed string is " + revS);
        }
    }
}

//Output
The reversed string is ecnaillAsbaLveD

3. Write a program in C# to reverse the order of words in a string.

using System;

namespace DLA
{
    public class ReverseWordsInString
    {
        public static void Main(String[] args)
        {
            String str = "Learn Lead And Succeed in DevLabsAlliance";
            String[] a = str.Split(' ');

            int k = a.Length - 1;
            int temp = k;

            //String rev = "";
            for (int i = k; temp >= 0; k--)
            {
                Console.Write(a[temp] + "" + ' ');
                --temp;
            }
        }
    }
}

//Output
The reversed string is: DevLabsAlliance in Succeed And Lead Learn

4. Write a program in C# to find the frequency of character in a string.

using System;

namespace DLA
{
    public class FrequencyOfCharacter
    {
        public static void Main(String[] args)
        {
            String str = "DevLabs Alliance is awesome.";
            char ch = 'e';
            int frequency = 0;

            for (int i = 0; i < str.Length; i++)
            {
                if (ch == str[i])
                {
                    ++frequency;
                }
            }
            Console.Write("Frequency of " + ch + " = " + frequency);
        }
    }
}

//Output
Frequency of e = 4

5. Write a program in C# to sort Strings in Lexicographical(Dictionary) order.

using System;
namespace DLA {
    public class SortStringInDictionaryOrder
    {
        public static void Main(String[] args)
        {
            String[] words = { "Ruby", "C", "Python", "Java" };
            for (int i = 0; i < 3; ++i)
            {
                for (int j = i + 1; j < 4; ++j) { if (words[i].CompareTo(words[j]) > 0)
                    {
                        // swap words[i] with words[j]
                        String temp = words[i];
                        words[i] = words[j];
                        words[j] = temp;
                    }
                }
            }
            Console.Write("In lexicographical order:");
            for (int i = 0; i < 4; i++)
            {
                Console.Write(words[i]);
            }
        }
    }
}

//Output
In lexicographical order:
C
Java
Python
Ruby

6. Write a program in C# to replace the SubString with another string.

using System;

namespace DLA
{
    public class ReplaceSubStringWithAnotherString
    {
        public static void Main(String[] args)
        {
            String str = "Learn, Lead and Succeed in DevLabsAlliance";
            String toBeReplaced = "in";
            String toReplacedWith = "with";
            Console.Write(str.Replace(toBeReplaced, toReplacedWith));
        }
    }
}

//Output
Learn, Lead and Succeed with DevLabsAlliance

7. Write a program in C# to find the largest value from the given array.

namespace DLA
{
    public class LargestNumberInArray
    	{
        public static void Main(String[] args)
        {
            int[] arr = { 28, 3, 15, 9, 17, 4, 23, 2 };
            int val = arr[0];
            for (int i = 0; i < arr.Length; i++) { if (arr[i] > val)
                {
                    val = arr[i];
                }
            }
            Console.Write("Largest value in the Given Array is " + val);
        }
    }
}

//Output
Largest value in the Given Array is 28

8. Write a program in C# to find whether a string is palindrome or not.

using System;
namespace DLA
{
    public class Palindrome
    {
        public static void Main(String[] args)
        {
            String original = "nitin", reverse = "";
            int length;
            length = original.Length;
            for (int i = length - 1; i >= 0; i--)
            {
                reverse = reverse + original[i];
            }
            if (original.Equals(reverse))
            {
                Console.Write("The string is palindrome");
            }
            else
            {
                Console.Write("The string is not a palindrome");
            }
        }
    }
}

//Output
The string is palindrome

9. Write a program in C# to find the duplicate characters in a string.

using System;

namespace DLA
{
    public class DuplicateCharacters
    {
        public static void Main(String[] args)
        {
            string str = "DevLabsAlliance";
            int count = 0;
            char[] chars = str.ToCharArray();
            Console.Write("Duplicate characters are:");
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = i + 1; j < str.Length; j++)
                {
                    if (chars[i] == chars[j])
                    {
                        Console.Write(chars[j]);
                        count++;
                        break;
                    }
                }
            }
        }
    }
}

//Output
Duplicate Characters are:
e
a
l

10. Write a program in C# to find the second highest number in an array.

using System;

namespace DLA
{
    public class SecondHighestNumberInArray
    {
        public static void Main(String[] args)
        {
            int[] arr = { 14, 46, 47, 94, 94, 52, 86, 36, 94, 89 };
            int largest = arr[0];
            int secondLargest = arr[0];
            for (int i = 0; i < arr.Length; i++) { if (arr[i] > largest)
                {
                    secondLargest = largest;
                    largest = arr[i];
                }
                else if (arr[i] > secondLargest && arr[i] != largest)
                {
                    secondLargest = arr[i];
                }
            }
            Console.Write("Second largest number in an array is:" + secondLargest);
        }
    }
}

//Output
Second largest number in an array is:89

11. Write a Program in C# to create pyramid of numbers with 5 rows

using System;

namespace DLA
{
    public class PyramidWith5Rows
    {
        public static void Main(String[] args)
        {
            int numberOfRows = 5, Space, number;
            Console.WriteLine("Here Is Your Pyramid");
            for (int i = 1; i <= numberOfRows; i++) 
            {
                for (Space = 1; Space <= (numberOfRows - i); Space++) 
                    Console.Write(" ");
                for (number = 1; number <= i; number++) //increase the value Console.Write(number); for (number = (i - 1); number >= 1; number--) //decrease the value  
                    Console.Write(number);
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

//Output
Here Is Your Pyramid
1
121
12321
1234321
123454321

12. Write a program in C# to count occurrences of each character in a string.

using System;
namespace DLA
{
    public class EachCharCountInString
    {
        static int MAX_CHAR = 256;
        public static void Main(String[] args)
        {
            // Create an array of size 256 i.e. ASCII_SIZE
            int[] count = new int[MAX_CHAR];
            String str = "devlabsalliance";
            int len = str.Length;
            // Initialize count array index
            for (int i = 0; i < len; i++)
                count[str[i]]++;
            // Create an array of given String size
            char[] ch = new char[str.Length];
            for (int i = 0; i < len; i++)
            {
                ch[i] = str[i];
                int find = 0;
                for (int j = 0; j <= i; j++)
                {
                    // If any matches found
                    if (str[i] == ch[j])
                        find++;
                }
                if (find == 1)
                    Console.Write("Number of Occurrence of " + str[i] + " is:" + count[str[i]]);
            }
        }
    }
}

//Output
Number of Occurrence of d is:1
Number of Occurrence of e is:2
Number of Occurrence of v is:1
Number of Occurrence of l is:3
Number of Occurrence of a is:3
Number of Occurrence of b is:1
Number of Occurrence of s is:1
Number of Occurrence of i is:1
Number of Occurrence of n is:1
Number of Occurrence of c is:1

13. Write a program in C# to move all zeros to the end of array.

using System;
namespace DLA
{
    public class pushZerosToEnd
    {
        public static void Main(String[] args)
        {
            int[] arr = { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9 };
            int n = arr.Length;
            int count = 0; // Count of non-zero elements
                           // Traverse the array. If element encountered is
                           // non-zero, then replace the element at index 'count'
                           // with this element
            for (int i = 0; i < n; i++)
                if (arr[i] != 0)
                    arr[count++] = arr[i];
            // Now all non-zero elements have been shifted to
            // front and 'count' is set as index of first 0.
            // Make all elements 0 from count to end.
            while (count < n)
                arr[count++] = 0;
            Console.Write("Array after pushing zeros to the end: ");
            for (int i = 0; i < n; i++)
                Console.Write(arr[i] + " ");
        }
    }
}

//Output
Array after pushing zeros to the end:
1 9 8 4 2 7 6 9 0 0 0 0

14. Write a program in C# to print Floyd’s triangle with 5 rows.

using System;
namespace DLA
{
    public class FloydTriangleWith5Rows
    {
        public static void Main(String[] args)
        {
            int rows = 5;
            int i, j, k = 1;
            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j < i + 1; j++)
                {
                    Console.Write(k++ + " ");
                }
                Console.Write("\n");
            }
        }
    }

//Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

15. Write a program in C# to print all prime factors of a given number.

using System;
namespace DLA
{
    public class AllPrimeFactors
    {
        public static void Main(String[] args)
        {
            int n = 315;
            // Print the number of 2s that divide n
            while (n % 2 == 0)
            {
                Console.Write(2 + " ");
                n /= 2;
            }
            // n must be odd at this point. So we can
            // skip one element (Note i = i +2)
            for (int i = 3; i <= Math.Sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { Console.Write(i + " "); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2)
                Console.Write(n);
        }
    }
}

//Output
3 3 5 7

16. Write a program in C# to print all prime numbers till 15.

using System;
namespace DLA
{
    public class PrimeNumberTill15
    {
        public static void Main(String[] args)
        {
            int maxNum = 15;
            // printing primer numbers till the limit ( 1 to 100)
            Console.Write("Printing prime number from 1 to " + maxNum);
            for (int number = 2; number <= maxNum; number++)
            {
                // print prime numbers only
                if (isPrime(number))
                {
                    Console.Write(number);
                }
            }
        }

        /*
         * Prime number is not divisible by any number other than 1 and itself
         * 
         * @return true if number is prime
         */
        public static bool isPrime(int number)
        {
            for (int i = 2; i < number; i++)
            {
                if (number % i == 0)
                {
                    return false; // number is divisible so its not prime
                }
            }
            return true; // number is prime now
        }
    }
}

//Output
Printing prime number from 1 to 15
2
3
5
7
11
13

17. Write a program in C# to reverse digits of an integer.

using System;
namespace DLA
{
    public class ReverseDigits
    {
        public static void Main(String[] args)
        {
            int num = 3689;
            int rev_num = 0;
            while (num > 0)
            {
                rev_num = rev_num * 10 + num % 10;
                num = num / 10;
            }
            Console.WriteLine("Reverse of no. is " + rev_num);
            Console.ReadLine();
        }
    }
}

//Output
Reverse of no. is 9863

18. Write a program in C# to find the angle between hour and minute hands of a clock at 9.30.

using System;
namespace DLA
{
    public class FindAngleinTime
    {
        public static void Main(string[] args)
        {
            int hours = 9;
            int mins = 30;
            double hourDegrees = (hours * 30) + (mins * 30.0 / 60);
            double minuteDegrees = mins * 6;

            double diff = Math.Abs(hourDegrees - minuteDegrees);

            if (diff > 180)
            {
                diff = 360 - diff;
            }
            Console.Write("The angle between hour hand and minute hand is {0} degrees", diff);
        }
    }
}

//Output
The angle between hour hand and minute hand is 105 degrees

19. Write a program in C# to print Fibonacci Series.

using System;
namespace DLA
{
    public class FibonacciSeries
    {
        public static void Main(string[] args)
        {
            int number = 5;
            int f_0 = 0;
            int f_1 = 1;
            int sum;
            Console.Write("{0} {1} ", f_0, f_1);
            for (int i = 2; i < number; i++)
            {
                sum = f_0 + f_1;
                f_0 = f_1;
                f_1 = sum;
                int value = f_0 + f_1;
                Console.Write("{0} ", value);
            }
        }
    }
}

//Output
0 1 2 3 5

20. Write a program in C# to check whether 371 is Armstrong number or not.

using System;
namespace DLA
{
    public class ArmstrongExample
    {
        public static void Main(string[] args)
        {
            int r, sum = 0, temp;
            int n = 371;
            temp = n;
            while (n > 0)
            {
                r = n % 10;
                sum = sum + (r * r * r);
                n = n / 10;
            }
            if (temp == sum)
            {
                Console.Write("This is Armstrong Number.");
            }
            else
            {
                Console.Write("This is not Armstrong Number.");
            }
        }
    }
}

// Output
This is Armstrong Number.

More SDET Interview Question by Devlabs Alliance

  1. TOP 20 SOFTWARE TESTING INTERVIEW QUESTIONS FOR SDET
  2. TOP 20 JUNIT INTERVIEW QUESTIONS FOR SDET
  3. TOP 50 SELENIUM INTERVIEW QUESTIONS FOR SDET
  4. TOP 50 GIT INTERVIEW QUESTIONS FOR SDET
  5. TOP 20 TESTNG INTERVIEW QUESTIONS FOR SDET
  6. TOP 20 JAVA PROGRAMMING INTERVIEW QUESTIONS FOR SDET
  7. TOP 20 CUCUMBER INTERVIEW QUESTIONS FOR SDET
  8. TOP 20 BASIC JAVA INTERVIEW QUESTIONS FOR SDET
Related Articles
Categories
Latest Articles

Popular Courses

Google Reviews
  • review rating 5  Training provided by the trainers here is completely awesome. Trainer is highly qualified in their areas with practical experience. Also amenities provided during training by institute is great

    thumb Abhishek Tripathi
    6/02/2019
  • review rating 4  It was good session, I left my training room with basic knowledge on all the tools. Tutors have great knowledge and experience on the tools. But three days is not enough time.

    thumb sairam shyamala
    6/30/2019
  • review rating 4  Training provided by the trainers in DevLabs Alliance is completely awesome. Trainers are highly qualified in their areas with practical experience. Since it is 3 days training we have completely got the opportunity to have hands on.

    thumb Salaja K
    6/30/2019
  • review rating 4  Both trainers (Ravi and Naresh) were excellent in their approach. Session was less of a theory and more of a practical. And even the theory part was quite interesting. A bit issue with the volume of information which I had received in just a span of 2-3 days - it seemed a bit over-loaded. But overall, it has given me a great platform to take it forward from here!

    thumb Varenya Varshney
    6/02/2019
  • review rating 5  Excellent Training program. Training courses and trainers are very good. Trainer will explain you as many time you ask the question. Best thing is hands on based training. You learn by doing. Looking for more courses from here.

    thumb Vijay Kumar
    6/30/2019
$('.enrollbtn').on('click', function (){ $( "#announcement" ).tabs( "option", "active", 0 ); $(' a[href="#announcement"]').trigger('click'); });