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:
1) September 1752
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.
2
can happen quite easily. e.g. If you delete your directory while you're in in it and then execute something. Many programs actually handle this.
noufal@sanitarium% ls
noufal@sanitarium% cd
noufal@sanitarium% mkdir /tmp/foo
noufal@sanitarium% cd /tmp/foo
noufal@sanitarium% touch x
noufal@sanitarium% ls
x
noufal@sanitarium% rm -Rf /tmp/foo
noufal@sanitarium% ls
noufal@sanitarium% touch x
touch: cannot touch `x': No such file or directory
nr 5 happens in the case of leap seconds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With