This is taken from Exercise 19 of The Pragmatic Programmer.
A quick reality check. Which of these “impossible” things can happen?:
-
A month with fewer than 28 days
-
stat(“.” ,&sb) == -1 (that is, can’t access the current directory)
-
In C++:
a = 2; b = 3; if (a + b != 5) exit(1); -
A triangle with an interior angle sum ≠ 180 °
-
A minute that doesn’t have 60 seconds
-
In Java:
(a + 1) <= a
I have some answers but I don’t think my answers are “right” (in the sense that it is the answer that the writers were thinking about when they wrote the question).
This is my attempt:
2) –I think we can skip this, not understanding the question–
3) I don’t know C++, but is it something to do with threading and volatile variables (like Java) ?
4) impossible..
5) Due to occurence of leap Seconds
6)
double a = 100000000000000001d;
I was wondering does anyone know the correct answer to these questions?
3: If a and b are of class type then the overloaded operator+ could do anything (or an overloaded assignment operator, or operator !=, or even an implicit conversion operator for that matter).
4: A triangle on a curved surface does not have an interior angle sum of 180.
6: Overflow in a.
The point of the questions is IMHO to demonstrate that a lot of “impossible” things do actually happen, and that you might not just be being paranoid when you are programming very defensively.