Introduction to C++ Programming, Y. Daniel Liang

Chapter 10 More on Objects and Classes


Section 10.2 Immutable Objects and Classes
1  Which of the following statements are true about an immutable object?

A. The contents of an immutable object cannot be modified.
B. All properties of an immutable object must be private.
C. All properties of an immutable object must be of primitive types.
D. An object type property in an immutable object must also be immutable.
E. An immutable object contains no mutator functions.

Section 11.3 Preventing Multiple Declarations
2  Suppose two header files t1.h and t2.h contain the declarations for class T. What happens if you include both header files in your program?

A. You will get multiple declaration error if the header files don't have the include guard.
B. The compile will automatically decides which implementation to use.
C. The program will compile fine and the first header file that is included is used.
D. The program will compile fine and the first header file that is included is used if the header files have the include guard.

Section 10.4 Instance and Static Members
3  Variables that are shared by every instances of a class are __________.

A. public variables
B. private variables
C. instance variables
D. static variables

4  You should add the static keyword in the place of ? in which of the following function:
   #include <iostream>
   using namespace std;
  
   class Test
   {
   public:
     ? int square(int n)
     {
       return n * n;
     }
  
     ? int getAge()
     {
       return age;
     }
  
   private:
     int age;
   };

A. in the square function
B. in the getAge function
C. in both lthe square function and the getAge function
D. none

5  What is wrong in the following code?

   #include <iostream>
   using namespace std;
  
   class Test
   {
   public:
     static int square(int n)
     {
       return n * n;
     }
  
     int getAge()
     {
       return age;
     }
  
   private:
     int age;
   };

   int main()
   {
     cout << Test.square(4);
   }

A. It is a syntax error to invoke Test.square(4).
B. You should replace Test.square(4) with square(4).
C. You should replace Test.square(4) with Test->square(4).
D. You should replace Test.square(4) with Test::square(4).

6  What is wrong in the following code?

   #include <iostream>
   using namespace std;

   class Test
   {
   public:
     static int square(int n)
     {
       return n * n;
     }

     int getAge()
     {
       return age;
     }

     static int k = 5;

   private:
     int age;
   };

   int main()
   {
     cout << Test::square(4);
   }


A. The static variable k cannot be initialized inside the class.
B. You should replace static int k = 5 with static int k and declare int Test::k = 5 outside the Test class.
C. You should replace static int k = 5 with static int k and declare int Test.k = 5 outside the Test class.
D. You should replace static int k = 5 with static int k and declare int Test->k = 5 outside the Test class.

7  A function that is associated with an individual object is called __________.

A. a static function
B. a class function
C. an instance function
D. an object function

Section 10.5 Destructors
8  Which of the following statements are true?

A. Every class has a default constructor if no constructors are defined explicitly.
B. Every class has a default destructor if no destructors are defined explicitly.
C. A class can have only one destructor.
D. The destructor does not have any arguments.

Section 10.6 Copy Constructors
9  Which of the following statements are true?

A. Every class has a copy constructor with the signature ClassName(ClassName &).
B. The copy constructor can be used to create an object initialized with another object?s data.
C. By default, the copy constructor simply copies each data field in one object to its counterpart in the other object.
D. By default, the copy constructor performs a shallow copy.

Section 10.8 friend Functions and friend Classes
10  Which of the following statements are true?

A. Private members of a class cannot be accessed from outside of the class.
B. C++ enables you to use the friend keyword to declare friend functions and friend classes for a class so these functions and classes can access the class?s private members.

11  Analyze the following code:
   #include <iostream>
   using namespace std;
  
   class Date
   {
     friend void p();
  
   private:
     int year;
     int month;
     int day;
   };
  
   void p()
   {
     Date date;
     date.year = 2000;
     cout << date.year;
   }
  
   int main()
   {
     p();
     return 0;
   }

A. The program has a syntax error because year is a private data field in Date.
B. The program compiles and runs fine and display 2000.
C. The program will have a syntax error if the line friend void p() is deleted.
D. Since year is private, you cannot access it using date.year in function p().

Section 10.9 Object Composition
12  Which of the following statements are true?

A. An object can contain another object. The relationship between the two is called composition.
B. Composition is actually a special case of the aggregation relationship.
C. Aggregation models has-a relationships and represents an ownership relationship between two objects.
D. The owner object is called an aggregating object and its class an aggregating class.
E. The subject object is called an aggregated object and its class an aggregated class.

Section 10.12 The C++ vector Class
13  Which of the following statements are true?

A. The array size is fixed in the class declaration.
B. C++ provides the vector class and you can create vector objects.
C. A vector object is just like an array, but a vector?s size can grow automatically if needed.
D. A vector has a no-arg constructor.

14  To declare a vector for holding int values, use __________.

A. vector<int> v;
B. vector v;
C. vector v<int>;
D. vector<int> v();

15  To add an int value 5 to a vector v of integers, use _________.

A. v.add(5);
B. v.insert(5);
C. v.push_back(5);
D. v.append(5);

16  To obtain the size of the vector v, use _______.

A. v.getSize();
B. v.length();
C. v.getLength();
D. v.size();

17  To delete all the elements in a vector v, use _______.

A. v.deleteAll();
B. v.clear();
C. v.eraseAll();
D. v.delele();

18  To obtain the first element in a vector v, use _______.

A. v.at(0);
B. v[0];
C. v.at(1);
D. v[1];

19  What is wrong in the following code?

   #include <iostream>
   #include <vector>
   using namespace std;

   int main()
   {
     vector<int> v;
     cout << v[0];
     return 0;
   }

A. The program has a syntax error on v[0].
B. The program has a runtime error on v[0], because the vector is empty.
C. The program has a syntax error on vector<int> v.
D. The program has a runtime error on vector<int> v.

20  What is wrong in the following code?

   vector<string> v;
   v.push_back("Beijing");
   v.push_back("Tokyo");
   v.push_back("Shanghai");
   v[3] = "Hong Kong";
A. The last line in the code causes a runtime error because there is no element at index 3 in the vector.
B. The last line in the code has a comple error because there is no element at index 3 in the vector.
C. If you replace the last line by v[2] = "Hong Kong", the code will compile and run fine.
D. If you replace the last line by cout << v[3] << endl, the code will compile and run fine.
E. If you replace the last line by cout << v[2] << endl, the code will compile and run fine.