Exam 3 Topics and Practice Problems
CSSE 221 – Fundamentals of Software Development Honors
Fall 2008–2009

Exam format

HALF of this exam is closed-book, in-class, as follows:

HALF of this exam is OPEN-book, TAKE-HOME, as follows:

Exam topics and Practice problems

All the topics and questions below refer to the C programming language, unless otherwise specified.

  1. [5 points] C versus Java: biggest differences. Sample problem:

    1. List three of the biggest differences between C and Java.
    2. What happens when the following executes in C? In its equivalent in Java?
          int x[3];
          int y;
          x[3] = 48;
      
    3. What happens when the following executes in C? Can its equivalent happen in Java?
          int* x;
          int y;
          x = 10;
          y = *x;
      
  2. [5 points] Pointers: basics. Sample problems:

    1. Declare a pointer to a float.
    2. Suppose x is a pointer to a float. Print the value of x, followed by the value of the float that x refers to.
    3. Suppose that y is a char. Print the address of y.
  3. [5 points] Obtaining information from a function: returning values from the function and returning information through pointer arguments. Sample problems:

    1. Explain, by giving an example, how functions "normally" return values. Give both the function prototype and an example of the function call.
    2. Repeat the previous problem, this time using a void function.
  4. [10 points] Arrays versus pointers, and pointer arithmetic. Sample problems:

    1. Consider a function whose specification and prototype is:
              // Returns the sum of the numbers in the given array.
              double foo(double* arr, int arrayLength);
      
      • Implement the function using array (bracket) notation.
      • Implement the function using pointer arithmetic.
  5. [10 points] Dynamic allocation of arrays. Sample problems:

    1. Is the following code legal in C? Why or why not? Is its equivalent legal in Java?
          void foo(int n) {
              double x[n];
              ...
          }
      
    2. Write a statement that declares an array of floats and allocates space for 300 floats in the array.
    3. Write a statement that declares a C-style string and allocates space for it to contain up to 300 characters.
    4. Write the statement that deallocates (i.e. makes available again) the space allocated in the previous problem.
    5. Explain the difference between how space is deallocated (i.e. made available again) in C and in Java.
  6. [10 points] Structures. Sample problems:

    1. What is the most obvious difference between structures in C and classes in Java?
    2. Write a statement that declares a structure type Car that contains the Car's model (at most 20 characters), year, and gasoline tank capacity (to the nearest tenth of a gallon).
    3. Write statements that declares a Car variable and gives values to its fields.
    4. Write statements that declares a pointer to a Car variable, allocates space for the variable to hold a Car, and gives values to the Car's fields.
    5. How would you change the above subproblems if you wanted to allow the Car's model to be a string of unlimited length?
    6. Consider the following code:
              typedef struct {
                  int x;
                  int y;
                  int z;
              } Point3D;
      
              Point3D p1;
              Point3D p2;
      
              p1.x = 10;
              p1.y = 20;
              p1.z = 30;
      
              p2 = p1;
      
      Is p2 a copy of p1, or do p2 and p1 refer to the same place in memory? What about the equivalent situation in Java?
    7. Continuing the previous problem, consider the following code:
              Point3D p1;
      
              p1.x = 10;
              p1.y = 20;
              p1.z = 30;
      
              foo(p1);
      
      When execution returns after the call to foo, is p1.x guaranteed to be 10, or might foo have changed the value of p1.x? What about the equivalent situation in Java?
    8. Consider the code:
              typedef struct {
                  float*   w;
                  float    x;
                  char*    y;
                  char[10] z;
              } Trio;
      
              Trio t;
      
      How much storage is allocated by the declaration of t? That is, how many floats? Chars? Etc?
  7. [5 points] Strings. Sample problems:

    1. What is the terminating character of a C-style string?
    2. Declare a C-style string and give it value java.
    3. What is the minimum number of characters that you must allocate to solve the previous problem?
    4. Consider the following code:
              char* x = "jumbled";
              char* y = x;
              stringSort(y);  // Sorts y, so that y becomes "bdejlmu"
      
      Explain why the above code changes x. Then fix the code so that x does not change (but y still becomes the sorted version of x).

Total: 50 points.