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
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();
- }
'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.
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.
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");
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");
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.
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.
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
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"); // {0}=software and {1} =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.
Age should be greater than 1 and less than 99.
Note
{0} should be greater than {1} and less than {2}
{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
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#.
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();
- }

Gajanan ChavhanPosted Apr 18, 2018, 6:12 AM
Simple and Nice...
Chandan KumarPosted Apr 6, 2018, 11:15 PM
Thank You, SirGreat Work from you its really Helpful