JMU
Sample Questions on C Programming


  1. Indicate whether each of the following statements is true or false:
    (1) _____ All functions in C must return a value.
    (2) _____ C only uses "call by value".
    (3) _____ Uninitialized pointers should never be set to NULL.
  2. Consider the following fragment:
      char *school = "JMU";
      school[0] = 'G';
    
    which is part of a file that contains everything that is needed for it to compile and link.
    1. Will this fragment compile without error? In other words, is this fragment syntactically correct? If not, why not?
    2. If it will compile and link, will it execute without error? If not, why not?
  3. Consider the following fragment:
      char school[] = "JMU";
      school[0] = 'G';
    
    which is part of a file that contains everything that is needed for it to compile and link.
    1. Will this fragment compile without error? In other words, is this fragment syntactically correct? If not, why not?
    2. If it will compile and link, will it execute without error? If not, why not?
  4. Consider the following fragment:
      const char *school = "JMU";
      school[0] = 'G';
    
    which is part of a file that contains everything that is needed for it to compile and link.
    1. Will this fragment compile without error? In other words, is this fragment syntactically correct? If not, why not?
    2. If it will compile and link, will it execute without error? If not, why not?
  5. Consider the following fragment:
      const char school[] = "JMU";
      school[0] = 'G';
    
    which is part of a file that contains everything that is needed for it to compile and link.
    1. Will this fragment compile without error? In other words, is this fragment syntactically correct? If not, why not?
    2. If it will compile and link, will it execute without error? If not, why not?
  6. Consider the following fragment:
      const char *school = "JMU";
      char *temp = school;
      temp[0] = 'G';
    
    which is part of a file that contains everything that is needed for it to compile and link.
    1. Will this fragment compile without error? In other words, is this fragment syntactically correct? If not, why not?
    2. Will the compiler generate any warnings? If so, why?
    3. If it will compile and link, will it execute without error? If not, why not?
    4. Should you ignore warning messages?
  7. What will be printed by the following fragments?
    1. char *s = "James Madison University";
      printf("%s", s+6);
      
    2. char *s = "James\0Madison University";
      printf("%s", s);
      
    3. char *s = "James\0Madison University";
      printf("%s", s+6);
      
  8. Given the following file: rectangle.h
    struct rectangle
    {
      int x;      // x-coordinate of the upper left vertex
      int y;      // y-coordinate of the upper left vertex
      int width;
      int height;
    };
    
    Indicate whether each of the following statements will or will not compile (assuming that everything has been included properly).
    1. rectangle   r;
      
    2. struct rectangle s;
      
    3. struct rectangle t = {0, 0, 50, 100};
      printf("%d\n", t.width);
      
    4. struct rectangle *p;
      p = &t;
      printf("%d\n", p.height);
      
    5. struct rectangle *p;
      p = &t;
      printf("%d\n", p->height);
      
  9. What will be printed if the following is compiled, linked and executed properly? (Hint: Be careful!)
    #include <stdio.h>
    
    
    int max(int a, int b)
    {
       if (a > b) return a;
       else       return b;   
    }
    
    
    int min(int *a, int *b)
    {
       (*a)++;
       (*b) = (*b) + 2;
       
       if (*a < *b) return *a;
       else       return *b;   
    }
    
    
    int 
    main(void)
    {
       int        temp, x, y;
       
       x = 3;
       y = 7;
       
       temp = max(x, y);
       
       printf("max: %d \n", temp);
    
       temp = min(&x, &y);   
    
       printf("min: %d \n", temp);
       printf("x,y: %d,%d \n", x, y);
    
      
    }
      
  10. What will be printed by each of the following (assuming the appropriate headers are included).
    1. int n = 0;
      
      int
      main(void)
      {
        n += 100;
        printf("%d\n", n);
      
        return 0;
      }
      
    2. int n = 0;
      
      int
      main(void)
      {
        int n;
      
        n += 5;
        printf("%d\n", n);
      
        return 0;
      }
      
    3. int n = 0;
      
      int
      main(void)
      {
        int n = 1;
      
        n += 100;
        printf("%d\n", n);
      
        return 0;
      }
      
    4. int 
      total(int x)
      {
        static int total = 0;
      
        total += x;
        return total;
      }
      
      int 
      main(void)
      {
        total(5);
        total(10);
        printf("%d\n", total(15));
        return 0;
      }
      
    5. int n = 0;
      
      int
      main(void)
      {
        extern int n;
      
        n += 100;
        printf("%d\n", n);
      
        return 0;
      }  
      
  11. Given the following
    int
    add(int a, int b)
    {
      return a+b;
    }
    
    1. Write the declaration for a variable named op that could hold a pointer to the function add().
    2. Given your answer to the previous question, assign a pointer to the function add() to op.
    3. Given your answer to the previous questions, use op to add the values 5 and 6.
  12. What will be printed by the following? (Note: Unlike the previous question, this question uses a typdef to make the code more readable.)
    #include <stdio.h>
    
    typedef int binop_t(int, int);
    
    int
    add(int a, int b)
    {
      return a+b;
    }
    
    int multiply(int a, int b)
    {
      return a*b;
    }
    
    int operate(binop_t *op, int a, int b)
    {
      return op(a, b);
    }
    
    int main(void)
    {
      binop_t *op = add;
    
      printf("%d\n", op(5, 6));
      printf("%d\n", operate(multiply, 5, 6));
    
      return 0;
    }
    
  13. What will be printed by the following?
    #include <stdio.h>
    
    void 
    change(int *a, int length)
    {
      int  i;
      for (i=1; i<length; i++)
        {
          a[i] += *(a+i-1);
        }
    }
    
    
    int
    main(void)
    {
      int i;
      int n = 4;
      int value[4];
    
      for (i=0; i<n; i++) 
        {
          value[i] = 100 * i;
        }
    
      change(value, n);
    
      for (i=0; i<n; i++)
        {
          printf("%d\n", value[i]);
        }
    }
    
  14. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       for (int i=0; i<n; i++) sum += a[i];   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  15. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += *(a++);   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  16. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += *(a+i);   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       cout << total(x,4) << "\n";
    
  17. Given the following total() function:
    int total(int *a, int n)
    {
       int sum;
       
       sum = 0;   
       for (int i=0; i<n; i++) sum += *(++a);   
    
       return sum;   
    }
    

    what will be printed by the following? Write "UNKNOWN" if the answer is unknown (e.g., because it depends on the contents of a memory location and there is not enough information to know what is in that memory location).

       int x[4] = {1,2,3,4};
    
       printf("%d\n", total(x,4));
    
  18. Given the following program named print_args.c
    #include <stdio.h>
    
    int
    main(int argc, char **argv)
    {
      int i;
    
      printf("argc: %d\n", argc);
      for (i=0; i<argc; i++)
        {
          printf("argv[%d]: %s\n", i, argv[i]);
        }
      return 0;
    }
    

    and assuming that it is compiled and linked into an executable named print_args, what will be output for each of the following?

    1. ./print_args
    2. ./print_args 2 9
    3. ./print_args 2+9
  19. Complete the following function. You may assume that the "string" is terminated with a '\0'. You may not use any library functions other than malloc().
      /**
       * Returns a substring of the given "string".
       * The correct amount of memory required 
       * by the substring is allocated from the heap.
       *
       * @param s      A pointer to the string
       * @param start  Index of the first char in the substring
       * @param length Length of the substring
       */
      char *substring(const char *s, int start, int length)
      {
    
    
      }
      

Copyright 2017