Introduction to C++ Programming, Y. Daniel Liang

Chapter 9 Objects and Classes


Section 9.2 Defining Classes for Objects
1  __________ represents an entity in the real world that can be distinctly identified.

A. A class
B. An object
C. A function
D. A data field

2  _______ is a construct that defines objects of the same type.

A. A class
B. An object
C. A function
D. A data field

3  An object is an instance of a __________.

A. program
B. class
C. function
D. data

4  The keyword __________ is required to declare a class.

A. public
B. private
C. class
D. All of the above.

Section 9.3 Constructors
5  ________ is invoked to create an object.

A. A constructor
B. The main function
C. A function with a return type
D. A function with the void return type

6  Which of the following statements are true?

A. A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class.
B. At least one constructor must always be defined explicitly.
C. Constructors do not have a return type, not even void.
D. Constructors must have the same name as the class itself.
E. Constructors are invoked when an object is created.

7  Analyze the following code:

   #include <iostream>
   using namespace std;
  
   class A
   {
   public:
     int s;
  
     A(int newS)
     {
       s = newS;
     }
  
     void print()
     {
       cout << s;
     }
   };
  
   int main()
   {
     A a;
     a.print();
   }


A. The program has a compilation error because class A is not a public class.
B. The program has a compilation error because class A does not have a default constructor.
C. The program compiles and runs fine and prints nothing.
D. The program would compile and run if you change A a to A a(5).

8  What is wrong in the following code?

   #include <iostream>
   using namespace std;
  
   class TempClass
   {
   public:
     int i;
  
     TempClass()
     {
       int i = 5;
     }
   };
  
   int main()
   {
     TempClass temp(2);
   }

A. The program has a compilation error because TempClass does not have a default constructor.
B. The program has a compilation error because TempClass does not have a constructor with an int argument.
C. The program compiles fine, but it does not run because class C is not public.
D. The program compiles and runs fine.

Section 9.4 Object Names
9  Given the declaration Circle x, which of the following statement is most accurate.

A. x contains an int value.
B. x refers to an object of the Circle type.
C. *x refers to an object of the Circle type.
D. You can assign an int value to x.

10  Analyze the following code.

   #include <iostream>
   using namespace std;
  
   class Test
   {
   public:
     int x;
  
     Test()
     {
       cout << "Test";
     }
   };
  
   int main()
   {
     Test test;
     cout << test.x;
   }


A. The program has a syntax error because test is not initialized.
B. The program has a syntax error because x has not been initialized.
C. The program runs fine, but test.x is unpredictable.
D. The program has a syntax error because Test does not have a default constructor.

11  There are no default value for data fields in a class.

A. true
B. false

12  Which of the following statements are true?

A. local variables do not have default values.
B. data fields have no default values.
C. A variable of a primitive type holds a value of the primitive type.
D. An object name is like a constant, which cannot be reassigned with a new object.

13  Suppose circle1 and circle2 are two Circle objects. What does the following statement do?

   circle2 = circle1;


A. It copies the contents of circle1 to circle2.
B. It makes circle2 and circle1 the same object.
C. It copies the contents of circle2 to circle1.
D. This statement is illegal.

14  Which of the following statements are true?

A. Object names are like array names. Once an object name is declared, it references to an object.
B. Object names cannot be reassigned to reference another object.
C. An object name is a constant, though the contents of the object may change.
D. An object is associated with only one object name.

15  Which of the following statements are true?

A. The statement Circle circle = Circle() creates a Circle object using the no-arg constructor and copies its contents to circle.
B. The statement Circle circle = Circle(5) creates a Circle object with radius 5 and copies its contents to circle.
C. Circle circle = Circle() should be replaced by Circle circle.
D. Circle circle = Circle(5) should be replaced by Circle circle(5).

16  Analyze the following code.

  #include <iostream>
  using namespace std;

  class B
  {
  public:
    B() { };
    int k;
  };

  int main()
  {
    B b;
    cout << b.k << endl;

    return 0;
  }

A. The program has a compile error because b.k cannot be accessed.
B. The program displays 0.
C. The program displays 1.
D. The program displays unpredictable number.
E. The program has a runtime error because b.k does not have a value.

Section 9.5 Separating Declaration from Implementation
17  Which of the following statements are true?

A. C++ allows you to separate class declaration from implementation.
B. The class declaration describes the contract of the class and the class implementation implements the contract.
C. The class declaration simply lists all the data fields, constructor prototypes, and the function prototypes. The class implementation implements the constructors and functions.
D. The class declaration and implementation are in two separate files. Both files should have the same name, but with different extension names.
E. The class declaration file has an extension name .h and the class implementation file has an extension name .cpp.

