Programming for Perverts

   Today I was looking at a Sun Certified Java Programmer example test (quite an old one - url no longer valid). There were some quite curious questions. I have taken out and modified questions that apply to C# (unless otherwise noted) and left out Java specific ones. Try to guess the compilation result/output! Here they are in C# version:

1.
     int i = 1;
     i <<= 31;
     i >>= 31;
     Console.WriteLine(i);
2.
     Console.WriteLine(Double.NaN == Double.NaN);

     double zero = 0;
     Console.WriteLine(Double.NaN == 3 / zero);
3.
     int i = 10;
     int j = 10;
     bool b = false;

     if (b = 10 == 10)
         Console.WriteLine("true");
     else
         Console.WriteLine("false");
4. This one behaves differently in Java and C#
Java:
     System.out.println(-0.0);
     System.out.println(-0.0 == +0.0);
     System.out.println(Math.min(-0.0,+0.0));
     System.out.println(Math.max(-0.0,+0.0));
     System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
C#:
     Console.WriteLine(-0.0);
     Console.WriteLine(-0.0 == +0.0);
     Console.WriteLine(Math.Min(-0.0, +0.0));
     Console.WriteLine(Math.Max(-0.0, +0.0));
     Console.WriteLine(Math.Min(-0.0, +0.0) == Math.Max(0.0, +0.0));
5.
     int i = 0;
     Console.WriteLine(i++ + ++i);
     Console.WriteLine(i++ + i++);
6.
     Console.WriteLine(Double.PositiveInfinity + Double.NegativeInfinity);
     Console.WriteLine(Double.PositiveInfinity == Double.PositiveInfinity);

And here are the results:

1.
   -1

   When we shift bits to the left we fill the rightmost position with zeros. When we push to the left we fill the leftmost position with the value of the sign bit. That is why we do not end up with the same number. That is called arithmetic shift (as opposed to logical shift). Java has >>> operator for logical right shift. According to Wikipedia in C/C++ the behavior of the >> operator should be specified by the compiler (which is different from undefined).

2.
   False
   False

   According to both C# and Java documentation  NaNs should be checked with the IsNaN method. Bonus points for C# documentation because the issue is easier to find.

3.
   true

   The first reaction here is that you cannot put assignment in if condition so you will get compilation error. Many programmers are under this impression because a = 10 in an if fails. However that is not the case. You can put assignment just like in C/C++ but you cannot put expression that returns non-boolean value. You can put assignment for bool and that is what we do.

4.
in Java:
   -0.0
   true
   -0.0
   0.0
   true
in C#:
   0
   True
   0
   0
   True

   Why and how Java makes a difference between +0.0 and -0.0 is unbeknown to me. On the other hand C# probably makes the same difference but it is clever enough not to show it. Good news is that obviously this cannot affect calculations in Java because the comparison still recognizes them as the same number.

5.
   2
   5

   In Java and C# the + is a sequence point so the value of i is changed before the evaluation of the second operand. So what we've got is incremented i as a second operand from then on the ++ operator, be it prefix or suffix, works as always. This example is more interesting in C because C does not have a sequence point where the + is. The result of this code in C is undefined. It could print 42 or -42 or whatever. Most compilers on x86 machines will output
   2
   4
but that is only because the author of the compiler has chosen the optimal implementation for the target machine. You cannot depend on this behavior even on one machine and even with one and the same compiler. Cool? :)

6.
   NaN
   True

   It seems to me like the first result fits with the indeterminate form infinity – infinity from mathematics, so it results in a NaN. I do not know if it has anything to do with mathematics though. However the second result does not fit with the mathematical concept that infinities are incomparable. What is more if NaNs are incomparable why are infinities comparable (be it only for equality)?

   So how did you like these? And if you liked them did you feel like a pervert?
Tags:   english programming 
Posted by:   Stilgar
03:28 16.05.2008

Comments:

First Previous 1 Next Last 

Posted by   trx   on   01:15 19.05.2008

Явно не съм достатъчно перверзен че да се изкефя на тия неща :)

Posted by   . (Unregistered)   on   14:00 21.05.2008

Pervert!

First Previous 1 Next Last 


Post as:



Post a comment: