You are on page 1of 11

Problem Solving with C++ 10th Edition

Savitch Test Bank


Visit to download the full and correct content document: https://testbankdeal.com/dow
nload/problem-solving-with-c-10th-edition-savitch-test-bank/
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

TRUE/FALSE
1. A struct variable is declared differently from a predefined type such as an int.
ANSWER: FALSE
2. Two different structure definitions may have the same member names.
ANSWER: TRUE.
3. A structure can only be passed to a function as a call-by-value parameter
ANSWER: FALSE
4. A function may return a structure.
ANSWER: TRUE
5. Different class may not have member functions with the same name.
ANSWER: FALSE
6. A class member function may be private.
ANSWER: TRUE
7. Class data members are almost always public.
ANSWER: FALSE
8. It is possible to have multiple private labels in a class definition.
ANSWER: TRUE
9. The assignment operator may not be used with objects of a class.
ANSWER: FALSE
10. All constructors for a class must be private.
ANSWER: FALSE
11. A derived class is more specific than its parent, or base class.
ANSWER: TRUE

Short Answer
1. The keyword ________ defines a structure type definition.
ANSWER: struct
2. A structure definition ends with the closing brace and a _________.
ANSWER: semicolon
3. A structure variable is a collection of smaller values called ____________ values
ANSWER: member
4. When a structure contains another structure variable as one of its members, it is
known as a ___________________.
ANSWER: hierarchical structure
5. When several items (variables or variables and functions) are grouped together
into a single package, that is known as ______________.
ANSWER: (data) encapsulation
6. The double colon (::) is known as the __________ operator.
ANSWER: scope resolution operator
7. Who can access private members in a class?
ANSWER: only other members of the class
8. A member function that allows the user of the class to find out the value of a
private data type is called a ___________________.
ANSWER: accessor function.
9. A member function that allows the user of the class to change the value of a
private data type is called a ____________________.
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

ANSWER: mutator function.


10. If you have a class with a member function called display(ostream& out), that will
send the values in the class to the parameter stream, and you need to call that
function from within another member function, how would you call it to print the
data to the screen? ___________________________
ANSWER: display(cout);
11. What can a constructor return? _______________
ANSWER: nothing
12. The name of a constructor is _____________
ANSWER: the name of the class
13. The constructor of a class that does not have any parameters is called a
__________ constructor.
ANSWER: default
14. In the following class constructor definition, the part of the header starting with a
single colon is called the ________________.

BankAccount::BankAccount(): balance(0), interest(0.0)

ANSWER: initialization section


15. A class in which modifications to the implementation appear to be invisible to the
user of the class is known as _________________.
ANSWER: an Abstract Data Type (ADT)
16. A member function that gets called automatically when an object of the class is
declared is called a _______________.
ANSWER: constructor
17. If class A is derived from class B, then B is a _______ of A.
ANSWER: parent
18. C++11 allows you to directly set the member variables to initial values in the
definition of the class. This feature is called __________________.
ANSWER: member initializers
19. C++11 allows you to have a constructor call another constructor. This feature is
called _________________________.
ANSWER: constructor delegation

Multiple Choice
1. In a structure definition, the identifiers declared in the braces are called
a. classes
b. structs
c. member names
d. variables
ANSWER: C
2. You specify an individual member of a struct by using
a. the assignment operator
b. an ampersand
c. an underscore
d. The dot operator
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

ANSWER: D
3. To assign values to a structure variable, you use the
a. equals operator
b. assignment operator
c. extraction operator
d. less than operator
ANSWER: B
4. What is wrong with the following structure definition?
struct MyStruct
{
int size;
float weight;
}
a. Nothing
b. Can not have mixed data types in a structure
c. missing semicolon
d. Braces are not needed.
ANSWER: C
5. Given the following strucure definitions, what is the correct way to print the
person's birth year?
struct DateType
{
int day;
int month;
int year;
}

struct PersonType
{
int age;
float weight;
DateType birthday;
}

PersonType person;
a. cout << person.birthday.year;
b. cout << year;
c. cout << birthday.year;
d. cout << peson.year;
ANSWER: A
6. Given the following strucure definition, what is the correct way to initialize a
variable called today?
struct DateType
{
int day;
int month;
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

int year;
}

a. DateType today(1,1,2000);
b. DateType today = (1,1,2000);
c. DateType today = {1,1,2000);
d. DateType today = {1,1,2000,0);
ANSWER: C
7. When defining a class, the class should be composed of the kind of values a
variable of the class can contain, and
a. member functions for that class
b. the keyword private
c. other class definitions
d. nothing else
ANSWER: A
8. Which of the following is the correct function definition header for the getAge
function which is a member of the Person class?
a. int getAge();
b. int getAge()
c. int Person:getAge()
d. int Person::getAge()
ANSWER: D
9. Given the following class definition and the following member function header,
which is the correct way to output the private data?
class Person
{
public:
void outputPerson(ostream& out);
private:
int age;
float weight;
int id;
};

void Person::outputPerson(ostream& out)


{
//what goes here?
}
a. out << person.age << person.weight << person.id;
b. out << person;
c. out << age << weight << id;
d. outputPerson(person);
ANSWER: C
10. Why do you want to usually make data members private in a class?
a. so that no one can use the class.
b. ensure data integrity
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

c. provide data abstraction.