18  Which of the following statements are true?

A. The :: symbol is called the scope operator.
B. The binary scope operator can be used as ClassName::member to tell the compiler that a member belongs to a class.
C. The unary scope operator can be used as ::var to tell the compiler that the variable is a global variable.

19  Show the output of the following code:

   #include <iostream>
   using namespace std;

   class A
   {
   public:
     int x;
     int y;
     int z;

     A(): x(1), y(2), z(3)
     {
     }
   };

   int main()
   {
     A a;
     cout << a.x << " " << a.y << " " << a.z;

     return 0;
   }


A. 1 1 1
B. 1 1 2
C. 1 2 3
D. 2 2 2
E. 3 3 3

Section 9.6 Accessing Object Members via Pointers
20  Show the output of the following code:

   #include <iostream>
   using namespace std;

   class A
   {
   public:
     int x;
     int y;
     int z;

     A(): x(1), y(2), z(3)
     {
     }
   };

   int main()
   {
     A a;
     A *p1 = &a;
     a.x = 2;

     A a1;
     p1 = &a1;
     cout << p1->x << " " << (p1*).y << " " << p1->z;

     return 0;
   }


A. 1 1 1
B. 1 1 2
C. 1 2 3
D. 2 2 2
E. 3 3 3

Section 9.7 Creating Dynamic Objects on Heap
21  Which of the following statements are correct?

29. Which of the following statements are correct to delete a dynamic object from a pointer p?

A. delete *p;
B. delete p;
C. delete [] *p;
D. delete [] p;

Section 9.8 The C++ string Class
22  What is the output of the following code?

     string s("abc");
     s.append("welcome");
     cout << s;


A. abcwelcome
B. abc
C. welcome
D. welcomeabc

23  What is the output of the following code?

     string s("abc");
     s.append("welcome", 0, 3);
     cout << s;


A. abcwelcome
B. abc
C. abcwel
D. welcomeabc

24  What is the output of the following code?

     string s("abc");
     s.append("welcome", 3);
     cout << s;


A. abcwelcome
B. abc
C. abcwel
D. welcomeabc

25  What is the output of the following code?

     string s("abc");
     s.append(3, 'w');
     cout << s;


A. abcwelcome
B. abc
C. abcwel
D. abcwww

26  What is the output of the following code?

     string s("abc");
     s.assign("welcome");
     cout << s;


A. abcwelcome
B. abc
C. welcome
D. abcwww

27  What is the output of the following code?

     string s("abc");
     s.assign("welcome", 0, 3);
     cout << s;


A. abcwelcome
B. abc
C. welcome
D. abcwww
E. wel

28  What is the output of the following code?

     string s("abc");
     s.assign("welcome", 3);
     cout << s;


A. abcwelcome
B. abc
C. welcome
D. abcwww
E. wel

29  What is the output of the following code?

     string s("abc");
     s.assign(3, 'w');
     cout << s;


A. abcwww
B. abc
C. www
D. abcwww
E. wel

30  What is the output of the following code?

     string s("abcd");
     cout << s.at(1);


A. a
B. b
C. c
D. d

31  What is the output of the following code?

     string s("abcd");
     cout << s.length();


A. 1
B. 2
C. 3
D. 4
E. 5

32  What is the output of the following code?

     string s("abcd");
     cout << s.size();


A. 1
B. 2
C. 3
D. 4
E. 5

33  What is the output of the following code?

     string s("abcd");
     s.clear();
     cout << s.length();


A. 1
B. 2
C. 3
D. 4
E. 0

34  What is the output of the following code?

string s("abcdefg");
cout << s.compare("abb");


A. 1
B. 0
C. -1
D. -2

35  What is the output of the following code?

     string s("abcdefg");
     s.erase(2, 3);
     cout << s;


A. abcd
B. abfg
C. abcg
D. aefg

36  What is the output of the following code?

string s("abcdefg");
char s1[15] = "welcome";
s.copy(s1, 3, 0);
cout << s1;


A. welcome
B. abccome
C. welcomeabc
D. abcwelcome

37  What is the output of the following code?

string s("abcdefg");
cout << s.substr(1, 3);


A. abc
B. bcd
C. a
D. c

38  What is the output of the following code?

string s("abcdefg");
cout << s.substr(3);


A. abc
B. bcd
C. defg
D. efg

39  What is the output of the following code?

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


A. abcdefg
B. welcome

40  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

41  What is the output of the following code?

string s("abcdefg");
s.replace(1, 2, "wel");
cout << s;


A. abcdefg
B. aweldefg
C. welabcdefg
D. awelbcdefg

42  What is the output of the following code?

string s("abcdefg");
s.insert(1, "wel");
cout << s;


A. abcdefg
B. aweldefg
C. welabcdefg
D. awelbcdefg

43  What is the output of the following code?

string s("abcdefg");
s.insert(1, 3, 'w');
cout << s;


A. abcdefg
B. aweldefg
C. awwwbcdefg
D. awelbcdefg

Section 9.9 Data Field Encapsulation
44  Which of the following statements are true?

A. Use the private keyword to encapsulate data fields.
B. Encapsulating data fields makes the program easy to maintain.
C. Encapsulating data fields makes the program short.
D. Encapsulating data fields helps prevent programming errors.
E. If you don't use the public keyword, the visibility is private by default.

45  Suppose you wish to provide an accessor function for a boolean property finished, what signature of the function should be?

A. void getFinished()
B. bool getFinished()
C. bool isFinished()
D. void isFinished()

46  What is the output of the following code?

   #include <iostream>
   using namespace std;
  
   class Foo
   {
   public:
     int x; // data field
     int y; // data field
  
     Foo()
     {
       x = 10;
       y = 10;
     }
  
     void p()
     {
       int x = 20; // local variable
       cout << "x is " << x << " ";
       cout << "y is " << y << endl;
     }
   };
  
   int main()
   {
     Foo foo;
     foo.p();
  
     return 0;
   }

A. x is 10 y is 10
B. x is 20 y is 20
C. x is 20 y is 10
D. x is 10 y is 20

47  Analyze the following code.

   #include <iostream>
   using namespace std;

   class B
   {
   public:
     B() { };

   private:
     int k;
   };

   int main()
   {
     B b;
     cout << b.k << endl;

     return 0;
   }

A. The program displays 0.
B. The program displays 1.
C. The program displays unpredictable number.
D. The program has a compile error because b.k cannot be accessed.
E. The program has a runtime error because b.k does not have a value.

Section 9.11 The this Keyword
48  Analyze the following code:

class Circle
{
public:
   Circle(double radius)
   {
     radius = radius;
   }

private:
   double radius;
};

A. The program has a compilation error because it does not have a main function.
B. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.
C. The program will compile, but you cannot create an object of Circle with a specified radius. The object will have an unpredictable value for radius.
D. The program has a compilation error because you cannot assign radius to radius.
E. The program does not compile because Circle does not have a default constructor.

Section 9.12 Passing Objects to Functions
49  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. C++ allows you to pass a parameter of object type in a function by pointer.
D. Passing objects by reference is commonly used because it saves memory.

50  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

51  What is the printout of 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

52  What is the printout of 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
E. myCount.count is 100 times is 100

53  What is the printout of 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
E. myCount.count is 100 times is 100

Section 9.13 Array of Objects
54  Given the declaration Circle x[10], which of the following statement is most accurate.

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 9.16 (Optional) Data Field Initializers
55  Show the output of the following code:

   #include <iostream>
   #include <string>
   using namespace std;
  
   class A
   {
   public:
     A(): i(5), s("abc")
     {
     };
  
     int i; // Declare a data field of the int type
     string s; //Declare a data field of the string type
   };
  
   int main()
   {
     A a;
     cout << "s is " << a.s.data() << endl;
     cout << "i is " << a.i << endl;
  
     return 0;
   }

A. The program displays s is abc followed by i is 5.
B. The program displays s is abc followed by i is 0.
C. The program displays s is followed by i is 5.
D. The program displays s is followed by i is 0.
E. The program displays s is followed by i is.

56  Analyze the following code:

   class A
   {
   public:
     A();
  
   private:
     int i = 5;
     string s("abc");
   };

A. The program has a syntax error because int data field i cannot be initialized when it is declared.
B. The program has a syntax error because object data field s cannot be created in its declaration.
C. The program has no syntax error.

57  Analyze the following code:

  #include <iostream>
  #include "Circle.h"
  using namespace std;

  int main()
  {
    cout << Circle(5).getArea() << endl;
    cout << (new Circle(5))->getArea() << endl;

    return 0;
  }

A. The program has a syntax error on Circle(5).getArea().
B. The program has a syntax error on new Circle(5).getArea().
C. The program compiles, but cannot run.
D. The program compiles and runs, but new Circle(5) creates an anonymous object on the heap. This causes memory leak.