In this article, we will discuss on Interview questions for C# programmers.
1. What happens when you compile and run the below program?
public class CsharpArray { public static void Main(string[] args) { int[] i = new int[0]; Console.WriteLine(i[0]); } }
Show Answer
Explaination: System.IndexOutOfRangeException: Index was outside the bounds of the array
2. What will be the result for below program?
public class InterviewProgram { public static void Main(string[] args) { #if (!pi) Console.WriteLine("i"); #else Console.WriteLine("pi not define"); #endif Console.WriteLine("ok"); Console.ReadLine(); } }
Show Answer
Explaination: i
ok
ok
3. What will be the result for below program?
public class CsharpArray { public static void Main(string[] args) { int[] a = new int[3]; a[1] = 10; Object o = a; int[] b = (int[])o; b[1] = 100; Console.WriteLine(a[1]); ((int[])o)[1] = 1000; Console.WriteLine(a[1]); } }
Show Answer
Explaination: 100
1000
Press any key to continue…
1000
Press any key to continue…
4. Predict the output for below program.
namespace InterviewNamespace { public class Generic { public T Field; } public class InterviewProgram { public static void Main(string[] args) { Generic g2 = new Generic(); Generic g3 = new Generic(); g2.Field = 8; g3.Field = 4; if (g2.Field % g3.Field == 0) { Console.WriteLine("Welcome to CsharpStar !!"); } else Console.WriteLine("Prints nothing:"); Console.ReadLine(); } } }
Show Answer
Explaination: Welcome to CsharpStar !!
5. What will be the ouput for below program?
public class CsharpArray { public static void Main(string[] args) { int[] a = { 10, 20, 30, 40, 50, 80 }; Console.WriteLine(a[-1]); } }
Show Answer
Explaination: System.IndexOutOfRangeException: Index was outside the bounds of the array because arry indices always starts with 0
6. Predict the ouput for below program?
public class InterviewProgram { public static void Main(string[] args) { String a = "CsharpStar"; String b = "CSHARPSTAR"; int c; c = a.CompareTo(b); Console.WriteLine(c); } }
Show Answer
Explaination: -1
7. What will be the ouput for below program?
public class CsharpArray { static void methodOne(int[] a) { int[] b = new int[5]; a = b; Console.WriteLine(a.Length); Console.WriteLine(b.Length); } public static void Main(string[] args) { int[] a = new int[10]; methodOne(a); Console.WriteLine(a.Length); } }
Show Answer
Explaination: 5
5
10
Press any key to continue…
5
10
Press any key to continue…
8. Predict the output for below program
public class InterviewProgram { public static void Main(string[] args) { InterviewProgram p = new InterviewProgram(); p.display(2, 3, 8); int[] a = { 2, 56, 78, 66 }; Console.WriteLine("Example of array"); Console.WriteLine("Array elements added are"); p.display(a); Console.ReadLine(); } public void display(params int[] b) { foreach (int i in b) { Console.WriteLine("ARRAY IS HAVING:{0}", i); } } }
Show Answer
Explaination: ARRAY IS HAVING: 2
ARRAY IS HAVING: 3
ARRAY IS HAVING: 8
Example of array
Array elements added are
ARRAY IS HAVING: 2
ARRAY IS HAVING: 56
ARRAY IS HAVING: 78
ARRAY IS HAVING: 66
ARRAY IS HAVING: 3
ARRAY IS HAVING: 8
Example of array
Array elements added are
ARRAY IS HAVING: 2
ARRAY IS HAVING: 56
ARRAY IS HAVING: 78
ARRAY IS HAVING: 66
9. What happens when you compile and run the below program?
public class InterviewProgram { public static void Main(string[] args) { char A = 'P'; char B = Convert.ToChar(76); A++; B++; Console.WriteLine(A + " " + B); Console.ReadLine(); } }
Show Answer
Explaination: Q M
10. What happens when you compile and run the below program?
namespace InterviewNamespace { delegate string del(string str); class sample { public static string DelegateSample(string a) { return a.Replace(',', '*'); } } public class InterviewProgram { public static void Main(string[] args) { del str1 = new del(sample.DelegateSample); string str = str1("Welcome to CsharpStar !!"); Console.WriteLine(str); } } }
Show Answer
Explaination: Welcome to CsharpStar !!
This test was taken from the site http://www.csharpstar.com/csharp-coding-interview-questions-part-1/
Pingback: Interview Freelance/Offshore Programmer as NonTechnical Founder