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
// 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 == DateTime.MinValue ? "time is null" : time.ToString()); | |
} | |
} |
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
// 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. |
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. | |
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? |
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. | |
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(); | |
} | |
} |
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; | |
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. |
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
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; | |
} | |
} |
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. | |
class Program { | |
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