Sample Data Structures Questions
Chapter 2
Abstract Data Types and C++ Classes

Data Structures and Other Objects Using C++
Fourth Edition
by Michael Main and Walter Savitch
ISBN 0132129485


The Purpose of These Questions

These are typical exam questions from Chapter 2 of the textbook. These exact questions might not be on your exam, but if you research and find the right answers to these questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) At the moment there are 14 short answer questions and 11 multiple choice questions in this file.

Short Answers

    Short Answers
    Section 2.1 - 2.2
    Introduction to Classes
  1. Here is part of the throttle declaration:
    
        class throttle
        {
        public:
            void shut_off();
            double flow();
            ...
        private:
            int position;
         };
    
    Write several lines of C++ code to declare a throttle called quiz, activate the member function that shuts off quiz's flow, and then print the current flow from quiz.

  2. The public part of the throttle declaration from class notes is shown below. Mark each function member header as follows: Mark C for any constructor; mark X for any function that is forbidden from changing the throttle's data fields.
    
        class throttle
        {
        public:
            throttle( );
            throttle(int size);
            void shut_off( );
            void shift(int amount);
            double flow( ) const;
            bool is_on( ) const;
            ...
    

  3. What is an automatic default constructor, and what does it do?

  4. What is an inline member function, and when might one be used? Give a small example as part of your answer.

    Short Answers
    Section 2.3
    The Header File and the Implementation File

  5. What is a macro guard in a header file, and what is its purpose? Give a small example as part of your answer.

  6. Suppose that the throttle class is implemented with a header file (throttle.h) and an implementation file (throttle.cxx), and that it is part of the namespace main_savitch_2A. Write the include directive and the using directive that are needed for a program to use the throttle class.

    Short Answers
    Section 2.4
    Classes and Parameters

  7. When is it appropriate to use a const reference parameter? Give a small example as part of your answer.

  8. Suppose a function has a parameter named x, and the body of the function changes the value of x. When should x be a value parameter? When should it be a reference parameter? With this function, could x ever be a const reference parameter?

  9. Write one clear sentence telling me when it would be appropriate to use a const reference parameter.

  10. Write one clear sentence telling me when it would be appropriate to use a reference parameter.

    Short Answers
    Section 2.5
    Operator Overloading and Friends

  11. Here is a small class definition:
    
        class small
        {
        public:
            small( );
            void k() const;
            void h(int i);
            friend f(Small z);
        private:
            int size;
        };
    
    Suppose that x and y are both small objects. Write the word "legal" or "illegal" in each location of this table to indicate whether the indicated statement is legal or illegal in these locations:
    Statement In a main program In the const member function k In the friend function f
    x = y;...
    x.size = y.size;...
    x.size = 3;...
    x.h(42);...

  12. Suppose that you define a new class called foo. For two foo objects x and y, you would like the expression x+y to be a new foo object. What is the prototype of the function that you must write to enable expressions such as x+y?

  13. I have written a class with an operator + which is not a member function, but the operator + implementation does access the private member variables of objects in the class. What did I need to do to gain this access?

  14. Write one clear sentence telling me when it would be appropriate to declare a function as a friend of some class.

Multiple Choice

    Multiple Choice
    Section 2.1 - 2.2
    Introduction to Classes
  1. Here is the start of a class declaration:
    
        class foo
        {
        public:
          void x(foo f);
          void y(const foo f);
          void z(foo f) const;
          ...
    
    Which of the three member functions can alter the PRIVATE member variables of the foo object that activates the function?
    • A. Only x can alter the private member variables of the object that activates the function.
    • B. Only y can alter the private member variables of the object that activates the function.
    • C. Only z can alter the private member variables of the object that activates the function.
    • D. Two of the functions can alter the private member variables of the object that activates the function.
    • E. All of the functions can alter the private member variables of the object that activates the function.

  2. Is it possible for a member function of a class to activate another member function of the same class?
    • A. No.
    • B. Yes, but only public member functions.
    • C. Yes, but only private member functions.
    • D. Yes, both public and private member functions can be activated within another member function.

  3. Can two classes contain member functions with the same name?
    • A. No.
    • B. Yes, but only if the two classes have the same name.
    • C. Yes, but only if the main program does not declare both kinds
    • D. Yes, this is always allowed.

  4. What is the common pattern of class definitions that is used in Chapter 2?
    • A. Member functions and member variables are both private.
    • B. Member functions are private, and member variables are public.
    • C. Member functions are public, and member variables are private.
    • D. Member functions and member variables are both public.

  5. Consider this class definition:
    
        class quiz
        {
        public:
            quiz( );
            int f( );
            int g( ) const;
        private:
            double score;
        };
    
    Which functions can carry out an assignment score=1.0; to the private member variable score?
    • A. Both f and g can carry out the assignment.
    • B. f can carry out the assignment, but not g.
    • C. g can carry out the assignment, but not f.
    • D. Neither f nor g can carry out the assignment.

  6. What is the primary purpose of a default constructor?
    • A. To allow multiple classes to be used in a single program.
    • B. To copy an actual argument to a function's parameter.
    • C. To initialize each object as it is declared.
    • D. To maintain a count of how many objects of a class have been created.

  7. Suppose that the foo class does not have an overloaded assignment operator. What happens when an assignment a=b; is given for two foo objects?
    • A. The automatic assignment operator is used
    • B. The copy constructor is used
    • C. Compiler error
    • D. Run-time error

    Multiple Choice
    Section 2.4
    Classes and Parameters

  8. When should you use a const reference parameter?
    • A. Whenever the data type might be many bytes.
    • B. Whenever the data type might be many bytes, the function changes the parameter within its body, and you do NOT want these changes to alter the actual argument.
    • C. Whenever the data type might be many bytes, the function changes the parameter within its body, and you DO want these changes to alter the actual argument.
    • D. Whenever the data type might be many bytes, and the function does not change the parameter within its body.

  9. Here is a small function definition:
    
        void f(int i, int &k)
        {    
            i = 1;         
            k = 2;
        }                      
    
    Suppose that a main program has two integer variables x and y, which are given the value 0. Then the main program calls f(x,y); What are the values of x and y after the function f finishes?
    • A. Both x and y are still 0.
    • B. x is now 1, but y is still 0.
    • C. x is still 0, but y is now 2.
    • D. x is now 1, and y is now 2.

  10. Here is a function prototype and some possible function calls:
    
        int day_of_week(int year, int month = 1, int day = 1);
        // Possible function calls:
           cout << day_of_week( );
           cout << day_of_week(1995);
           cout << day_of_week(1995, 10);
           cout << day_of_week(1995, 10, 4);
    
    How many of the function calls are legal?
    • A. None of them are legal
    • B. 1 of them is legal
    • C. 2 of them are legal
    • D. 3 of them are legal
    • E. All of them are legal

    Multiple Choice
    Section 2.5
    Operator Overloading and Friends

  11. Which kind of functions can access private member variables of a class?
    • A. Friend functions of the class
    • B. Private member functions of the class
    • C. Public member functions of the class
    • D. All of the above can access private member variables
    • E. None of the above


Data Structures and Other Objects Using C++

Michael Main (main@colorado.edu)
and
Walter Savitch (wsavitch@ucsd.edu)

Thank you for visiting http://www.cs.colorado.edu/~main/questions/chap02q.html
Copyright © 2000 Addison-Wesley Computer and Engineering Publishing Group