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?
- public static void Main()
- {
- Console.writeLine("Welcome");
- Console.ReadKey();
- }
Answer
'Console' does not contain a definition for 'writeLine'
- public static void Main()
- {
- Console.WriteLine("Welcome");
- Console.ReadKey();
- }
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?
- public static void Main(string[] args)
- {
- Console.WriteLine("Hi")
- Console.ReadKey();
- }
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.
- public static void Main(string[] arg)
- {
- Console.WriteLine("Hi");
- Console.ReadKey();
- }
What Is the output of the below code snippet?
- public static void Main(string[] arg)
- {
- string a = "Hi , I am {0} {1}";
- string b = "software";
- string c = "engineer";
- Console.WriteLine(a,b,c);
- Console.ReadKey();
- }
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
- Console.WriteLine(a,b,c);
- Console.WriteLine("Hi , I am {0} {1}","software","engineer");
- Console.WriteLine("Hi , I am software engineer");
What Is the output of the below code snippet?
- public static void Main(string[] args)
- {
- string a = "{0} should be greater than {1} and less than {2}";
- string Age = "Age";
- int minAge = 1;
- int maxAge = 99;
- Console.WriteLine(a,Age,minAge,maxAge);
- Console.ReadKey();
- }
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?
- public static void Main(string[] args)
- {
- short a = 45;
- short b = 26;
- int c;
- c = a * b;
- Console.WriteLine(c);
- Console.ReadKey();
- }
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?
- public static void Main(string[] args)
- {
- int c = 25;
- Console.WriteLine("1st {0}",c);
- Console.WriteLine("2nd {0}",c++);
- Console.WriteLine("3rd {0}",c);
- c--;
- Console.WriteLine("4th {0}",c);
- Console.WriteLine("5th {0}",++c);
- Console.WriteLine("6th {0}",c);
- --c;
- Console.WriteLine("7th {0}",c);
- Console.ReadKey();
- }
Answer
1st 25
2nd 25
3rd 26
4th 25
5th 26
6th 26
7th 25
- public static void Main(string[] args)
- {
- int c = 25;
- Console.WriteLine("1st {0}",c);
- Console.WriteLine("2nd {0}",c++); // c increment by 1 after printing. it's clalled Post increment
- Console.WriteLine("3rd {0}",c);
- c--;
- Console.WriteLine("4th {0}",c);
- Console.WriteLine("5th {0}",++c); // first increment c and then then print 1
- Console.WriteLine("6th {0}",c);
- --c;
- Console.WriteLine("7th {0}",c);
- Console.ReadKey();
- }
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
- public static void Main(string[] args)
- {
- int fnum = 10, snum = 20;
- fnum = fnum + snum;
- snum = fnum - snum;
- fnum = fnum - snum;
- Console.WriteLine("First Number is {0}\n Second Number is {1}",fnum,snum);
- Console.ReadKey();
- }
Output is:
First Number is 20
Second Number is 10
What is the output of the below code snippet?
- public static void Main(string[] args)
- {
- int a = 6;
- int b = 9;
- int c;
- Console.WriteLine(++a + b++);
- Console.ReadKey();
- }
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?
- public static void Main(string[] args)
- {
- float a = 5.5;
- decimal b = 1000.5;
- Console.ReadKey();
- }
Answer - there are two errors.
- Use f suffix in float data type
- Use m suffix in decimal data type
- public static void Main(string[] args)
- {
- float a = 5.5f;
- decimal b = 1000.5m;
- Console.ReadKey();
- }
What Is the output of the below code snippet?
- public static void Main(string[] args)
- {
- string str = "I am human bein";
- char c = 'g';
- Console.WriteLine(str+c);
- Console.ReadKey();
- }
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?
- public static void Main(string[] args)
- {
- int a = 10;
- int b = 20;
- Console.WriteLine(a<b);
- Console.ReadKey();
- }
Answer
TRUE
WriteLine has several overloaded methods:
- public static void WriteLine(bool value);
- public static void WriteLine(float value);
- public static void WriteLine(int value);
- public static void WriteLine(uint value);
- 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?
- public static void Main(string[] args)
- {
- const int a = 10;
- int b = 20;
- const int c= 100 * b;
- Console.WriteLine(c);
- Console.ReadKey();
- }
Answer
Compile time Error: The expression being assigned to
'c' must be constant.
What Is the output of the below code snippet?
- public static void Main(string[] args)
- {
- string s = "Ankit";
- s = s.Insert(5, "Sahu");
- Console.WriteLine(s);
- Console.ReadKey();
- }
Answer
AnkitSahu
Insert method is just inserting the value based on an index
What Is the output of the below code snippet?
- public static void Main(string[] args)
- {
- string s = "Ankit";
- Console.WriteLine(s.IndexOf("k"));
- Console.ReadKey();
- }
Answer
2
A N K I T
0 1 2 3 4
What Is the output of the below code snippet?
- public static void Main(string[] args)
- {
- Console.WriteLine(Convert.ToInt32((30 > 20)));
- Console.WriteLine(Convert.ToDouble((30 < 20)));
- Console.WriteLine(Convert.ToSingle((30 > 20)));
- Console.ReadKey();
- }
Answer
In this case, all functions behave as a boolean and it behaves as a boolean and returns 1 or 0
Output is:
What is the output of the below code snippet?
- public static void Main(string[] args)
- {
- string s = "Hello"+"?";
- Console.WriteLine(string.Compare(s, "Hello?").GetType());
- Console.ReadKey();
- }
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! !!!