Last active
July 30, 2024 19:43
-
-
Save csim/0c857f0e707985b6896e9332259cb9b9 to your computer and use it in GitHub Desktop.
C# Interview Questions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
static string location = default(string); | |
static DateTime time = default(DateTime); | |
public static void Main() | |
{ | |
Console.WriteLine(location == null ? "location is null" : location); | |
Console.WriteLine(time == DateTime.MinValue ? "time is null" : time.ToString()); | |
} | |
} | |
// What is the output of the short program below? Explain your answer. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var circle1 = new Circle { | |
Radius = 1 | |
}; | |
// Given an instance of the Circle class | |
// Write code to calculate the circumference of the circle (2 * pi * r) | |
// without modifying the Circle class itself. | |
} | |
} | |
public sealed class Circle | |
{ | |
public double Radius { get; set; } | |
public double Calculate(Func<double, double> op) | |
{ | |
return op(Radius); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What is the output of the program below? Explain your answer. | |
using System; | |
using System.Threading.Tasks; | |
public class Program | |
{ | |
private static string _result = "Nothing"; | |
public static void Main() | |
{ | |
SaySomethingAsync(); | |
Console.WriteLine(_result); | |
} | |
public static async Task<string> SaySomethingAsync() | |
{ | |
await Task.Delay(5); | |
_result = "Something"; | |
return "SaySomething exited"; | |
} | |
} | |
// Also, would the answer change if we were to replace await Task.Delay(5); with Thread.Sleep(5)? Why or why not? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What is the output of the program below? Explain your answer. | |
using System; | |
using System.Collections.Generic; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var printers = new List<Action>(); | |
for (int i = 0; i < 10; i++) | |
{ | |
printers.Add(() => Console.WriteLine(i)); | |
} | |
foreach (var printer in printers) | |
{ | |
printer(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
int[] numbers = { 30, 325, 18, 92, 100, 42, 72 }; | |
// Use linq to find which numbers are evenly divisible by 5. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What is the output of the short program below? Explain your answer. | |
using System; | |
public class Person | |
{ | |
public string Name { get; set; } | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var joe = new Person(); | |
joe.Name = "Joe"; | |
var tim = joe; | |
tim.Name = "tim"; | |
Console.WriteLine(joe.Name); | |
Console.WriteLine(tim.Name); | |
var myNumber = 666; | |
DoSomething(myNumber); | |
Console.WriteLine(myNumber); | |
} | |
private static void DoSomething(int someNumber) | |
{ | |
someNumber = 777; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What is the output of the short program below? Explain your answer. | |
using System; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
try | |
{ | |
throw new Exception("Exception 1"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
try | |
{ | |
throw new ApplicationException("Exception 2"); | |
} | |
catch (ApplicationException ex) | |
{ | |
Console.WriteLine("ApplicationException 2"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
finally | |
{ | |
Console.WriteLine("Finally"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment