From: ajayshah@almaak.usc.edu (Ajay Shah)
Newsgroups: comp.lang.c
Subject: Updated and improved "testing competence" questions
Date: 11 Dec 1992 21:21:56 -0800
Organization: University of Southern California, Los Angeles, CA
NNTP-Posting-Host: almaak.usc.edu

Questions for an exam on C (for hiring purposes)
------------------------------------------------

1. Input is an array of strings.  The function should reverse the
order of the strings in the array.

2.  Input is a pointer to an array of 10 integers.  Sort the array.

He can do it anyway he likes, and there is a spread in the
possibilities: buggy < hand- written bubblesort < hand-written
quicksort < using qsort(3).  This helps us seperate the men from the
boys.

(Actually, smart algorithms have large constants, so for 10 integers
a simple bubblesort is the fastest.  The question and above answer
will work better if N=10000 or so).

3. How do you set up .c and .h files ? What goes into the .h and what
in the .c ?

Building on this:

How do you set up two .h files which are mutally dependend, that is:
file A.h defines a structure type A that contains a pointer to B, file
B.h defines a structure type B that contains a pointer to A.

4. What tool will help you decipher 'char (*(*x())[])()'?

answer: either "my brain" or "cdecl".

cdecl is an acceptable answer, and would demonstrate some knowledge of
tools to actually help get the job done.

5. After
	int i = 2;
	i = ++i;
   what does i equal?

Answer: Nothing prevents the compiler from interpreting this as:

	 i = i + 1;
	 ++i;

i.e., there is no reason the compiler can't decide to find what value
is to be assigned to the i on the LHS side of the equation and only
then look at the right hand side to increment i by 1.

	x = ++i;

means that x is assigned the incremented value of i and i is
incremented, it in no way requires an ordering of these two
operations.

Another way of saying this is that there are no sequence points during
the evaluation of i = ++i and i is assigned to twice. This makes the
behavior of this expression undefined.

6. (trivial competence measurement)
   Consider this code fragment:

   void abc(char *cde)
   {
      for (; *cde; ++cde) *cde = toupper(*cde);
      return cde;
   }

   a) Roughly what is he trying?  (convert a string to uppercase).
   b) Comment on the code.
        - it's a void and he's returning cde
        - he is not checking if cde == NULL
        - For portability, you need to check isascii() and islower()
          of *cde before calling toupper().  pre-ANSI compilers do
          not promise that toupper() will work correctly for all inputs.
   c) what header files are needed for this to compile?
        - toupper() is in ctype.h, but you don't need it since the
          implicit declaration `int toupper()' is compatible with
          the truth `int toupper(int)'.

7. There is a C program which contains the line

#include "min.h"

   Now min.h EITHER contains
        #define min(a, b) (a)<(b)?(a):(b)
   OR
        int min(int a, int b);
   but you don't know which.

   Write a test program which will tell you what is in min.h
   You are not allowed to know anything about how your program is linked;
   you are only allowed to write this program, and see it's behaviour in
   compilation and execution.

A1:
   {
        int i, j, s;
        i=2; j=4;
        s = min((++i), j);
        printf("%d\n", s);
   }

If it's written as #define min(a,b) a<b?a:b then ++i will get executed
twice, giving 4.  Otherwise the min will be 3.

A2 : Try compiling
   {
        double p, q, s;
        s = min(p, q);
   }

If it compiles, it's a hash-def, else it's a true function.

A3: 
   {
   #undef min
       printf("%d\n", min(1, 3));
       return 0;
   }

If it compiles, it's a #def, else it's a function.

A4: {
    #ifdef min
        printf("It's a #def.\n");
    #else
        printf("It's a function.\n");
    #endif
    }
(This works no matter how min is #defed).

A5: {
       (void) (min)(1, 2);
    }
This will break unless min is a function.  (Enclosing min in brackets
prevents it's expansion as a function-like macro).

8. What does this do: printf("shop"+1);
   - prints "hop" to stdout, given you included stdio.h.

9. What is the difference between 'X' and "X"?
   - 'X' is just the integer 88.  "X" is a pointer to a scratch space
     containing 88 followed by 0.


Authors:

1 and 2 are from wags@cimage.com
3 is from dekker@dutiag.tudelft.nl (Rene Dekker)
4 is from mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
     and bandy@netnews.jhuapl.edu (Mike Bandy)
5 is by jfw@ksr.com (John F. Woods)
     the answer is by dkeisen@leland.Stanford.EDU (Dave Eisen)
6,7 are ajayshah@rcf.usc.edu
8 is by Wade Guthrie, wade@nb.rockwell.com
9 is by Scott P. Riegelhaupt-Herzig, scott@kronos.com

With much assorted help from the authors (listed above) and :

jutta@opal.cs.tu-berlin.de (Jutta Degener)
matt@centerline.com
carl@montebello.ecom.unimelb.EDU.AU (Carl Brewer)
mouse@thunder.mcrcim.mcgill.edu (der Mouse)

-- 
Ajay Shah, (213)749-8133, ajayshah@rcf.usc.edu