C# Coding Interview Questions (Part – 2)




1. What happens when you compile and run the below program?


public class Program
    {
public static void Main(string[] args)
        {
Console.WriteLine(Math.Round(6.5));
Console.WriteLine(Math.Round(9.5));
        }
    }

Output:

6
10

2. Predict the output for below program.


public class Program
    {
public static void Main(string[] args)
        {
varintArray = new int[] { 1, 2, 3, 4, 5 };
vardoubleArray = intArray.Cast<double>().ToList();
for (int i = 0; i <doubleArray.Count; i++)
Console.WriteLine(doubleArray[i]);
        }
    }

Output:

InvalidCastException exception at runtime.Cast is defined as an extension method on the non-generic IEnumerable type instead of IEnumerable. As enumerating an IEnumerable enumerates on object types, and casting from object to double is not permitted.
 

3. What will be the output for below program?


public class Program
    {
public static void Main(string[] args)
        {
byte b = 100;
dynamic d = b;
Console.WriteLine(d.GetType());
            d += 100;
Console.WriteLine(d.GetType());
        }
    }

Output:

System.Byte
System.Int32
 

4. Predict the output for below Program


public class Program
    {
public static void Main(string[] args)
        {
var actions = new List<Action>();
for (int i = 0; i < 4; i++)
actions.Add(() =>Console.WriteLine(i));
foreach (var action in actions)
action();
        }
    }

Output:

4
4
4
4
 

5. What will be the output for below program in release mode and how different is it from debug mode?


public class Program
    {
public  static void Main(string[] args)
        {
boolisComplete = false;

var t = new Thread(() =>
            {
int i = 0;
while (!isComplete) i += 0;
            });

t.Start();

Thread.Sleep(500);
isComplete = true;
t.Join();
Console.WriteLine("complete!");

        }

    }

Output:

The program will hung in release mode and will give an output in debug mode.
complete!

6. Predict the output for below Program.


public class Program
    {
public static  void Main()
        {
var list = new List<int> { 10, 20, 30, 40, 50 };
var collection = new Collection<int>(list);
list.Add(60);
Console.WriteLine(String.Join(",", collection));
        }
    }

Output:

10,20,30,40,50,60

7. What will be the output for below program?


public class Program
    {
public static  void Main()
        {
Nullable<int>nullableVariable = 0;
intintVariable = 1;
Console.WriteLine(nullableVariable.GetType() == intVariable.GetType());
        }
    }

Output:

True

8. Predict the Output for below program


public class Program
    {
public static  void Main()
        {
class1 a = new class3();
Console.WriteLine(a.DoSomething());
        }

public class class1
        {
public virtual string DoSomething()
            {
return "class1";
            }
        }

public class class2 : class1
        {
public override string DoSomething()
            {
return "class2";
            }
        }

public class class3 : class2
        {
public new string DoSomething()
            {
return "Class3";
            }
        }
    }

Output:

class2

9. What will be the output for below program?


public class Program
    {
public static  void Main()
        {
int? i = null;
i++;
Console.WriteLine("Value of i is {0} ", i);
        }      
    }

Output:


Value of i is

10. Predict the output for below program?


public class Program
    {
public static void Main()
        {
doubledVal = 100.1;
Console.WriteLine((int)dVal);

objectobjVal = dVal;
Console.WriteLine((int)objVal);
        }
    }

Output:

100
Unhandled Expecption:
System.InvalidCastException: Specified cast is not valid.
atProgram.Main()


Post a Comment

0 Comments