
Hi habr!
The purpose of this article is self-serving, namely to get an invite. But what to write about, so that the material is individual and not similar to other publications, in which often words just change places. I wanted to think up my own niche, and then somehow during the interview I received a task, where I was asked, through which symbol the interfaces are listed during inheritance, “:” or “,”. Of course, sophisticated workers with such examples will not even go under the cut, so I developed this topic and it turned out to be “trick tasks” that are small and solved without an IDE. It is in my opinion that there are few tasks on the resource, but there are few tasks on 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.First, a couple of very simple, and then more difficult, to calculate. Go!
')
Task 1
What result will return the method?
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
Does the habroclass compile?
public class HabraClass { public int Id { set; get; } }
Options:
a) yes
b) no
Task 4
Does 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) run time exception
c) returns true
d) returns false
Task 5
Does 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
Does this method compile? If so, what will be displayed?
private void Do() { int i = 0; Action<int> action = ref value => { i = i++; }; action(ref i); Console.WriteLine(i); }
Options:
a) compilation error
b) run time exception
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) run time exception
c) runs without errors
Task 10
Calculate what is linqCounter?
private void Do() { int linqCounter = 0; var source = new List<byte> { 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 task analysis when they can be copied to VS and checked. Thank you for your attention, I hope I could confuse you;)
Answers:
Hidden textTask 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