Introduction

In this blog, I will discuss some C# coding interview questions which every developer should know. All questions contain a unique coding snippet.

Try to attempt each question carefully and test your C# programming skills. Solving these coding questions should be fun for you.

Also, I’ve listed down some quick C# coding tips which could be quite handy while programming.

C# Basic Question

What Is the error of the below code snippet?
  1. public static void Main()
  2. {
  3. Console.writeLine("Welcome");
  4. Console.ReadKey();
  5. }
Answer

'Console' does not contain a definition for 'writeLine'
  1. public static void Main()
  2. {
  3. Console.WriteLine("Welcome");
  4. Console.ReadKey();
  5. }
Note

C# is a case sensitive language so if you change case it is treated as a different method or variable and Console. WriteLine is present in System namespace in C# where a console is a static class and WriteLine is a method.
Use of Console.WriteLine

There are two console methods to render the text in a console application.
  • Console.Write
  • Console.WriteLine
Console.Write

The use of Console.Write to render text in a console application.

e.g.:

Console.Write("Hello world");
Console.WriteLine

The use of Console.Write to render text with a line break in a console application.

e.g.:

Console.WriteLine("Hello world");
What is an error in the following set of code?
  1. public static void Main(string[] args)
  2. {
  3. Console.WriteLine("Hi")
  4. Console.ReadKey();
  5. }
Answer

All CSharp statements terminate with a semicolon ( ; ) if you miss it the compiler will give you an "Console.WriteLine("Hi") semicolon expected." error. You can write a single or multiple statements on a single line but all statements end with the semicolon. A semicolon is mandatory.
  1. public static void Main(string[] arg)
  2. {
  3. Console.WriteLine("Hi");
  4. Console.ReadKey();
  5. }
What Is the output of the below code snippet?
  1. public static void Main(string[] arg)
  2. {
  3. string a = "Hi , I am {0} {1}";
  4. string b = "software";
  5. string c = "engineer";
  6. Console.WriteLine(a,b,c);
  7. Console.ReadKey();
  8. }
Answer

Hi, I am a software engineer.
Note

In C# {0},{1} is called a placeholder syntax. The main use of placeholder syntax is for string concatenation. In runtime {0} replace with the value b (software) and {1} replace with the value c (engineer).

e.g
  1. Console.WriteLine(a,b,c);
  2. Console.WriteLine("Hi , I am {0} {1}","software","engineer"); // {0}=software and {1} =engineer
  3. Console.WriteLine("Hi , I am software engineer");
What Is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. string a = "{0} should be greater than {1} and less than {2}";
  4. string Age = "Age";
  5. int minAge = 1;
  6. int maxAge = 99;
  7. Console.WriteLine(a,Age,minAge,maxAge);
  8. Console.ReadKey();
  9. }
Answer

Age should be greater than 1 and less than 99.
Note

{0} should be greater than {1} and less than {2}
here {0} replace with Age
{1} replace with 1
{2} replace with 99
So, the final output is "Age should be greater than 1 and less than 99"
Data Types Question
What Is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. short a = 45;
  4. short b = 26;
  5. int c;
  6. c = a * b;
  7. Console.WriteLine(c);
  8. Console.ReadKey();
  9. }
Answer

c=45*26= 1170 so the answer is 1170
Note

Short and int both are integers; short contains only 16 bits but int contains 32 bits -- just double the memory --- that's why integer accepts short (no data loss) but short can't accept int (memory loss) so, if you want to assign int to short; you should use convert function in C#.
What is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. int c = 25;
  4. Console.WriteLine("1st {0}",c);
  5. Console.WriteLine("2nd {0}",c++);
  6. Console.WriteLine("3rd {0}",c);
  7. c--;
  8. Console.WriteLine("4th {0}",c);
  9. Console.WriteLine("5th {0}",++c);
  10. Console.WriteLine("6th {0}",c);
  11. --c;
  12. Console.WriteLine("7th {0}",c);
  13. Console.ReadKey();
  14. }
Answer

1st 25
2nd 25
3rd 26
4th 25
5th 26
6th 26
7th 25
  1. public static void Main(string[] args)
  2. {
  3. int c = 25;
  4. Console.WriteLine("1st {0}",c);
  5. Console.WriteLine("2nd {0}",c++); // c increment by 1 after printing. it's clalled Post increment
  6. Console.WriteLine("3rd {0}",c);
  7. c--;
  8. Console.WriteLine("4th {0}",c);
  9. Console.WriteLine("5th {0}",++c); // first increment c and then then print 1
  10. Console.WriteLine("6th {0}",c);
  11. --c;
  12. Console.WriteLine("7th {0}",c);
  13. Console.ReadKey();
  14. }
Note

There are two ways to increase the value of C#
  • Pre-Increment
  • Post-Increment
Pre-Increment: first increment value +1 then print (++Value)
Post-Increment: print first then increment value +1 (Value++)
Swap two numbers without using the third variable in C#

Answer
  1. public static void Main(string[] args)
  2. {
  3. int fnum = 10, snum = 20;
  4. fnum = fnum + snum;
  5. snum = fnum - snum;
  6. fnum = fnum - snum;
  7. Console.WriteLine("First Number is {0}\n Second Number is {1}",fnum,snum);
  8. Console.ReadKey();
  9. }
Output is:

First Number is 20
Second Number is 10
What is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. int a = 6;
  4. int b = 9;
  5. int c;
  6. Console.WriteLine(++a + b++);
  7. Console.ReadKey();
  8. }
Answer

7+9=16
Note

++a in pre-increment value; first increase value by 1 and then calculate.
b++ in post-increment value; first print or use your value and later increase by 1.
What is the error in the below code?
  1. public static void Main(string[] args)
  2. {
  3. float a = 5.5;
  4. decimal b = 1000.5;
  5. Console.ReadKey();
  6. }
Answer - there are two errors.
  1. Use f suffix in float data type
  2. Use m suffix in decimal data type
  1. public static void Main(string[] args)
  2. {
  3. float a = 5.5f;
  4. decimal b = 1000.5m;
  5. Console.ReadKey();
  6. }
What Is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. string str = "I am human bein";
  4. char c = 'g';
  5. Console.WriteLine(str+c);
  6. Console.ReadKey();
  7. }
Answer - I am a human being

Note

Console.WriteLine(str+c) worked as cancellation

e.g.: "I am a human bein"+"g" = "I am a human being"
What Is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. int a = 10;
  4. int b = 20;
  5. Console.WriteLine(a<b);
  6. Console.ReadKey();
  7. }
Answer

TRUE
WriteLine has several overloaded methods:
  1. public static void WriteLine(bool value);
  2. public static void WriteLine(float value);
  3. public static void WriteLine(int value);
  4. public static void WriteLine(uint value);
  5. public static void WriteLine(long value);
So, if you passed a bool condition into a writeline method it will work as a boolean value in the console application.
What is the error in the below code?
  1. public static void Main(string[] args)
  2. {
  3. const int a = 10;
  4. int b = 20;
  5. const int c= 100 * b;
  6. Console.WriteLine(c);
  7. Console.ReadKey();
  8. }
Answer

Compile time Error: The expression being assigned to 'c' must be constant.
What Is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. string s = "Ankit";
  4. s = s.Insert(5, "Sahu");
  5. Console.WriteLine(s);
  6. Console.ReadKey();
  7. }
Answer

AnkitSahu
Insert method is just inserting the value based on an index
What Is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. string s = "Ankit";
  4. Console.WriteLine(s.IndexOf("k"));
  5. Console.ReadKey();
  6. }
Answer

2
A N K I T
0 1 2 3 4
What Is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. Console.WriteLine(Convert.ToInt32((30 > 20)));
  4. Console.WriteLine(Convert.ToDouble((30 < 20)));
  5. Console.WriteLine(Convert.ToSingle((30 > 20)));
  6. Console.ReadKey();
  7. }
Answer

In this case, all functions behave as a boolean and it behaves as a boolean and returns 1 or 0

Output is:
1
0
1
What is the output of the below code snippet?
  1. public static void Main(string[] args)
  2. {
  3. string s = "Hello"+"?";
  4. Console.WriteLine(string.Compare(s, "Hello?").GetType());
  5. Console.ReadKey();
  6. }
Answer

System.Int32
string.Compare is used to compare a string value; if both are the same it will return true, otherwise false, and GetType will return data type like int, float, double etc.
I hope it is helpful to you. If you face any problems drop a comment or message to me.
Thank you for reading! !!!