C 測試題(答案)

來源: chenm 2022-10-22 09:32:25 [] [博客] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (14980 bytes)
1.  Why (x=y)=z is illegal in C?
 
    A: Because x=y will create rvalue, we cannot assign a value to rvalue.
 
2.  How to call C function in C++?
 
    A: Use extern "C" {
               func();
           }
       because C and C++ have different signatures of functions, C++
       uses mangling mechanism to deal with the function.
 
3.  How to avoid multiple definition error in compiling time? in liking 
time?
 
    A: Use #ifndef something 
           #define something
             ...
           #endif
       in header file
       to avoid the error in compiling time;
       Use extern somevariable to avoid the error in linking time.
 
4.  What's generic pointer? What's generic function pointer?
 
    A: Generic pointer is void*; generic function pointer is void (*)().
 
5.  What's memory leak?
 
    A: Memory leak usually happens when using malloc (new in C++) to locate 
 
         memory in heap, but do not use free (delete in C++) to free the 
memory.
 
6.  Explain the following:
    (1) struct str *(*block())[8];
    (2) double (*(*func)())[2][6];
 
    A: (1) block is a function that return a pointer to an array of 8 pointers
           to structures of type str.
       (2) func is a pointer to a function that return a pointer to a 2 by 6 arrary
           of doubles. 
 
7.  Explain the following type names:
    (1) int *[3]
    (2) int (*)[3]
 
    A: (1) array of three pointers to int;
       (2) pointer to an array of three ints.
 
8.  What's difference of type (signed) char and unsigned char?
 
    A: C states that char is both an integer type and a character type.
       When char is treated as an integer, (signed) char is between -127 
and 127;
       and unsigned char is between 0 and 255.
 
9.  What is the result in j?
      short s = 0xA000;
      unsigned long j;
      j = (unsigned long) s;
 
    A: j = 0xFFFFA000, because of sign extension.
 
10. How many keywords, how many precedences, how many operators, how many basic type conversions in C?
 
    A: 32 keywords, 15 precedences, 41 operators and 16 basic type conversions.
 
 
11. How many name spaces in C?
 
    A:  4  Name space of label, tag, struct or union member and ordinary 
identifier.
 
12. Is the following code legal?
    struct list { in x; struct list *list; } *list;
    fun(struct list *list) {
       list:
         printf("%dn", list->x);
         if ((list = list->list) != NULL)
           goto list;
    }
 
    A: Yes, because the lists belong to different name spaces.
 
13. What's is qualified type?
 
    A: Each (unqualified) type has three qualified versions of its type: 
volatile, constant, and both.
 
14. Explain 
      int *const *const volatile *p;
 
    A: p is a pointer to a constant volatile pointer to a constant pointer 
to an integer.
       p and ***p can be changed, but *p and **p cannot, and *p may be changed 
by some external means because it is volatile.
 
15. Is the following code legal?
       char c[] = "abc"def";
 
    A: It is legal.
 
16. Why we need to avoid 
      int a;
      a = (-9)/5;
 
    A: Because C cannot guarantee that the integer truncating is correct; 
 
       better to use function div().
 
17. Why we need to avoid the code such as:
      y[i] = x[i++];
 
    A: Because the order of evaluation of this expression is unspecified.
 
18. What's difference between sizeof and strlen()?
 
    A: sizeof is keyword which is evaluated and replaced by the size of 
the data element checked.
       When it is applied to a string, it tells us how large the string 
was originally defined to be.
       strlen() is a library function which tells us how many characters 
are currently in it.
 
19. How is the unsigned number 0xaabbccdd layed out in the little endian 
computer(such as PC)?
    In the big endian computer?
 
    A: In the little endian computer: aabbccdd (<- increasing addresses)
       In the big endian computer: ddccbbaa (<- increasing addresses)
 
20. In the code 
    float *p, f[128];
    p = f;
    p = p + 120;
    p is always pointing to f[120] independent on the machine?
 
    A: Yes.
 
21. In the code
    int a;
    a = 4 > 3;
    how many is a?
 
    A: a == 1.
 
22. In the code
    while(-1) {
      fun();
    }
    Will fun() run forever?
 
    A: Yes.
 
23. Is the following code legal?
    struct { int x, y; } *p;
    struct { int x, y; } *q;
    main() { p = q; }
 
    A: No, because the compiler will create different temporary tags for 
the structures.
 
24. Is 123.4.5 a valid preprocessing number?
 
    A: Yes, it sometimes shows up in the code; but it is not a valid numeric 
constant.
 
25. What's the problem with the following macro definition:
    #define twice(x)  (2*x)
 
    A: Should be
       #define twice(x) (2*(x))
       Otherwise, i = twice(a+5)*3;
       will become  i = (2*a+5)*3;
 
26. Is the following code legal?
    switch (n%4) {
       while (n>0) {
         case 0: *x++ = *y++; n--;
         case 3: *x++ = *y++; n--;
         case 2: *x++ = *y++; n--;
         case 1: *x++ = *y++; n--;
       }
    }
 
    A: Yes.  Because any statement can follow the switch clause.
 
27. In the following, which one is legal?
    (1) static int x;
        extern int x;
    (2) extern int x;
        static int x;
 
    A: (1) is legal, and x has internal linkage.
 
28. What's the layout for 2-D arrays in the memory?
 
    A: C uses row-major storage order for arrays (FORTRAN uses column-major)
       float a[3][4] layout is
         a[0][0]
         a[0][1]
         .
         .
 
29. Is the following code legal?
    int b[100], p;
    for (p = b; p < &b[100]; p++) *p = 0;
 
    A: Yes, it is legal to address &b[100], but it is illegal to get the 
content of b[100].
 
30. What's difference when reading a file using binary-mode or text-mode?
 
    A: In PC machine, reading a file using text-mode, we would get fewer 
than the number of requested characters.  Because text-mode would change rn (in PC) to be n.
 
 
The following questions are related to compiler design:
31. Returning the address of a local variable is a common programming error.
    Why the C compiler cannot catch such errors at compile-time?
 
  A: You can design a compiler to catch some of the above error, but cannot 
detect
      the all of the above error.  For example, you cannot catch the errors 
in which 
      the address of a local is first passed as the value of another variable.
 
      Here is a buggy function I have seen several times:
 
     char* toString(int n) {
           char buf[20], *p = buf + sizeof (buf);
           *--p = '';
          do 
             *--p = n%10 + '0';
          while ((n /= 10) != 0);
          return p;
     }
 
32. In the following code:
    int g(int, int);
    int f(n) int n; {
      int i = 0, j;
      if (n == 1) i = 2;
      while (n > 0) {
         j = i + 1;
         n = g(n, i);
      }
      return j;
    }
    Why do we say "whether the while loop terminates is recursively undecidable"
?
 
    A:  Whether the while loop terminates, which is, in general,  recursively 
undecidable.
     The problem is something like Turing Halting problem.
 
33. What are text area, data area and bss area?
 
   A:  Compiler usually uses bss area for uninitialized variables, data 
area for defined
      and initialized variables and text area for source code.
 
 
The following questions are related to the new C 1999 Standard 
34. Explain the new keyword "restrict".
 
   A: The data pointed to by a pointer declared with the restrict
      qualifier may not be pointed to by any other pointer. This
      allows for more effective optimization.
 
 



更多我的博客文章>>>

加跟帖:

  • 標題:
  • 內容(可選項): [所見即所得|預覽模式] [HTML源代碼] [如何上傳圖片] [怎樣發視頻] [如何貼音樂]