Top 15 C# Interview Question and Answers 2020 [Updated]
I'm sure you have read part 1 C# interview questions and answers. In case you have missed out here is Unseen 41 Interview Q & A
Here are some frequently asked interview questions for beginners, advanced and experienced C# developers candidates to get placed at Top ranked IT companies.
People constantly also search for c# interview questions and answers pdf, c# interview questions for 5 years and above experience, c# interview questions for experienced professionals, c# interview questions with real time examples.
What this page could offer you?
All the real time coding practices asked during interview session.
1. Consider the following class:
public class Employee
{
public Employee()
{
}
public int employeeCounter;
private void AddEmployee()
{
++employeeCounter;
}
}
After the following snippet of code executes,
the value of employeeCounter should be 3.
var e1 = new Employee();
var e2 = new Employee();
var e3 = new Employee();
Which change will maintain the correct number of employees?
public class Employee { public Employee() { } public int employeeCounter; private void AddEmployee() { ++employeeCounter; } }
After the following snippet of code executes,
the value of employeeCounter should be 3.
var e1 = new Employee();
var e2 = new Employee();
var e3 = new Employee();
Which change will maintain the correct number of employees?
Option A
Option A
public static class Employee
{
public Employee()
{
AddEmployee();
}
public int employeeCounter;
private void AddEmployee()
{
++employeeCounter;
}
}
public static class Employee { public Employee() { AddEmployee(); } public int employeeCounter; private void AddEmployee() { ++employeeCounter; } }
Option B
Option B
public class Employee
{
public static Employee()
{
AddEmployee();
}
public int employeeCounter;
public void AddEmployee()
{
++employeeCounter;
}
}
public class Employee { public static Employee() { AddEmployee(); } public int employeeCounter; public void AddEmployee() { ++employeeCounter; } }
Option C
Option C
var e1 = new Employee();
e1.AddEmployee();
var e2 = new Employee();
e2.AddEmployee();
var e3 = new Employee();
e3.AddEmployee();
var e1 = new Employee(); e1.AddEmployee(); var e2 = new Employee(); e2.AddEmployee(); var e3 = new Employee(); e3.AddEmployee();
Option D
public class Employee
{
public Employee()
{
AddEmployee();
}
public static int employeeCounter;
private void AddEmployee()
{
++employeeCounter;
}
}
public class Employee { public Employee() { AddEmployee(); } public static int employeeCounter; private void AddEmployee() { ++employeeCounter; } }
2. Given the following dictionary, which statement would you use to find the value of the element with the key value of "NV"?
IDictionary<string,string> states = new Dictionary<string,string>();
states.Add("NY", "New York");
states.Add("FL", "Florida");
states.Add("CA", "California");
states.Add("NV", "Nevada");
string value = string.Empty;
foreach( var s in states)
{
if (s.Compary(s.Key, "NV"))
{
// do action
}
}
Option A
Option A
string value = string.Empty;
foreach( var s in states)
{
if (s.Compary(s.Key, "NV"))
{
// do action
}
}
Option B
Option B
string value = string.Empty;
foreach( var s in states)
{
if (s.Exists(s.Key, "NV"))
{
// do action
}
}
Option C
Option C
string value = string.Empty;
foreach( var s in states)
{
if (s.Value("NV", out value))
{
// do action
}
}
Option D
string value = string.Empty;
if (states.TryGetValue("NV", out value))
{
// do action
}
3. What is the difference between Action<T> and Func<TResult>?
Action<T> encapsulates a method that has a single parameter and does not return a value. Func<TResult> encapsulates a method that has no parameters and returns a value.
Action<T> is a public method that has a single parameter. Func<TResult> is a delegate method that has no parameters.
Action<T> encapsulates a public method that has a single parameter.Func<TResult> encapsulates a delegate method that has no parameters.
Action<T> encapsulates a method that has a single parameter and returns a value.Func<TResult> encapsulates a method that has no parameters and does not return a value.
4. Given the following dictionary, which statement would you use to find the value of the element with the key value of "NV"?
Option A
Option A
try{
return Convert.ToInt32(someValue);
}
catch(Exception ex){
return ex.Message;}
Option B
Option B
if(someValue.ToString()){
someValue = 0;
}
else if (someValue.ToInt32){
someValue = Convert.ToInt32(someValue);
}
else{
someValue = null;
}
Option C
Option C
Convert.ToInt32(someValue);
Option D
bool isNum = Int32.TryParse(value, out number);
if (isNum){
/*action*/
}
else{
/*action*/
}
5. The following code block uses a LINQ query to identify the value in the Dictionary<TKey, TValue> that is associated with a specific key.
IDictionary<string,string> states = new Dictionary<string,string>();
states.Add("NY", "New York");
states.Add("FL", "Florida");
states.Add("CA", "California");
states.Add("NV", "Nevada");
// LINQ Query
var sn2 = from x in states where x.Key.Contains("NH") select x;
// Write to console
Console.WriteLine(sn2.First().Value);
query throw an unhandled exception:System.InvalidOperationException.
What you change in query to stop exception when item is not found?
IDictionary<string,string> states = new Dictionary<string,string>(); states.Add("NY", "New York"); states.Add("FL", "Florida"); states.Add("CA", "California"); states.Add("NV", "Nevada"); // LINQ Query var sn2 = from x in states where x.Key.Contains("NH") select x; // Write to console Console.WriteLine(sn2.First().Value); query throw an unhandled exception:System.InvalidOperationException.
What you change in query to stop exception when item is not found?
Replace First() with FirstOrDefault()
Check for null using sn2.First().Value before proceeding
Replace First() with Exists()
Replace First() with IfExists() and check for null
6. Review the following class
public class NumberRandomizer
{
public NumberRandomizer()
{
// perform tasks
}
// custom event handler
public event EventHandler NumberRandomized;
}
How to handle NumberRandomized event in another object?
Declare a new instance of the NumberRandomizer class in the object;Attach a custom event handler within the object using +=.
Declare a new instance of the NumberRandomizer class in the event;Attach a custom event within the object using Subscribe().
Extend the calling class with NumberRandomizer;Attach the NumberRandomized event within the object using +=.
Declare a new instance of the NumberRandomizer class in the event;Attach a custom event within the object using Add().
7. Review the following class,How can you modify this class declaration?
CustomCollection<T> implements IComparable<T>? class CustomCollection<T> where T:struct{ //etc.
class CustomCollection<T> : IComparable<T> where T : struct { // etc.
class CustomCollection<T> where T : struct, CustomCollection<T> : IComparable<T> { // etc.
class CustomCollection<T> : IComparable<T>, where T : struct { // etc.
class CustomCollection<T> where T : struct : IComparable<T>{ // etc.
8. Consider this class, The following is a custom System.Windows.Forms.Button
//custom button
public class MyCustomButton : Button
{
public MyCustomButton()
{
// default settings
}
// Mouse up
protected override void OnMouseUp(MouseEventArgs mevent)
{
// do tasks
}
}
Which statement is true regarding the function OnMouseUp?
The base class button OnMouseUp event will not execute.
The base class button OnMouseUp event will execute before the custom OnMouseUp event.
The base class button OnMouseUp event will execute unless the custom OnMouseUp method includes the code base(void);
The base class button OnMouseUp event will execute after the custom OnMouseUp event.
9. You have three DateTime values: dateToCheck, beginningDate, endingDate
Assuming that beginningDate is less than endingDate,
which line of code will return a boolean value indicating
if the dateToCheck value is within the beginningDate and endingDate range?
var dateExists = (dateToCheck >= beginningDate && dateToCheck <= endingDate);
var dateExists = (dateToCheck DateTime.Between(beginningDate, endingDate);
DateTime.Range dateRange = new DateTime.Range(beginningDate, endingDate);var dateExists = dateRange.Exists(dateToCheck);
var dateExists = (dateToCheck >= beginningDate || dateToCheck <= endingDate);
10. Consider this code:
public static class MyClass
{ // etc.
What effect does the keyword static have on the class?
It enforces at compile time that the class can contain only static members and types.
It prevents modifications to any field in this class after instantiation.
It causes an exception to be thrown at run time if more than one instance of the class is instantiated.
It causes an exception to be thrown at run time if a non-static member of the class is accessed.
11. What does the following code illustrate?
public class Selector
{
public static T ChooseSecond<T>(T[] items)
{
return items[1];
}
}
A generic method
A generic collection
A generic constraint
A generic type of the class is accessed.
12. Consider the following variables, Which statement accurately describes their similarity?
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;
DateTime date3 = DateTime.Today;
They are all DateTime structs.
They are not DateTime objects.
They all have DateTimeKind of UTC.
They all have the same time of day when initialized.
13. Consider the following class: You must add a member variable to this class that will maintain the total population of the city as people move in or out and it must be accessible once the class is constructed from all references.
public class City
{
public City()
{
}
}
Which variable declaration will accurately store population count?
public static int totalPopulation;
const int totalPopulation;
static int totalPopulation = 0;
public const int totalPopulation = 0;
14. Consider the following List<Cat>
List<Cat> cats = new List<Cat>
{
new Cat{ Id = 1, Name = "Sylvester", Age=8 },
new Cat{ Id = 2, Name = "Whiskers", Age=2 },
new Cat{ Id = 3, Name = "Sasha", Age=14 }
};
Method written to find and update correct obj from List<Cat> cats.
private void UpdateCat(List<Cat> list, int id, string newName)
{
var c = list.Find(x => x.Id == id);
c.Name = newName;
}
The method rare throws exception. So what caused an exception?
Option A
Option A
var c = list.Find(x => x.Id == id) returns a pointer to the object and not the object itself which means c has not been initialized as a Cat object.
Option B
Option B
The following line of code in the method UpdateCat()
var c = list.Find(x => x.Id == id);
must replace List.Find() with List.Exists()
var c = list.Exists(x => x.Id == id);
Option C
Option C
The following line of code in the method UpdateCat()
var c = list.Find(x => x.Id == id); must be altered to:
var c = new Cat(list.Find(x => x.Id == id));
Option D
If the line var c = list.Find(x => x.Id == id) in the method UpdateCat() does not return an object,
assigning a new name using c.Name = newName will throw a System.NullReferenceException.
15. The following code is not compiling.
DateTime day = DateTime.Now;
string dayNum = day.DayOfWeek.ToString();
int x = dayNum++;
What is the problem with this block of code?
The .ToString() function cannot be called on a DateTime object and then incremented as an integer in line 3.
The Now property in line 1 is not a member of the DateTime class, it's a part of the Date class.
DateTime.DayOfWeek in line 2 returns the name of the day as a string value, not an integer.
The DayOfWeek property in line 2 does not work with DateTime objects.
0 Comments