home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18307 < prev    next >
Encoding:
Internet Message Format  |  1992-12-14  |  4.6 KB

  1. Path: sparky!uunet!munnari.oz.au!comp.vuw.ac.nz!amigans!acheron!alien
  2. From: alien@acheron.amigans.gen.nz (Ross Smith)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Testing competence of potential hires
  5. Message-ID: <alien.01wq@acheron.amigans.gen.nz>
  6. Date: 14 Dec 92 12:32:34 GMT+12
  7. References: <1g9pm5INNosg@almaak.usc.edu>
  8. Distribution: world
  9. Organization: Muppet Labs
  10. Lines: 143
  11.  
  12. In article <1g9pm5INNosg@almaak.usc.edu> ajayshah@almaak.usc.edu (Ajay Shah) writes:
  13. >I thought up two questions.  What holes can you poke in them?
  14. >
  15. >------------------------------
  16. >
  17. >(trivial competence measurement)
  18. >   Consider this code fragment:
  19. >
  20. >   void abc(char *cde)
  21. >   {
  22. >      for (; *cde; ++cde) *cde = toupper(*cde);
  23. >      return cde;
  24. >   }
  25. >
  26. >   a) Roughly what is he trying?  (convert a string to uppercase).
  27. >   b) What errors will the (ANSI) compiler choke on?
  28. >        - it's a void and he's returning cde
  29. >      What errors will the compiler not find?
  30. >        - he is not checking if cde == NULL
  31.  
  32. And he's using *really* dumb names :-)
  33.  
  34. >   c) what header files are needed for this to compile? (string.h,ctype.h).
  35.  
  36. Wrong, it only needs <ctype.h>.
  37.  
  38. >------------------------------
  39. >
  40. >There is a C program which contains the line
  41. >
  42. >#include "min.h"
  43. >
  44. >   You are told it allows you to use a "function" min(a, b) which returns
  45. >   the smaller of a and b (both integers).
  46. >
  47. >   Write a test program which will tell you whether min is a true function,
  48. >   or a #define.  You are not allowed to know anything about how your
  49. >   program is linked; you are only allowed to write this program, and
  50. >   see it's execution results.
  51. >
  52. >Answer 1:
  53. >   {
  54. >        int i, j, s;
  55. >        i=2; j=4;
  56. >        s = min((++i), j);
  57. >        printf("%d\n", s);
  58. >   }
  59. >
  60. >Answer 2: try compiling
  61. >   {
  62. >        char p,q,s;
  63. >        s = min(p, q);
  64. >   }
  65. >
  66. >A1. If it's written as #define min(a,b) a<b?a:b then ++i will get
  67. >executed twice, giving 4.  Otherwise the min will be 3.
  68.  
  69. Only works if it's an unsafe macro. I don't know any way offhand of writing
  70. the minimum function as a safe macro ... anyone?
  71.  
  72. >A2: if this program compiles it's a hash-def, else it's a true function.
  73.  
  74. Only works on ANSI compilers (and not even then, if the function is defined
  75. old-style, which is still allowed).
  76.  
  77. >Here are the others I hoarded off the net:
  78. >
  79. >1. Input is an array of strings.  The function should reverse the
  80. >order of the strings in the array.
  81. >
  82. >2.  Input is a pointer to an array of 10 integers.  Sort the array.
  83. >
  84. >He can do it anyway he likes, and there is a spread in the
  85. >possibilities: buggy < hand- written bubblesort < hand-written
  86. >quicksort < using qsort(3).  This helps us seperate the men from the
  87. >boys.
  88.  
  89. If somebody used qsort() on an array that was known to have only 10
  90. elements, I'd suspect them of "programming by voodoo" rather than using
  91. their brains.
  92.  
  93. >3. How do you set up .c and .h files ? What goes into the .h and what
  94. >in the .c ?
  95. >
  96. >Building on this:
  97. >
  98. >How do you set up two .h files which are mutally dependend, that is:
  99. >file A.h defines a structure type A that contains a pointer to B, file
  100. >B.h defines a structure type B that contains a pointer to A.
  101.  
  102. Why would you want to?
  103.  
  104. >4. What tool will help you decipher 'char (*(*x())[])()'?
  105. >
  106. >answer: either "my brain" or "cdecl".
  107. >
  108. >cdecl is an acceptable answer, and would demonstrate some knowledge of
  109. >tools to actually help get the job done.
  110. >
  111. >5. After
  112. >       int i = 2;
  113. >       i = ++i;
  114. >   what does i equal?
  115. >
  116. >Answer: Nothing prevents the compiler from interpreting this as:
  117. >
  118. >        i = i + 1;
  119. >        ++i;
  120. >
  121. >i.e., there is no reason the compiler can't decide to find what value
  122. >is to be assigned to the i on the LHS side of the equation and only
  123. >then look at the right hand side to increment i by 1.
  124. >
  125. >       x = ++i;
  126. >
  127. >means that x is assigned the incremented value of i and i is
  128. >incremented, it in no way requires an ordering of these two
  129. >operations.
  130. >
  131. >Another way of saying this is that there are no sequence points during
  132. >the evaluation of i = ++i and i is assigned to twice. This makes the
  133. >behavior of this expression undefined.
  134. >
  135. >
  136. >Authors:
  137. >
  138. >1 and 2 are from wags@cimage.com
  139. >3 is from dekker@dutiag.tudelft.nl (Rene Dekker)
  140. >4 is from mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
  141. >     and bandy@netnews.jhuapl.edu (Mike Bandy)
  142. >5 is jfw@ksr.com (John F. Woods)
  143. >     the answer is by dkeisen@leland.Stanford.EDU (Dave Eisen)
  144. >--
  145. >Ajay Shah, (213)749-8133, ajayshah@rcf.usc.edu
  146.  
  147. --
  148. ...... Ross Smith (Wanganui, NZ) ...... alien@acheron.amigans.gen.nz ......
  149.    Mine is the ship bound for Alpha Centauri
  150.    We must be out of our minds
  151.    For though we are shipmates bound for tomorrow
  152.    Everyone here's flying blind                          (Crystal Gayle)
  153. --
  154.  
  155.