CSCI 3300
Spring 2009
Exercises for Quiz 5

  1. We discussed the distinction between two areas of memory: the heap and the run-time stack. Suppose that variable p has type int*. Which of the following is a correct C++ statement or statement sequence that makes p point to newly allocated memory in the heap?

    1. *p = new int;
    2. p = new int;
    3. p = new int*;
    4. int x; p = &x;
    5. int x; *p = x;

    Answer

  2. Suppose that p is defined to point to memory as in the preceding question. Which of the following stores 25 in the integer variable to which p points.

    1. p = 25;
    2. p* = 25;
    3. *p = 25;
    4. p = *25;
    5. p *= 25

    Answer

  3. Suppose that a C++ program contains the following statements.

      int* p;
      p[0] = 1;
    
    Which of the following is a true statement about what happens?
    1. Performing those statements can cause unpredictable results
    2. The program containing those statements will get a fatal compile error.
    3. Performing those statements will always lead to a run-time error
    4. There is nothing wrong with those statements; they store 1 into the first cell in an array of integers.

    Answer

  4. In a C++ program, x[y] abbreviates

    1. (*x) + y
    2. x + *y
    3. *(x + y)
    4. (x + y)*
    5. x + y

    Answer

  5. C++ notation p->x abbreviates

    1. p.(*x)
    2. *(p[x])
    3. (*p)[x]
    4. (p*).x
    5. (*p).x

    Answer

  6. Suppose that the following structure type definition is given.

        struct Gadget
        {
          int puff;
          int stuff;
    
          Gadget(int p, int s)
          {
            puff  = p;
            stuff = s;
          }
        };
    
    Which of the following will create a Gadget called g, stored in the run-time stack, whose puff variable contains 4 and whose stuff variable contains 8?
    1. Gadget g(puff = 4, stuff = 8);
    2. Gadget g; puff.g = 4; stuff.g = 8;
    3. Gadget g; puff = 4; stuff = 8;
    4. Gadget g(4, 8);
    5. Gadget g = Gadget(puff = 4, stuff = 8);

    Answer

  7. If g is the Gadget created in the preceding question, which of the following will print the value of the stuff variable in Gadget g on the standard output?

    1. cout << g[stuff];
    2. cout << g.stuff;
    3. cout << stuff.g;
    4. cout << Gadget g.stuff;
    5. cout << stuff(Gadget);

    Answer

  8. Using type Gadget from the previous question, which of the following will create a new Gadget in the heap, with its puff variable holding 39, and its stuff variable holding 4, and make variable w point to the new Gadget?

    1. Gadget* w = new Gadget(39,4);
    2. Gadget* w = new Gadget(4,39);
    3. new Gadget w(39,4);
    4. new Gadget w(4,39);
    5. new Gadget* w(39, 4);

    Answer