d. provide information hiding.
e. B and D
f. B, C and D
ANSWER: F
11. A member function of a class should be made private
a. always
b. only if it will never be used
c. if it will only be used by other members of the class
d. never, it is illegal to make a member function private.
ANSWER: C
12. A member function that allow the user of the class to change the value in a data
member is known as
a. a mutator function
b. a mutation
c. a manipulator function
d. an accessor function
ANSWER: A
13. A Member function that allows the user of the class to see the value in a data
member is known as
a. a mutator function
b. a mutation
c. a manipulator function
d. an accessor function
ANSWER: D
14. If you design a class with private data members, and do not provide mutators and
accessors, then
a. The private data members can still be accessed from outside the class by
using the & operator
b. The data can not be changed or viewed by anyone.
c. None of the above
d. A and B
ANSWER: B
15. A class member function that automatically initializes the data members of a class
is called
a. the init function
b. an operator
c. a constructor
d. a cast
ANSWER: C
16. If you have a class named myPersonClass, which of the following correctly
declare a constructor in the class definition?
a. myPersonClass::myPersonClass();
b. myPersonClass();
c. init();
d. cast();
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

ANSWER: B
17. given the following class definition, how could you use the constructor to assign
values to an object of this class?

class CDAccount
{
public:
CDAccount();
CDAccount(float interest, float newBalance);
float getBalance();
float getRate();
void setRate(float interest);
void setBalance(float newBalance);
private:
float balance, rate;
};

and the following object declaration


CDAccount myAccount;
a. myAccount = CDAccount(float myRate, float myBalance);
b. myAccount = CDAccount {myRate, myBalance};
c. myAccount = CDAccount[myRate, myBalance];
d. myAccount = CDAccount(myRate, myBalance);
ANSWER: D
18. Given the following class definition, what is missing?
class ItemClass
{
public:
ItemClass(int newSize, float newCost);
int getSize();
float getCost();
void setSize(int newSize);
void setCost(float newCost);
private:
int size;
float cost;
};
a. nothing
b. a default constructor
c. accessor functions
d. mutator functions
ANSWER: B
19. Given the following class definition, how would you declare an object of the
class, so that the object automatically called the default constructor?
class ItemClass
{
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

public:
ItemClass();
ItemClass(int newSize, float newCost);
int getSize();
float getCost();
void setSize(int newSize);
void setCost(float newCost);
private:
int size;
float cost;
};
a. ItemClass() myItem;
b. ItemClass myItem(1, 0.0);
c. ItemClass myItem;
d. ItemClass myItem();
e. You can not do this
ANSWER: C
20. A data type consisting of data members and operations on those members which
can be used by a programmer without knowing the implementation details of the
data type is called
a. an abstract definition type
b. an available data type
c. an abstract data type
d. a primitive data type
ANSWER: C
21. Which part of the ADT tells the programmer using it how to use it?
a. the implementation
b. the interface
c. the abstractness
d. the scope resolution
ANSWER: B
22. If you are designing a class for an ADT, you can tell if the class is an ADT if
a. when you change the implementation of the class, none of the rest of the
program needs to change.
b. when you change the interface of the class, nothing else needs to change.
c. you change the privte part and the rest of the program using the ADT does
not compile.
d. everything must be changed.
ANSWER: A
23. Developing an ADT means that the user of your class does not have to know the
details about how the class is implemented. This is known as
a. interface
b. implementation
c. testing and debugging
d. information hiding
ANSWER: D
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

24. Given the following class, what would be the best declaration for a mutator
function that allows the user of the class to change the age?

class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
int age;
float cost;
}
a. int getAge(int newAge);
b. Wine();
c. void setAge();
d. void setAge(int newAge);
ANSWER: D
25. Given the following class, what would be the best declaration for a constructor
that would allow the user to initialize the object with an initial age and cost?

class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
int age;
float cost;
}
a. int getAge(int newAge);
b. Wine();
c. Wine(int age);
d. Wine(int newAge, float newCost);
ANSWER: D
26. Given the following class and object declaration, how would you print out the age
and cost of a bottle of wine?

class Wine
{
public:
Wine();
int getAge();
float getCost();
private:
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

int age;
float cost;
}

Wine bottle;
a. cout << bottle;
b. cout << Wine.age, Wine.cost;
c. cout << bottle.getAge() << bottle.getCost();
d. cout << bottle.getAge << bottle.getCost;
e. cout << bottle.age << bottle.cost;
ANSWER: C
27. Data members or member functions of a class that are declared to be private may
a. only be accessed by the main program
b. only be accessed by members of the class
c. not be accessed by the class
d. are considered to be global variables
ANSWER:B
28. Member functions of a class
a. may not be in the private section
b. must be in the private section
c. may be in either section
d. can not be called in the main program
ANSWER: C
29. In a struct, all members are ____________ by default
a. public
b. private
c. global
d. all of the above
ANSWER: A
30. In a class, all members are ____________ by default
a. public
b. private
c. global
d. all of the above
ANSWER: B
31. Which of the following function declarations will correctly pass an output stream
to the function?
a. void display( ofstream& out);
b. void display( ostream out);
c. void display( ostream& out);
d. void display( ofstream out);
e. A and C
f. B and D
ANSWER: E
32. A derived class has access to
a. the private functions and variables of its ancestor classes
Test Bank for Problem Solving with C++: The Object of Programming, 10/e
Chapter 10 Defining Classes

b. the public functions and variables of its ancestor classes


c. only the functions and variables defined it its class
d. none of the above
ANSWER: B
33. Which of the following function declarations will accept either cout or a file
stream object as its argument?
a. void output( fstream &outFile);
b. void output( ofstream &outFile);
c. void output( ostream &outFile);
d. void output( iostream &outFile);
ANSWER: C

You might also like