home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18241 < prev    next >
Encoding:
Text File  |  1992-12-12  |  5.1 KB  |  176 lines

  1. Path: sparky!uunet!usc!usc!not-for-mail
  2. From: ajayshah@almaak.usc.edu (Ajay Shah)
  3. Newsgroups: comp.lang.c
  4. Subject: Updated and improved "testing competence" questions
  5. Date: 11 Dec 1992 21:21:56 -0800
  6. Organization: University of Southern California, Los Angeles, CA
  7. Lines: 165
  8. Message-ID: <1gbstkINNfcp@almaak.usc.edu>
  9. NNTP-Posting-Host: almaak.usc.edu
  10.  
  11. Questions for an exam on C (for hiring purposes)
  12. ------------------------------------------------
  13.  
  14. 1. Input is an array of strings.  The function should reverse the
  15. order of the strings in the array.
  16.  
  17. 2.  Input is a pointer to an array of 10 integers.  Sort the array.
  18.  
  19. He can do it anyway he likes, and there is a spread in the
  20. possibilities: buggy < hand- written bubblesort < hand-written
  21. quicksort < using qsort(3).  This helps us seperate the men from the
  22. boys.
  23.  
  24. (Actually, smart algorithms have large constants, so for 10 integers
  25. a simple bubblesort is the fastest.  The question and above answer
  26. will work better if N=10000 or so).
  27.  
  28. 3. How do you set up .c and .h files ? What goes into the .h and what
  29. in the .c ?
  30.  
  31. Building on this:
  32.  
  33. How do you set up two .h files which are mutally dependend, that is:
  34. file A.h defines a structure type A that contains a pointer to B, file
  35. B.h defines a structure type B that contains a pointer to A.
  36.  
  37. 4. What tool will help you decipher 'char (*(*x())[])()'?
  38.  
  39. answer: either "my brain" or "cdecl".
  40.  
  41. cdecl is an acceptable answer, and would demonstrate some knowledge of
  42. tools to actually help get the job done.
  43.  
  44. 5. After
  45.     int i = 2;
  46.     i = ++i;
  47.    what does i equal?
  48.  
  49. Answer: Nothing prevents the compiler from interpreting this as:
  50.  
  51.      i = i + 1;
  52.      ++i;
  53.  
  54. i.e., there is no reason the compiler can't decide to find what value
  55. is to be assigned to the i on the LHS side of the equation and only
  56. then look at the right hand side to increment i by 1.
  57.  
  58.     x = ++i;
  59.  
  60. means that x is assigned the incremented value of i and i is
  61. incremented, it in no way requires an ordering of these two
  62. operations.
  63.  
  64. Another way of saying this is that there are no sequence points during
  65. the evaluation of i = ++i and i is assigned to twice. This makes the
  66. behavior of this expression undefined.
  67.  
  68. 6. (trivial competence measurement)
  69.    Consider this code fragment:
  70.  
  71.    void abc(char *cde)
  72.    {
  73.       for (; *cde; ++cde) *cde = toupper(*cde);
  74.       return cde;
  75.    }
  76.  
  77.    a) Roughly what is he trying?  (convert a string to uppercase).
  78.    b) Comment on the code.
  79.         - it's a void and he's returning cde
  80.         - he is not checking if cde == NULL
  81.         - For portability, you need to check isascii() and islower()
  82.           of *cde before calling toupper().  pre-ANSI compilers do
  83.           not promise that toupper() will work correctly for all inputs.
  84.    c) what header files are needed for this to compile?
  85.         - toupper() is in ctype.h, but you don't need it since the
  86.           implicit declaration `int toupper()' is compatible with
  87.           the truth `int toupper(int)'.
  88.  
  89. 7. There is a C program which contains the line
  90.  
  91. #include "min.h"
  92.  
  93.    Now min.h EITHER contains
  94.         #define min(a, b) (a)<(b)?(a):(b)
  95.    OR
  96.         int min(int a, int b);
  97.    but you don't know which.
  98.  
  99.    Write a test program which will tell you what is in min.h
  100.    You are not allowed to know anything about how your program is linked;
  101.    you are only allowed to write this program, and see it's behaviour in
  102.    compilation and execution.
  103.  
  104. A1:
  105.    {
  106.         int i, j, s;
  107.         i=2; j=4;
  108.         s = min((++i), j);
  109.         printf("%d\n", s);
  110.    }
  111.  
  112. If it's written as #define min(a,b) a<b?a:b then ++i will get executed
  113. twice, giving 4.  Otherwise the min will be 3.
  114.  
  115. A2 : Try compiling
  116.    {
  117.         double p, q, s;
  118.         s = min(p, q);
  119.    }
  120.  
  121. If it compiles, it's a hash-def, else it's a true function.
  122.  
  123. A3: 
  124.    {
  125.    #undef min
  126.        printf("%d\n", min(1, 3));
  127.        return 0;
  128.    }
  129.  
  130. If it compiles, it's a #def, else it's a function.
  131.  
  132. A4: {
  133.     #ifdef min
  134.         printf("It's a #def.\n");
  135.     #else
  136.         printf("It's a function.\n");
  137.     #endif
  138.     }
  139. (This works no matter how min is #defed).
  140.  
  141. A5: {
  142.        (void) (min)(1, 2);
  143.     }
  144. This will break unless min is a function.  (Enclosing min in brackets
  145. prevents it's expansion as a function-like macro).
  146.  
  147. 8. What does this do: printf("shop"+1);
  148.    - prints "hop" to stdout, given you included stdio.h.
  149.  
  150. 9. What is the difference between 'X' and "X"?
  151.    - 'X' is just the integer 88.  "X" is a pointer to a scratch space
  152.      containing 88 followed by 0.
  153.  
  154.  
  155. Authors:
  156.  
  157. 1 and 2 are from wags@cimage.com
  158. 3 is from dekker@dutiag.tudelft.nl (Rene Dekker)
  159. 4 is from mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
  160.      and bandy@netnews.jhuapl.edu (Mike Bandy)
  161. 5 is by jfw@ksr.com (John F. Woods)
  162.      the answer is by dkeisen@leland.Stanford.EDU (Dave Eisen)
  163. 6,7 are ajayshah@rcf.usc.edu
  164. 8 is by Wade Guthrie, wade@nb.rockwell.com
  165. 9 is by Scott P. Riegelhaupt-Herzig, scott@kronos.com
  166.  
  167. With much assorted help from the authors (listed above) and :
  168.  
  169. jutta@opal.cs.tu-berlin.de (Jutta Degener)
  170. matt@centerline.com
  171. carl@montebello.ecom.unimelb.EDU.AU (Carl Brewer)
  172. mouse@thunder.mcrcim.mcgill.edu (der Mouse)
  173.  
  174. -- 
  175. Ajay Shah, (213)749-8133, ajayshah@rcf.usc.edu
  176.