Page contents

Absolute C++ 5th Edition by Walter Savitch – Test Bank

Instant delivery only

  • ISBN-10 ‏ : ‎ 0132846810
  • ISBN-13 ‏ : ‎ 978-0132846813

In Stock

$28.00

Add to Wishlist
Add to Wishlist
Compare
SKU:tb1002055

Absolute C++ 5th Edition by Walter Savitch – Test Bank

Chapter 6 – Structures and Classes Test Questions
These test questions are fill in the blank, multiple choice, and true false. The multiple-choice questions may have more than one correct answer. There is one matching question. Mark all of the correct answers for full credit. True/false questions require an explanation in addition to the true/false response. If the response is false, a correction is required.
True False:
Comment required.
1. A C++ structure, or struct, like the C++ array, is a homogeneous data structure. (i.e., all data is of the same type)
Answer: False
Explanation: The struct provides a mechanism to define a collection of values that may be of several different types.
2. Structure definitions are usually global (defined outside any functions).
Answer: True
Explanation: A structure definition is a type that, like const definitions, we usually want to be available everywhere in a program that follows the structure definition.
3. A structure member is access using the index operator [ ], with the member name as index. [ ]
Answer: False
Explanation: Access to structure members is made by giving the structure tag, followed by a dot followed by the member name.
Example: Given the structure definition and variable definition such as
struct CDAccount
{
double balance,
double interestRate;
int term;
};
CDAccount account1;
Access to member variables of account1 may be had using the dot operator:
account1.balance1
account1.interestRate
4. A structure variable can be defined directly in the same statement that defines a structure definition.
Answer: True
Explanation: The variables s and t are structure variables of structure type myStruct under the following definitions:
struct myStruct
{
int i;
double d;
} s, t;
5. The following definition of the structure variables of type myStruct s and t could be made using several definitions using the structure tag.
struct myStruct
{
int i;
double d;
} s, t;
Answer: True
Explanation: This possibility is why the semicolon is necessary at the end of a structure definition. Here is how the definitions may be rewritten.
struct myStruct
{
int i;
double d;
};
myStruct s;
myStruct t;
6. A structure can have a member whose type is another structure.
Answer: True
Explanation: Structures collect, name and give a type to related data. A subcollection of data from a structure may be so closely related that we want to make a struct of the subcollection. Example: A struct containing personal data may contain a birth date of struct type, in turn composed of day, month and year.
7. Consider these hierarchical structures.
struct Date
{
int year;
//members
};
struct Person
{
Date birthDay;
//other members
};
Person Bill;
The year of Bill’s birthday may be accessed as Bill.year;
Answer: False.
Explanation: To access the year of Bill’s birthday one must specify the Person’s Date structure variable, as Bill.birthDay.year.
8. No special syntax is necessary to define a function that uses a structure parameter. (This is unlike using an array parameter.)
Answer: True
Explanation: Once the structure definition has been made, a structure tag is a type that may be used in any way that a built-in type such as int or double can be used. For example, given the structure definition
struct S {/*…*/};
here is a definition of the void function func that has an S structure parameter.
void func(S y);
This is unlike using an array parameter, which requires square brackets, as well as an additional size parameter, such as
void bar(Y[] x, int size);

9. There is no aggregate initialization available for structure variables. You must declare structure variables then use assignment to initialize the members.
Answer: False
Explanation: A struct can be initialized by using a sequenced of initializers very much like arrays.
struct A { int a; int b; double d;}; A x = { 1, 2, 3.0};
10. A class is a type similar to a structure type that normally has member functions as well as member variables.
Answer: True
Explanation: We have treated the structure as a data members only aggregate, as C does, and we treat the class as having function members and data members.
11. Consider the class and struct definitions below.
struct myStruct
{
int i;
double d;
};
class myClass
{
int i;
double d;
};
myStruct a;
a.i = 3;
myClass b;
b.i = 3;
True or False: All these statements will compile with no problem.
Answer: False
Explanation: The access for class defaults to private. When compiling this code one compiler warns: and another warns in addition that:
cpp 17: ‘myClass::i’ is not accessible in function main()
Another compiler warns in addition that all members of class myClass are private. The code b.i = 3; attempts to access a private member of myClass, and will cause the compiler to issue an error message.

12. The concept of class is central to Object Oriented Programming.
Answer: True
Explanation: The class concept supports OOP. Object oriented programming (OOP) is a powerful technique that divides problems by who is acting in a problem (actors are the class type objects) and what they are doing (these are the member functions.)
13. A class type cannot be used in some ways that a built-in type can be used.
Answer: False
Explanation: A class is a type just like an int or a double. You have variables or function parameters of class type. You can return a class object from a function. You can treat a class just like any other type.
14. In defining a member function whose declaration is in a class, you use the dot operator to specify that the member function being defined belongs in the class, as
class foo
{
public:
// other members
void output( );
// other members
};

