dimanche 2 décembre 2018

Under what circumstances will the body under the condition be executed?

Long time ago, while being interviewed, I stumbled across this question:

Suppose, somewhere, we have gotten this line of code. Could you provide at least three cases which would cause the body under the condition to be executed?

if (a == 3 && a == 4) { /* ... */ }

My answers were:

  1. It is quite possible that 'a' might be a custom class with overloaded equality operator.

  2. 'a' might be defined (or re-defined) somewhere as #define a b++, and if b equals 3, then it will be evaluated as true.

  3. There must be some kind of multithreading problem, such as, different threads access the same variable.

  4. goto label

I did not ask for the clarification, and also was not told whether my answers were right. How would you proceed? Are there any interesting answers?

Aucun commentaire:

Enregistrer un commentaire