C# Interview Questions - Part One

I would like to dedicate this C# Interview Questions part 1 for people who want to do better in their interviews.

  1. What output do we get when we execute the below code?
    1. object o = null;  
    2. try {  
    3.     int ? i = (int ? ) o;  
    4.     int i2 = i ? ? 0;  
    5.     Console.WriteLine(i2);  
    6.     Console.ReadLine();  
    7. catch (Exception e) {  
    8.     Console.WriteLine(e);  
    9.     Console.ReadLine();  
    10.     throw;  
    11. }  
    Ans: 0
  1. What is nullable type?

    Value types like int, long etc., can be assigned a Null value using this feature in C#. Just append "?" to a datatype, like int? and long?.
  1. When is "volatile" keyword used?

    The volatile keyword is used to ensure that multiple threads can access the current value of a variable at all times.
  1. When is "checked" keyword used?

    The checked keyword is used to enable the overflow checking for integer type arithmetic operations, like addition, subtraction, etc.
  1. When is "static" constructor called?

    A static constructor is called before you create the first instance of a class or reference any static member.
  1. What is a static constructor?

    A static constructor is used to initialize any static data or to perform any action that has to be executed only once.
  1. What is a dictionary?

    Dictionary is used to represent the key-value pairs of the data.
    1. Dictionary<string, Int16> bestSites = new Dictionary<string, Int16>();  
  1. What is the output of the below code?
    1. static void Main(string[] args) {  
    2.     try {  
    3.         Dictionary < Int16, string > bestSites = new Dictionary < short, string > ();  
    4.         bestSites.Add(2, "www.google.com");  
    5.         bestSites.Add(1, "www.c-sharpcorner.com");  
    6.         var ordered = bestSites.OrderBy(a => a.Value);  
    7.         foreach(var site in ordered) {  
    8.             Console.WriteLine("Key ={0},Value={1}", site.Key, site.Value);  
    9.         }  
    10.         bestSites[4] = "www.msn.com";  
    11.         Console.WriteLine(bestSites[4]);  
    12.         Console.ReadLine();  
    13.     } catch (Exception e) {  
    14.         Console.WriteLine(e.GetType());  
    15.         Console.ReadLine();  
    16.         throw;  
    17.     }  
    18. }  

Key =1,Value=www.c-sharpcorner.com

Key =2,Value=www.google.com

www.msn.com

  1. What is named parameter?

Named parameters provide provision for developers to pass parameters in any order.

  1. Here is the sample: static void Main(string[] args) {  
  2.     try {  
  3.         namedParameters(secondParameters: "this is second", thirdParameters: "this is third", firstParameters: "this is first");  
  4.     } catch (Exception e) {  
  5.         Console.WriteLine(e.GetType());  
  6.         throw;  
  7.     }  
  8. }  
  9. public static void namedParameters(string firstParameters, string secondParameters, string thirdParameters) {  
  10.     Console.WriteLine(firstParameters + "," + secondParameters + "," + thirdParameters);  
  11.     Console.ReadLine();  
  12. }  

 

When you execute the above code, you will see the below output.

“this is first, this is second,this is third”

  1. What are optional parameters?

Optional parameters provide provision to mark specific arguments as optional.

Here is the sample,

 

  1. static void Main(string[] args) {  
  2.     try {  
  3.         optionalParameters(secondParameters: "this is second", firstParameters: "this is first");  
  4.     } catch (Exception e) {  
  5.         Console.WriteLine(e.GetType());  
  6.         throw;  
  7.     }  
  8. }  
  9. public static void optionalParameters(string firstParameters, string secondParameters, string thirdParameters = "this is optional") {  
  10.     Console.WriteLine(firstParameters + "," + secondParameters + "," + thirdParameters);  
  11.     Console.ReadLine();  
  12. }  

 

Output

“this is first, this is second,this is optional”

  1. What is the output of the below code?
    1. static void Main(string[] args) {  
    2.     try {  
    3.         Console.WriteLine("Enum value for Orange is: " + (int) Colors.Orange);  
    4.         Console.ReadLine();  
    5.     } catch (Exception e) {  
    6.         Console.WriteLine(e.GetType());  
    7.         throw;  
    8.     }  
    9. }  
    10. public enum Colors {  
    11.     Blue = 1,  
    12.         Green = Blue | 2,  
    13.         Red = Blue | Green,  
    14.         Black = 4, White = 5,  
    15.         Orange = Red | White  
    16. }   

Enum value for Orange is: 7

  1. What is enum?

    Enum is used to define a set of constants.
  1. What is the purpose of Action<T> delegate?

    It encapsulates a method that has a single parameter and doesn’t return any value.
  1. What set of statements do you use in a continue statement?

    while, do, for, or foreach.
  1. What is the purpose of XSD?

    XSD is used to validate an XML document.