Study Guide 5 Answers

  1. b
  2. semicolon, ;
  3. b
  4. a
  5. c
  6. a
  7. c
  8. a
  9. a: When choosing between the dot and the arrow operator, locate the structure variable name (fb) and the field name (foo). If the structure variable is a pointer to a structure, join the variable and the field (i.e., select the field in the object being pointed at) with the arrow operator; otherwise, join them (i.e., select) with the dot operator.
  10. b: See the comment above
  11. If you are working on a program by yourself, then you can be flexible (within reason) with the names you give to the elements within the program. However, in the future, you will often work in teams as a student and a computer professional. When this happens, you may be working on one part of a program while your teammates work on other parts.

    When programmers collaborate on a program, they must follow a consistent naming convention; otherwise, the separate parts of the program will not fit together. For example, when one programmer names a structure field hrs while another programmer uses hours for the same field, the program will not compile. Please read the instructions carefully and treat them like a customer's requirements document.

    struct Time
    {
    	int hours;
    	int minutes;
    	int seconds;
    };

    Note that Time begins with an upper case T and that the structure ends with a semicolon.

  12. Like all variable definitions, the statement consists of two parts: the type and the variable name:
    Time time2;

    If the question asked to define a pointer variable, the answer would be

    Time* time2;

    By default, C++ variables are "automatic." The computer automatically allocates memory for them when they come into scope and deallocates the memory when they go out of scope. So, programmers don't typically add the "auto" keyword to a variable definition, but doing so is legal syntax:

    auto Time time2;
  13. Dot and arrow are both selection operators: they select fields or members in objects (instances of structures or classes). If the left-hand operand is a pointer, use the arrow operator:
    time2->hours = 11;
    If the left-hand operand is not a pointer, as it is in this question with an automatic variable, use the dot operator:
    time2.hours = 11;
  14. a: True
  15. c
  16. Unless explicitly changed, enumerations always begin with zero. So, in this example
    enum { alpha, beta, gamma };
  17. d. 3
  18. d. 16
  19. a. 9
  20. b. Naming constants makes programs more understandable but does NOT help the program run more efficiently.
  21. Please see sg02 Answers, question 28.
    Foo temp = x;
    x = y;
    y = temp;
    You may give the temporary variable any appropriate name.