Skip to content

Latest commit

 

History

History
101 lines (74 loc) · 2.36 KB

interview-questions.md

File metadata and controls

101 lines (74 loc) · 2.36 KB

Frequently asked interview questions

Table of contents

LINQ

Return value of pure method is not used

var list = new List<int>();

int i = 10;

list.Where(x => x == i).Where(x => x < 25); // Return value of pure method is not used

list.Add(10);
list.Add(15);
list.Add(20);
list.Add(25);

i = 15;

var result = list.ToList();
list.Clear();

Console.WriteLine(result.Count);
Console.WriteLine(result.FirstOrDefault());

// 4
// 10

Boxing and unboxing

Unable to cast object of type 'System.Int32' to type 'System.Int64'

int a = 5;
object o = a;
Console.WriteLine((long)o);

// Runtime error:
// Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int64'.
//   at Program.<Main>$(String[] args) in /Users/aleksei/repositories/ConsoleApp1/src/ConsoleApp2/Program.cs:line 3

try/catch

Cannot jump out of the finally block

try
{
    return 1;
}
finally
{
    return 2; // Compilation error: Cannot jump out of the finally block
}

i++ + ++i

var i = 2;
Console.WriteLine(i++ + ++i);
// 6

Miscellaneous

Foo foo = 1;
Console.WriteLine(foo);

record Foo(int Value)
{
    public static implicit operator Foo(int value) => new(value + 1);

    public static Foo operator *(Foo right, Foo left) => right.Value * left.Value;

    public override string ToString() => (this * 1).Value.ToString();
}
// 5