C Programming Puzzlers

download C Programming Puzzlers

of 10

Transcript of C Programming Puzzlers

  • 7/28/2019 C Programming Puzzlers

    1/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html

    C Programming Puzzlers

    These questions originally appeared as an article on programmersheaven.com,

    written by Ashok K. Pathak, a researcher at Bharat Electronics Limited (CRL),

    Ghaziabad. They are reproduced here with minor modifications.

    The questions test advanced knowledge of the C language, including somerarely-used features. Effective C programming requires a strong understanding

    of concepts like undefined behavior, recursion, and pointer arithmetic, but the

    deliberately convoluted examples on this page are not representative of real-

    world code, and certainly won't win any prizes for clarity and maintainability.

    Performance on these questions is not a good indicator of broader competence

    in software development. As such, they are unlikely to be useful in an interview

    setting.

    Steve Kobes, 8/25/04 (amended 6/19/11)

    Jump to question: 12345678910111213141516

    1. Consider the following program:

    #include

    #include

    static jmp_buf buf;

    int main(void)

    {

    volatile int b = 3;

    if(setjmp(buf) != 0)

    {

    printf("%d\n", b);

    exit(0);

    }

    b = 5;

    longjmp(buf, 1);}

    What is the output of this program?

    (a) 3

    (b) 5

    (c) 0

    (d) none of the above

    http://stevenkobes.com/ctest.html#q16ahttp://stevenkobes.com/ctest.html#q15ahttp://stevenkobes.com/ctest.html#q14ahttp://stevenkobes.com/ctest.html#q13ahttp://stevenkobes.com/ctest.html#q12ahttp://stevenkobes.com/ctest.html#q11ahttp://stevenkobes.com/ctest.html#q10ahttp://stevenkobes.com/ctest.html#q9ahttp://stevenkobes.com/ctest.html#q8ahttp://stevenkobes.com/ctest.html#q7ahttp://stevenkobes.com/ctest.html#q6ahttp://stevenkobes.com/ctest.html#q5ahttp://stevenkobes.com/ctest.html#q4ahttp://stevenkobes.com/ctest.html#q3ahttp://stevenkobes.com/ctest.html#q2ahttp://stevenkobes.com/ctest.html#q1ahttp://www.programmersheaven.com/articles/pathak/article2.htm
  • 7/28/2019 C Programming Puzzlers

    2/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 2

    Hide answer

    Answer: (b)

    The setjmp function stores context information for a non-local goto, and

    returns 0. The longjmp function transfers control to the setjmp call that initialized

    buf, and execution continues from this point as ifsetjmp had returned 1.

    Note: a non-volatile automatic variable that has been modified aftersetjmp

    becomes indeterminate afterlongjmp. Without the volatile qualifier, this

    programs behavior would be undefined. This rule permits better optimization of

    code.

    2. Consider the following program:

    #include

    int main(void)

    {

    struct node

    {

    int a;

    int b;

    int c;

    };

    struct node s = { 3, 5, 6 };

    struct node *pt = &s;

    printf("%d\n", *(int*)pt);

    return 0;

    }

    What is the output of this program?

    (a) 3

    (b) 5

    (c) 6

    (d) 7

    Show answer

    3. Consider the following code segment:

    int foo(int x, int n)

    {

    int val = 1;

    if (n > 0)

    {

    http://void%28show%282%29%29/http://void%28hide%281%29%29/
  • 7/28/2019 C Programming Puzzlers

    3/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 3

    if (n % 2 == 1)

    val *= x;

    val *= foo(x * x, n / 2);

    }

    return val;

    }

    What function ofx and n is computed by foo?

    (a) xn

    (b) x n

    (c) nx

    (d) none of the above

    Show answer

    4. Consider the following program:

    #include

    int main(void)

    {

    int a[5] = { 1, 2, 3, 4, 5 };

    int *ptr = (int*)(&a + 1);

    printf("%d %d\n", *(a + 1), *(ptr - 1));

    return 0;

    }

    What is the output of this program?

    (a) 2 2

    (b) 2 1

    (c) 2 5

    (d) none of the above

    Show answer

    5. Consider the following program:

    #include

    void foo(int[][3]);

    int main(void)

    {

    int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

    http://void%28show%284%29%29/http://void%28show%283%29%29/
  • 7/28/2019 C Programming Puzzlers

    4/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 4

    foo(a);

    printf("%d\n", a[2][1]);

    return 0;

    }

    void foo(int b[][3])

    {

    ++b;

    b[1][1] = 9;

    }

    What is the output of this program?

    (a) 8

    (b) 9

    (c) 7

    (d) none of the above

    Show answer

    6. Consider the following program:

    #include

    int main(void)

    {

    int a, b, c, d;

    a = 3;

    b = 5;

    c = a, b;d = (a, b);

    printf("c=%d ", c);

    printf("d=%d\n", d);

    return 0;

    }

    What is the output of this program?

    (a) c=3 d=3

    (b) c=5 d=3

    (c) c=3 d=5

    (d) c=5 d=5

    Show answer

    7. Consider the following program:

    http://void%28show%286%29%29/http://void%28show%285%29%29/
  • 7/28/2019 C Programming Puzzlers

    5/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 5

    #include

    int main(void)

    {

    int a[][3] = {1, 2, 3, 4, 5, 6};

    int(*ptr)[3] = a;

    printf("%d %d ", (*ptr)[1], (*ptr)[2]);

    ++ptr;

    printf("%d %d\n", (*ptr)[1], (*ptr)[2]);

    return 0;

    }

    What is the output of this program?

    (a) 2 3 5 6

    (b) 2 3 4 5

    (c) 4 5 0 0

    (d) none of the above

    Show answer

    8. Consider the following code segment:

    #include

    int *f1(void)

    {

    int x = 10; return &x;

    }

    int *f2(void)

    {

    int *ptr;

    *ptr = 10;

    return ptr;

    }

    int *f3(void)

    {

    int *ptr;ptr = malloc(sizeof *ptr);

    return ptr;

    }

    Which of these functions uses pointers incorrectly?

    (a) f3 only

    (b) f1 and f3

    (c) f1 and f2

    http://void%28show%287%29%29/
  • 7/28/2019 C Programming Puzzlers

    6/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 6

    (d) f1, f2, and f3

    Show answer

    9. Consider the following program:

    #include

    int main(void)

    {

    int i = 3;

    int j;

    j = sizeof(++i + ++i);

    printf("i=%d j=%d\n", i, j);

    return 0;

    }

    What is the output of this program on an implementation where int occupies 2

    bytes?

    (a) i=4 j=2

    (b) i=3 j=2

    (c) i=5 j=2

    (d) the behavior is undefined

    Show answer

    10. Consider the following program:

    #include

    void f1(int*, int);

    void f2(int*, int);

    void(*p[2])(int*, int);

    int main(void)

    { int a = 3;

    int b = 5;

    p[0] = f1;

    p[1] = f2;

    p[0](&a, b);

    printf("%d %d ", a, b);

    p[1](&a, b);

    printf("%d %d\n", a, b);

    http://void%28show%289%29%29/http://void%28show%288%29%29/
  • 7/28/2019 C Programming Puzzlers

    7/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 7

    return 0;

    }

    void f1(int *p, int q)

    {

    int tmp = *p;

    *p = q;

    q = tmp;

    }

    void f2(int *p, int q)

    {

    int tmp = *p;

    *p = q;

    q = tmp;

    }

    What is the output of this program?

    (a) 5 5 5 5

    (b) 3 5 3 5

    (c) 5 3 3 5(d) none of the above

    Show answer

    11. Consider the following program:

    #include

    void e(int);

    int main(void)

    {

    int a = 3;

    e(a);

    putchar('\n');

    return 0;

    }

    void e(int n)

    {

    if(n > 0) {

    e(--n);

    printf("%d ", n);

    e(--n);

    }

    }

    What is the output of this program?

    (a) 0 1 2 0

    http://void%28show%2810%29%29/
  • 7/28/2019 C Programming Puzzlers

    8/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 8

    (b) 0 1 2 1

    (c) 1 2 0 1

    (d) 0 2 1 1

    Show answer

    12. Consider the following code segment:typedef int(*test)(float*, float*);

    test tmp;

    What is the type oftmp?

    (a) function taking two pointer-to-float arguments and returning pointer

    to int

    (b) pointer to int

    (c) pointer to function taking two pointer-to-float arguments andreturning int

    (d) none of the above

    Show answer

    13. Consider the following program:

    #include

    int main(void)

    {

    char p;

    char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};

    p = (buf + 1)[5];

    printf("%d\n", p);

    return 0;

    }

    What is the output of this program?

    (a) 5

    (b) 6

    (c) 9

    (d) none of the above

    Show answer

    http://void%28show%2813%29%29/http://void%28show%2812%29%29/http://void%28show%2811%29%29/
  • 7/28/2019 C Programming Puzzlers

    9/10

    6/20/13 C Programming Puzzlers

    stevenkobes.com/ctest.html 9

    14. Consider the following program:

    #include

    void f(char**);

    int main(void)

    {

    char *argv[] = {"ab", "cd", "ef", "gh", "ij", "kl"};

    f(argv);

    return 0;

    }

    void f(char **p)

    {

    char *t;

    t = (p += sizeof(int))[-1];

    printf("%s\n", t);

    }

    What is the output of this program on an implementation where int and all

    pointer types occupy 2 bytes?

    (a) ab

    (b) cd

    (c) ef

    (d) gh

    Show answer

    15. Consider the following program:

    #include

    #include

    int ripple(int n, ...)

    {

    int i, j, k;

    va_list p;

    k = 0;

    j = 1;

    va_start(p, n);

    for(; j < n; ++j)

    {

    i = va_arg(p, int);

    for(; i; i &= i - 1)

    ++k;

    }

    va_end(p);

    http://void%28show%2814%29%29/
  • 7/28/2019 C Programming Puzzlers

    10/10

    6/20/13 C Programming Puzzlers

    t k b / t t ht l 10

    return k;

    }

    int main(void)

    {

    printf("%d\n", ripple(3, 5, 7));

    return 0;

    }

    What is the output of this program?

    (a) 7

    (b) 6

    (c) 5

    (d) 3

    Show answer

    16. Consider the following program:

    #include

    int counter(int i)

    {

    static int count = 0;

    count = count + i;

    return count;

    }

    int main(void)

    { int i, j;

    for(i = 0; i