CS 503 Pretest

CS 503, Advanced Programming I

Pretest, 16 January 2007


  1. True or false: the seventh element of an array is the same thing as the array element with index seven. Explain your answer.


    False. C++ uses zero-origin indexing, which means the seventh element of the array has index six; the eight array element has index seven.


  2. A colleague of yours wrote the following code segment
    char * in, out;
    in = "hello";
    out = "good bye";
    
    and was baffled when it wouldn't compile. What did your colleague do wrong?


    Your colleague forget that each variable has to be declared a pointer:

    char * in, * out;
    


  3. Explain why a constructor has to be careful when calling other member functions in the same class.


    When the constructor is called, the class instance is uninitialized; it is the constructor's responsibility initialize the instance. The constructor should only call member functions it knows will access parts of the instance that have been initialized when the method is called.


  4. Write a declaration for a constant, default-initialized instance of the widget class. What is the value of declaring a class instance constant?


    const widget w;
    
    Declaring an class instance const means the instance can't be changed (or at least observably changed).


  5. Explain the problem with declaring friend functions.


    Friend functions violate encapsulation because they exist outside the class with which they are friends but yet have access to all the internal state of class instances.


  6. Full parenthesize the expression
    a - 5 + 6 >= a + 3 * 2
    
    so that the fully-parenthesized expression has the same evaluation order as the given expression. An expression is fully parenthesized when each binary operator operator has its own set of parentheses.


    (((a - 5) + 6) >= (a + (3 * 2)))
    


  7. The expression ++(x + 1) is invalid; explain why.


    The prefix ++ operator adds one to the value of a variable and stores the new value back in the variable. The expression x + 1 isn't a variable; it's a numeric value and can't store anything.


  8. Show how to implement the do-while statement do S while(B) using a while statement.


    bool b = true;
    while (b) {
      S;
      b = B;
      }
    


  9. Write a function that accepts a string and returns the number of vowells in the string.


    unsigned
    vowell_count(const char * str) {
    
      unsigned c = 0;
    
      while (*str)
        if (is_vowell(*str++))
          c++;
    
      return c;
      }
    


  10. Given the function call
    f(a, 10);
    
    which of the two parameters might be declared as a reference in f's declaration. Explain your answer.


    The first parameter might be a reference parameter; the second parameter cannot be, because it's a number and not a variable.


  11. Here is the prototype for a function I'd like you to write:
    int find(int a[], int x);
    
    find() should return the index of the element in a that equals x or -1 if no element in a equals x. Can you write this function? Explain your answer.


    You cannot, because you don't know how big a is.


  12. Suppose C++ did not have function overloading. What effect, if any, would that have on the >> stream extraction operator?


    You would need to write a differently named stream extraction operator for each value type you wanted to read. Because you can't create new operators, you'd have to write the stream extractions as functions, for example

    widget get_widget(std::istream &) { /* whatever */ }
    



This page last modified on 16 January 2007.