The Chinese University of Hong Kong
Course Examination 1994
Introduction to Computing

Time: 2 hours

Answer ALL the questions:

  1. For the following program, assume it is valid, what is the output? (15%) #include <stdio.h> main () { char x = 'A', y = 'B'; int *p; p = (int *) &x; printf ("%d\n", *p); } If sizeof (int) = 2, sizeof (char) = 1, and x and y are allocated consecutively. Also, the ASCII values of 'A' and 'B' are 48 and 49 respectively.

  2. Represent 12.34 into a 16-bit floating-point format with

    bit 15: sign bit
    bits 14-10: exponent (bias 15)
    bit 9-0: mantissa with hidden bit and format 1.XXX... (8%)

  3. Which is wrong, a or b? Why? (6%)
    int n[10];
    int *m;
    
    n = m;   /* a */
    m = n;   /* b */
    
  4. What is the output of the following: (18%) a. i = 0; while (i++ <= 10); printf ("%d\n", i); b. for (sum = 0, i = 1; i <= 10; sum += i++); printf ("%d %d\n", sum, i); c. i = 1; while (i <= 10) j = ++i; k = i++; printf ("%d %d %d\n", i, j, k); d. short i = -10; printf ("%u\n", i); /* sizeof (short) = 1 */
  5. Define the following sequences:

    f0 = a0

    f1 = a1 + 1
    a0

    f2 = a2 + 1

    a1 + 1


    a0

    f3 = a3 + 1

    a2 + 1


    a1 + 1


    a0

    and so on.

    If a0 , a1 , a2 , ... , an , ... are stored in the array a as [a0 a1 a2 ... an ... ], write a recursive function to calculate fn. (18%)

  6. If x, y and z are integers, write a function which returns 1 if they represent the three sides of a triangle; otherwise return 0. (10%)

  7. What is the output of the following: (15%) #include <stdio.h> int x = 10, y = 5; int h (int *x, int w) { int static z = 2; *x = (++z) + (y++); z += w--; return (y + w); } main () { printf ("%d\n", h (&x, y)); printf ("%d\n", h (&y, x)); printf ("%d %d\n", x, y); }
  8. Sort the following sequences using

    a. Selection sort
    b. Improved bubble sort

    2, 1, 5, 4, 3

    Write down the appearance of the sequences after each pass. (10%)

  9. - END -


    Formatted by Ngai Chi-ho, Formatted by Ngai Chi-ho, Copyright © 1995.

    This site is best viewed with Netscape Navigator 2.0. Download Netscape Now!