10 simple tasks in c # with a trick

image

Hi Habr!

The purpose of this article is selfish, namely receiving an invite. But what to write about so that the material is individual and not like other publications, in which often the words just change places. I wanted to come up with my own niche, and somehow at the interview I got a task asking where the character is used to list interfaces during inheritance, ”:” or “,”. Of course, sophisticated habrozhiteli with such examples, even under the cut will not go, so I developed this topic and got the "problem with the catch", which are small and are solved without an IDE. It is precisely the tasks in my opinion that few resources are represented on the resource, and I have never met any tasks on the syntax.

If you find these examples interesting, I’m ready to write more, but they will be more voluminous (10-15 lines), and now I have to publish short ones so that the reader does not lose interest and does not jump off.
At first a couple of very simple, and then more difficult to count. Go!

Task 1
What result will the method return?

private bool SimpleComparison()
{
    return new byte() == new byte();
}

Options:
a) true
b) false

Task 2
What result will return another method?

private bool AnotherSimpleComparison()
{
    return new byte[0] == new byte[0];
}

Options:
a) true
b) false

Task 3
Will the habroclass compile?

public class HabraClass
{
    public int Id { set; get; }
}

Options:
a) yes
b) no

Task 4
Will this method compile? If so, what will he return?

private bool Jeez()
{
    if (null == (object)null != false)
    {
        return true;
    }
    return false;
}

Options:
a) compilation error
b) exception at run time
c) returns true
d) returns false

Task 5
Will this method compile?

private void Hello()
{
    throw;
}

Options:
a) yes
b) no

Task 6
And this one?

private void SafeHello()
{
    try
    {
        throw;
    }
    catch { }
}

Options:
a) yes
b) no

Task 7
What number will be displayed on the screen?

private void Do()
{
    int i = 0;
    i += Increment(ref i);
    Console.WriteLine(i);
}
private int Increment(ref int i)
{
    return i++;
}

Options:
a) 0
b) 1
c) 2

Task 8
Will this method compile? If so, what will be displayed on the screen?

private void Do()
{
    int i = 0;
    Action action = ref value =>
        {
            i = i++;
        };
    action(ref i);
    Console.WriteLine(i);
}

Options:
a) compilation error
b) exception at run time
c) 0
d) 1

Task 9
Will the following code work?

private void Do()
{
    using (var stream = new MemoryStream())
    {
        stream = new MemoryStream();
    }
}

Options:
a) compilation error
b) exception at run time
c) will be executed without errors

Task 10
Count what is linqCounter equal to?

private void Do()
{
    int linqCounter = 0;
    var source = new List { 0, 0, 1, 0, 1 };
    var bytes = source.Where(x =>
    {
        linqCounter ++;
        return x > 0;
    });
    if (bytes.First() == bytes.Last())
    {
        Console.WriteLine(linqCounter--);
    }
    else
    {
        Console.WriteLine(linqCounter++);
    }
}


Conclusions, analysis
I think it makes no sense to lengthen the post and do an analysis of tasks when they can be copied to VS and checked. Thank you for your attention, I hope I could confuse you;)

Answers:
Hidden text
Task 1 - a) true
Task 2 - b) false
Task 3 - a) yes
Task 4 - c) true
Task 5 - b) no
Task 6 - b) no
Task 7 - a) 0
Task 8 - a) compilation error
Task 9 - a) compilation error
Task 10 - 8

Also popular now: