Interview questions

Arseni Mourzenko
Founder and lead developer
December 13, 2014
Tags: hiring 14 short 50

I'm of­ten sur­prised by the low qual­i­ty of an­swers I get from the can­di­dates dur­ing in­ter­views. Tech­ni­cal ques­tions quick­ly show a deep mis­un­der­stand­ing of the ba­sic con­cepts, and I'm won­der­ing how could it be that a per­son who claims be­ing a pro­fes­sion­al C# pro­gram­mer for five years is un­able to ex­plain what IDisposable is or what sealed key­word is used for.

I al­ways thought that those can­di­dates don't even care prepar­ing them­selves for in­ter­views, but some en­sured me they do. Then I took a look at the web­sites which ap­pear in Google when search­ing for "c# in­ter­view ques­tions". In­deed, the poor qual­i­ty of most web­sites and the com­plete mis­un­der­stand­ing of C# and .NET Frame­work by the au­thors may be ac­count­able for the time we waste do­ing in­ter­views with can­di­dates who won't be hired any­way.

Let's see a few ex­am­ples.

What is the dif­fer­ence be­tween pub­lic, sta­t­ic and void?

All these are ac­cess mod­i­fiers in C#. Pub­lic de­clared vari­ables or meth­ods are ac­ces­si­ble any­where in the ap­pli­ca­tion. [...]

The au­thor has a com­plete mis­un­der­stand­ing of ba­sic C# con­cepts. There are four ac­cess mod­i­fiers in C#: pub­lic, pro­tect­ed, in­ter­nal, pri­vate. A pub­lic method isn't nec­es­sar­i­ly ac­ces­si­ble any­where in the ap­pli­ca­tion. A pub­lic method of an in­ter­nal class can­not be called from a dif­fer­ent name­space.

Void type in­di­cates that the method doesn't re­turn a val­ue. In an unsafe con­text, void can also be used to de­clare a point­er to an un­known type.

The sta­t­ic mod­i­fi­er, when used at the lev­el of a mem­ber, in­di­cates that this mem­ber is sta­t­ic, i.e. be­longs to the type it­self rather than to a spe­cif­ic ob­ject. When used at the lev­el of a class, it in­di­cates that all the mem­bers of the class are sta­t­ic.

Why are strings in C# im­mutable?

Im­mutable means string val­ues can­not be changed once they have been cre­at­ed. Any mod­i­fi­ca­tion to a string val­ue re­sults in a com­plete­ly new string in­stance, thus an in­ef­fi­cient use of mem­o­ry and ex­tra­ne­ous garbage col­lec­tion. The mu­ta­ble System.Text.StringBuilder class should be used when string val­ues will change.

So why are they im­mutable? It seems that the au­thor doesn't know the an­swer, since he doesn't tell us the rea­son strings are im­mutable.

The ac­tu­al rea­son is that im­mutable ob­jects are:

As usu­al, all those rea­sons are giv­en by an ex­cel­lent an­swer. on Stack Over­flow. In­stead of telling us ex­act­ly this, the au­thor blames im­mutabil­i­ty for an in­ef­fi­cient use of mem­o­ry.

What is a de­struc­tor?

A de­struc­tor deletes an ob­ject of the class from mem­o­ry. It is called when the ob­ject is ex­plic­it­ly delet­ed by the code you write, or when the ob­ject pass­es out of scope, which can hap­pen when the pro­gram ex­its a func­tion. The de­struc­tor has the same name as the class, but with a tilde pre­fix.

Com­plete­ly wrong. De­struc­tors are syn­taxic sug­ar for fi­nal­iz­ers. The “when the ob­ject pass­es out of scope” in­di­cates that the au­thor has no un­der­stand­ing of C# what­so­ev­er; oth­er­wise, the au­thor would have heard about Garbage Col­lec­tor and that “The pro­gram­mer has no con­trol over when the de­struc­tor is called be­cause this is de­ter­mined by the garbage col­lec­tor.”

De­scribe del­e­gate in de­tail.

A del­e­gate is an ob­ject that can re­fer to a method. It means that it can hold a ref­er­ence to a method. The method can be called through this ref­er­ence or we can say that a del­e­gate can in­voke the method to which it refers. The same del­e­gate can be used to call dif­fer­ent meth­ods dur­ing the run­time of a pro­gram by sim­ply chang­ing the method to which the del­e­gate refers. The main ad­van­tage of a del­e­gate is that it in­vokes the method at run time rather than com­pile time. Del­e­gate in C# is sim­i­lar to a func­tion point­er in C/C++.

A del­e­gate is not an ob­ject: it is a type. A del­e­gate in­stance is an ob­ject. The rest of the ex­pla­na­tion is so un­clear that I can't even tell if there is some­thing right in it. In all cas­es, when the au­thor as­serts that “The main ad­van­tage of a del­e­gate is that it in­vokes the method at run time rather than com­pile time”, it is clear that he has no un­der­stand­ing of nei­ther del­e­gates, nor the dif­fer­ence be­tween run­time/com­pile time. There is no such a thing as a method in­voked at com­pile time.

So what is a del­e­gate?

It is a type that rep­re­sents ref­er­ences to meth­ods with spe­cif­ic pa­ra­me­ters and a spe­cif­ic re­turn type. A del­e­gate in­stance is a ref­er­ence to a method which has the same pa­ra­me­ters and re­turn type as the del­e­gate.

As for the ad­van­tages, del­e­gates have mul­ti­ple ben­e­fits and there is no sin­gle "main ad­van­tage". For ex­am­ple, it al­lows to de­fine a call­back method.

Looks good. I'm hap­pi­ly sur­prised by the ques­tions and an­swers: the fact that an­swers are short is fine too: we don't ex­pect long, de­tailed an­swers dur­ing the Q&A part of an in­ter­view.