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 9 Objects and Classes


Section 9.2 Defining Classes for Objects
9.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

9.2  _______ is a construct that defines objects of the same type.
A. A class
B. An object
C. A function
D. A data field

9.3  An object is an instance of a __________.
A. program
B. class
C. function
D. data

9.4  The keyword __________ is required to define a class.
A. public
B. private
C. class
D. friend
E. static

Section 9.4 Constructors
9.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

9.6  Which of the following statements are true?
A. A default no-arg constructor is provided automatically if no constructors are explicitly defined 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.

9.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).

9.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.5 Constructing and Using Objects
9.9  Given the declaration Circle x, which of the following statement is most accurate?
A. x contains an int value.
B. x is an object of the Circle type.
C. You can assign an int value to x.
D. x is a reference to a Circle object.

9.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 compile error because test is not initialized.
B. The program has a compile error because x has not been initialized.
C. The program runs fine, but test.x is unpredictable.
D. The program has a compile error because Test does not have a default constructor.

9.11  There are no default value for data fields in a class.
A. true
B. false

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

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

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

9.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).

9.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.6 Separating Declaration from Implementation
9.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.

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

9.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.7 Preventing Multiple Declarations
9.20  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 9.8 Inline Functions in Classes
9.21  Constructors ___________ and functions _________ are inline defined in the following class A.

class A
{
public:
  A()
  {
    value = 0;
  }

  A(double);

  double f1()
  {
    // Return a number
    return value;
  }
  
  double f2();

private:
  double value;
};
A. A()
B. A(double)
C. f1()
D. f2()

Section 9.9 Data Field Encapsulation
9.22  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.

9.23  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()

9.24  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

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