void foo.output( )
{
/* whatever */
}
Answer: False.
Explanation: This is a mistake that students who have studied Java may make. Nevertheless, it occurs among all groups of students. C++ uses the scope resolution operator when we are specifying what class a member is from, and a dot operator to specify the calling object. The correct syntax for this function definition follows. Note carefully the use of the scope resolution operator, ::to specify that output is being defined in the scope of class foo.
void foo::output( )
{
/* whatever */
}

15. If, in a class, one uses the keyword public:, it affects only the member that immediately follows the keyword public, making this member accessible to any other function defined anywhere.
Answer: False
Explanation: The effect of the public: keyword is from the keyword to the next private: keyword, so there is a region where anything that is there defined is public, not just a single member. The remark about access is correct.
16. There is no access to private members of a class by any function defined outside the class.
Answer: True.
Explanation: In a class, the all members defined after a keyword private: and before the next public: keyword are called private members. Access to private members by functions defined outside the class is prohibited. (Note: This ignores the issue of friend functions, which will be treated in a later chapter.)
17. A data type is a collection of a set of values together with a set of basic operations defined on the values.
Answer: True
Explanation: Examples of a data type are int with +_*/% and other operations, double with +_*/.
18. An abstract data type is a collection of a set of values together with a set of basic operations defined on the values.
Answer: false
Explanation: To qualify as an abstract data type, a data type must conceal the details of how the operations and values are implemented. The operations should be sufficiently documented that one can use them without knowing the implementation.
19. Multiple public: and private: sections are prohibited in a class.
Answer: False
Explanation: The keywords public: and private: have effect that runs up to the next instance of one of these keywords. There is no prohibition against having a sequence of private section after a public section and so on.
20. A sure test of whether you have succeeded in producing an Abstract Data Type (whether you have truly separated the interface from the implementation) is whether you can use the ADT then change either the implementation or the client code without being required to change the other.
Answer: True
Explanation: This is the definition of ADT.
21. The scope resolution operator can be used with an object to qualify a member function.
Answer: False
Explanation: The scope resolution operator is used with a class name to qualify a member to tell the compiler that this member comes from that class.
22. The dot operator is used between an object and a data member or between a calling object and a call to a member function from the class of the object.
Answer: True
Explanation: If we have object.func(arg_list) we speak of the object as the calling object for the function call func(arg_list). The dot is the operator that signals to the compiler that we intend to call that function, with data members available from the object. If the object.data_member is specified, the dot says to the compiler that we want to access the data from data_member that belongs to that particular object.

Multiple Choice
1. C++ allows the programmer to deal with data at a higher level of abstraction in part because related data of differing types can be treated as a unit in
a) a array variable
b) a structure variable
c) a function
d) a library
e) a class variable
Answer:
b) structure variable. and e) a class variable
Explanation: Arrays are homogeneous, functions do not encapsulate data, and we know that libraries are collections of functions organized for easy access. The ones that are left, class and struct types, allow heterogeneous data as a unit is a structure or class variable.

2. A member of a structure or of a class is accessed using the
a) Comma operator
b) The dot operator
c) The indexing operator
d) The ampersand operator
Answer:
b) the dot operator
Explanation: The member of a structures or classes are accessed by writing the tag followed by the dot operator which in turn is followed by the name of the member or a call to a member function.

3. What is the error in the following structure definition?
struct A
{
int b;
int c;
}
int main()
{
A x;
// other code
}
Answer:
The terminating semicolon is missing from the definition of struct A. Error messages for this may be clear “Semicolon missing from structure or class definition.” However, one compiler says cryptically, “Too many types in declaration.”
4. Here are several different initializations of a structure variable. State what happens in each initialization.
struct WeatherData
{
int temperature;
int windChill;
int windSpeed;
};
a) WeatherData prediction ={ };
b) WeatherData prediction ={40};
c) WeatherData prediction ={40, -10, };
d) x WeatherData prediction ={40, -10, 20 };
Answer:
a) All the structure members are set to 0.
b) temperature is set to 40, the other two variables are set to zero.
c) temperature is set to 40, windChill is set to –10, and windSpeed is set to 0
d) temperature is set to 40, windChill is set to –10, and windSpeed is set to 20
Explanation: If there is an initializer list and a member is missing an initializer, that member is set to a zero appropriate to the type. a) The issue here is whether the empty initializer list is allowed. They are permitted for structs (but not for arrays), so all the structure members are set to 0. For b) and c), the members missing initializers are set to 0. In d), all members are set to the corresponding member of the initializer list.

Reviews

There are no reviews yet.

Write a review

Your email address will not be published. Required fields are marked *

Product has been added to your cart