Skip to content

Instantly share code, notes, and snippets.

@csim
Last active August 31, 2020 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
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 MinValue" : time.ToString());
}
}
// What is the output of the short program below? Explain your answer.
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.
}
}
// 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>();
var i = 0;
for (i = 0; i < 10; i++)
{
printers.Add(() => Console.WriteLine(i));
}
foreach (var printer in printers)
{
printer();
}
}
}
// 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?
// 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;
}
}
// 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)
{
Console.WriteLine("ApplicationException 2");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finally");
}
}
}
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> operation)
{
return operation(Radius);
}
}

Write a function in any language (preferable C#) which computes the angle between the hand hands of a clock given a time of day. The function must work for all combinations of hour and minute.

inputs: int hour, int minute
output: float angle (in degrees)

For Example:

At 3:00 PM, the angle is 90 degrees

        |
        |
        |
        .-----

At 3:45 PM, the angle is 180 degrees

   ---------.-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment