Skip to content

Instantly share code, notes, and snippets.

@csim
Last active July 30, 2024 19:43
Show Gist options
  • Save csim/0c857f0e707985b6896e9332259cb9b9 to your computer and use it in GitHub Desktop.
Save csim/0c857f0e707985b6896e9332259cb9b9 to your computer and use it in GitHub Desktop.
C# Interview Questions
// What is the output of the short program below? Explain your answer.
class Program {
static string location = default(string);
static DateTime time = default(DateTime);
static void Main() {
Console.WriteLine(location == null ? "location is null" : location);
Console.WriteLine(time == null ? "time is null" : time.ToString());
}
}
// Given an instance circle of the following class:
public sealed class Circle {
public double Radius { get; set; }
public double Calculate(Func<double, double> op) {
return op(Radius);
}
}
// Write code to calculate the circumference of the circle (2 * pi * r), without modifying the Circle class itself.
// What is the output of the program below? Explain your answer.
class Program {
private static string _result;
static void Main() {
SaySomethingAsync();
Console.WriteLine(_result);
}
static async Task<string> SaySomethingAsync() {
await Task.Delay(5);
_result = "Hello world!";
return "Something";
}
}
// Also, would the answer change if we were to replace await Task.Delay(5); with Thread.Sleep(5)? Why or why not?
// What is the output of the program below? Explain your answer.
delegate void Printer();
static void Main()
{
List printers = new List();
for (int i = 0; i < 10; i++)
{
printers.Add(delegate { Console.WriteLine(i); });
}
foreach (var printer in printers)
{
printer();
}
}
using System.Linq;
static void Main(string[] args)
{
int[] numbers = { 30, 325, 18, 92, 100, 42, 72 };
}
// Use linq to find which numbers are evenly divisible by 5.
public class Person
{
public string Name { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
Person joe = new Person();
joe.Name = "Joe";
Person tim = joe;
tim.Name = "tim";
Console.WriteLine(joe.Name);
Console.WriteLine(tim.Name);
int myNumber = 666;
DoSomething(myNumber);
Console.WriteLine(myNumber);
}
private static void DoSomething(int someNumber)
{
someNumber = 777;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment