Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 10 Object-Oriented Thinking


Section 10.2 The string Class
10.1  What is the output of the following code?

    string s("abc");
    s.append("welcome");
    cout << s << endl;
A. abcwelcome
B. abc
C. welcome
D. welcomeabc

10.2  What is the output of the following code?

    string s("abc");
    s.append("welcome", 0, 3);
    cout << s << endl;
A. abcwelcome
B. abc
C. abcwel
D. welcomeabc

10.3  What is the output of the following code?

    string s("abc");
    s.append("welcome", 3);
    cout << s << endl;
A. abcwelcome
B. abc
C. abcwel
D. welcomeabc

10.4  What is the output of the following code?

    string s("abc");
    s.append(3, 'w');
    cout << s << endl;
A. abcwelcome
B. abc
C. abcwel
D. abcwww

10.5  What is the output of the following code?

    string s("abc");
    s.assign("welcome", 0, 3);
    cout << s << endl;
A. abcwelcome
B. abc
C. welcome
D. abcwww
E. wel

10.6  What is the output of the following code?

    string s("abc");
    s.assign("welcome", 3);
    cout << s << endl;
A. abcwelcome
B. abc
C. welcome
D. abcwww
E. wel

10.7  What is the output of the following code?

    string s("abc");
    s.assign(3, 'w');
    cout << s << endl;
A. abcwww
B. abc
C. www
D. abcwww
E. wel

10.8  What is the output of the following code?

    string s("abcd");
    cout << s.at(1) << endl;
A. a
B. b
C. c
D. d

10.9  What is the output of the following code?

    string s("abcd");
    cout << s.length() << endl;
A. 1
B. 2
C. 3
D. 4
E. 5

10.10  What is the output of the following code?

    string s("abcd");
    cout << s.size() << endl;
A. 1
B. 2
C. 3
D. 4
E. 5

10.11  What is the output of the following code?

    string s("abcd");
    s.clear();
    cout << s.length() << endl;
A. 1
B. 2
C. 3
D. 4
E. 0

10.12  What is the output of the following code?

string s("abcdefg");
cout << s.compare("abb") << endl;
A. 1
B. 0
C. -1
D. -2

10.13  What is the output of the following code?

    string s("abcdefg");
    s.erase(2, 3);
    cout << s << endl;
A. abcd
B. abfg
C. abcg
D. aefg

10.14  What is the output of the following code?

string s("abcdefg");
char s1[15] = "welcome";
s.copy(s1, 30);
cout << s1 << endl;
A. welcome
B. abccome
C. welcomeabc
D. abcwelcome

10.15  What is the output of the following code?

string s("abcdefg");
cout << s.substr(13);
A. abc
B. bcd
C. a
D. c

10.16  What is the output of the following code?

string s("abcdefg");
cout << s.substr(3);
A. abc
B. bcd
C. defg
D. efg

10.17  What is the output of the following code?

string s("abcdefg");
string s1("welcome");
s.swap(s1);
cout << s << endl;
A. abcdefg
B. welcome

10.18  What is the output of the following code?

string s("abcdefag");
cout << s.find("def") << " " << s.find("a"3);
A. 3 0
B. 3 6
C. 2 4
D. 0 0

10.19  What is the output of the following code?

string s("abcdefg");
s.replace(12"wel");
cout << s << endl;
A. abcdefg
B. aweldefg
C. welabcdefg
D. awelbcdefg

10.20  What is the output of the following code?

string s("abcdefg");
s.insert(1"wel");
cout << s << endl;
A. abcdefg
B. aweldefg
C. welabcdefg
D. awelbcdefg

10.21  What is the output of the following code?

string s("abcdefg");
s.insert(13'w');
cout << s << endl;
A. abcdefg
B. aweldefg
C. awwwbcdefg
D. awelbcdefg

Section 10.3 Passing Objects to Functions
10.22  Which of the following statements are correct?
A. C++ allows you to pass a parameter of object type in a function by value.
B. C++ allows you to pass a parameter of object type in a function by reference.
C. Passing objects by reference is commonly used because it saves memory.
D. You should define constant reference parameters for objects that are not supposed to be changed in the function.

10.23  When invoking a function with a reference object parameter, ___________ is passed.
A. the contents of the object
B. a copy of the object
C. the reference of the object
D. the object is copied, then the reference of the copied object

10.24  What will be displayed by the following code?

  #include <iostream>
  using namespace std;

  class Count
  {
  public:
    int count;

    Count(int c)
    {
      count = c;
    }

    Count()
    {
      count = 0;
    }
  };

  void increment(Count c, int times)
  {
    c.count++;
    times++;
  }

  int main()
  {
    Count myCount;
    int times = 0;

    for (int i = 0; i < 100; i++)
      increment(myCount, times);

    cout << "myCount.count is " << myCount.count;
    cout << " times is " << times;

    return 0;
  }
A. myCount.count is 0 times is 0
B. myCount.count is 100 times is 100
C. myCount.count is 0 times is 100
D. myCount.count is 100 times is 0

10.25  What will be displayed by the following code?

  #include <iostream>
  using namespace std;

  class Count
  {
  public:
    int count;

    Count(int c)
    {
      count = c;
    }

    Count()
    {
      count = 0;
    }
  };

  void increment(Count &c, int &n)
  {
    c.count++;
    n++;
  }

  int main()
  {
    Count myCount;
    int times = 0;

    for (int i = 0; i < 100; i++)
      increment(myCount, times);

    cout << "myCount.count is " << myCount.count;
    cout << " times is " << times;

    return 0;
  }
A. myCount.count is 0 times is 0
B. myCount.count is 100 times is 100
C. myCount.count is 0 times is 100
D. myCount.count is 100 times is 0

10.26  What will be displayed by the following code?

  #include <iostream>
  using namespace std;

  class Count
  {
  public:
    int count;

    Count(int c)
    {
      count = c;
    }

    Count()
    {
      count = 0;
    }
  };

  void increment(Count c, int &n)
  {
    c.count++;
    n++;
  }

  int main()
  {
    Count myCount;
    int times = 0;

    for (int i = 0; i < 100; i++)
      increment(myCount, times);

    cout << "myCount.count is " << myCount.count;
    cout << " times is " << times;

    return 0;
  }
A. myCount.count is 0 times is 0
B. myCount.count is 100 times is 100
C. myCount.count is 0 times is 100
D. myCount.count is 100 times is 0

10.27  Analyze the following code:

 #include <iostream>
 using namespace std;

 class A
 {
 public:
   A();
   double getNumber();

 private:
   double number;
 };

 A::A()
 {
   number = 1;
 }

 double A::getNumber()
 {
   return number;
 }

 void printA(const A &a)
 {
   cout << "The numberr is " << a.getNumber() << endl;
 }

 int main ()
 {
   A myObject;
   printA(myObject);

   return 0;
 }
A. The program will compile and run fine.
B. The program will compile if the getNumber function is defined as const.
C. The program will compile if const A &a is changed to A &a in the function printA.
D. The program will compile if A myObject is changed to A myObject().
E. The program will compile if printA(myObject) is changed to printA(&myObject).

10.28  Analyze the following code:

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

  class Name
  {
  public:
    string firstName;
    char mi;
    string lastName;
  
    Name(string firstName1, char mi1, string lastName1)
    {
      firstName = firstName1;
      mi = mi1;
      lastName = lastName1;
    }
  };

  int main()
  {
    string firstName("John");
    Name name(firstName, 'F', "Smith");
    firstName = "Peter";
    name.lastName = "Pan";
    cout << name.firstName << " " << name.lastName << endl;
  }
A. The program displays Peter Pan.
B. The program displays John Pan.
C. The program displays Peter Smith.
D. The program displays John Smith.

10.29  Analyze the following code:

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

  class MyDate
  {
  public:
    int year;
    int month;
    int day;
  
    MyDate()
    {
      year = 2003;
      month = 2;
      day = 3;
    }

    MyDate(int year1, int month1, int day1)
    {
      year = year1;
      month = month1;
      day = day1;
    }
  };

  class Name
  {
  public:
    string firstName;
    char mi;
    string lastName;
    MyDate birthDate;
  
    Name(string firstName1, char mi1, string lastName1, MyDate birthDate1)
    {
      firstName = firstName1;
      mi = mi1;
      lastName = lastName1;
      birthDate = birthDate1;
    }
  };

  int main()
  {
    MyDate birthDate(1990, 3, 4);
    Name name("John", 'F', "Smith", birthDate);
    birthDate = MyDate(1991, 1, 2);
    birthDate.year = 1992;
    cout << name.birthDate.year << endl;
  }
A. The program displays 1990.
B. The program displays 1991.
C. The program displays 1992.
D. The program displays no thing.

10.30  Analyze the following code:

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

  class MyDate
  {
  public:
    int year;
    int month;
    int day;
  
    MyDate()
    {
      year = 2003;
      month = 2;
      day = 3;
    }

    MyDate(int year1, int month1, int day1)
    {
      year = year1;
      month = month1;
      day = day1;
    }
  };

  class Name
  {
  public:
    string firstName;
    char mi;
    string lastName;
    MyDate birthDate;
  
    Name(string firstName1, char mi1, string lastName1, MyDate& birthDate1)
    {
      firstName = firstName1;
      mi = mi1;
      lastName = lastName1;
      birthDate = birthDate1;
    }
  };

  int main()
  {
    MyDate birthDate(1990, 3, 4);
    Name name("John", 'F', "Smith", birthDate);
    birthDate.year = 1991;
    birthDate.year = 1992;
    cout << name.birthDate.year << endl;
  }
A. The program displays 1990.
B. The program displays 1991.
C. The program displays 1992.
D. The program displays no thing.

Section 10.4 Array of Objects
10.31  Given the declaration Circle x[10], which of the following statements is correct?
A. x contains an array of ten int values.
B. x contains an array of ten objects of the Circle type.
C. Each element in the array is a Circle object.
D. You cannot assign a new object to the elements in the array, but you can change the contents in each object element.

Section 10.5 Instance and Static Members
10.32  Variables that are shared by every instances of a class are __________.
A. public variables
B. private variables
C. instance variables
D. static variables

10.33  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 because the function does not use any instance data fields.
B. in the getAge function
C. in both lthe square function and the getAge function
D. none

10.34  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 compile 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).

10.35  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.

10.36  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.6 Constant Member Functions
10.37  Which of the following statements are true?
A. A constant member function cannot change the data fields in the object.
B. A constant memebr function may be an intance or a static function.
C. A constant function may be defined for any functions not just instance member functions.
D. A constructor can be defined as a constant member funciton.

Section 10.8 Object Composition
10.38  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.