Chapter 14

RECORDS (C++ STRUCTS)

QUESTIONS

True/False

1. Arrays and structs are both structured data types.

2. A struct is a homogeneous structured data type.

3. The values in an array are accessed by an index, whereas the values in a struct are accessed by a member name.

4. The members of a struct can be of any type except float.

5. It is not allowed for different struct types to have identical member names.

6. To select a member of a struct, you specify the member name, then a dot, and then the struct variable name.

7. C++ allows an array to be a member of a struct.

8. C++ allows a struct to be a member of another struct.

9. C++ allows the elements of an array to be structs.

10. One struct can be assigned to another in an assignment statement as long as the corresponding members of both structs have the same name and type.

11. C++ does not allow aggregate operations on structs.

12. C++ operators that may be applied to struct variables are assignment (=), equality testing (==), and member selection (.).

13. In C++, a struct can be passed as a parameter either by value or by reference.

14. In C++, it is possible to have arrays of structs of arrays.

15. The declaration

struct

{

char first[21];

char last[31];

} name;

creates an anonymous struct type.

16. A hierarchical record is a record that contains an array as one of its members.

Multiple Choice

17. A struct is an example of:

a. a simple data type

b. a homogeneous structured type

c. a heterogeneous structured type

d. an enumeration type

18. Which line of the following program fragment contains a syntax error?

struct StatType // Line 1

{ // Line 2

float height; // Line 3

int weight; // Line 4

} // Line 5

StatType stats[100]; // Line 6

a. line 1

b. line 3

c. line 4

d. line 5

e. line 6

19. Given the declaration

struct PersonRec

{

int age;

float height;

int weight;

};

which of the following is valid for creating and initializing a PersonRec variable?

a. PersonRec me;

me.age = 19;

me.height = 66.5;

me.weight = 140;

b. PersonRec me = {19, 66.5, 140};

c. PersonRec.age = 19;

PersonRec.height = 66.5;

PersonRec.weight = 140;

d. a and b above

e. a, b, and c above

20. In C++, which of the following is not allowed as an aggregate operation on structs?

a. assignment

b. I/O

c. parameter passage by value

d. parameter passage by reference

e. return as a function's return value

21. Given the declarations

struct RecType1

{

int length;

float width;

};

struct RecType2

{

int length;

float width;

};

RecType1 myRec;

RecType2 yourRec;

which of the following assignment statements is valid?

a. myRec.length = yourRec.length;

b. myRec = yourRec;

c. myRec.length = yourRec;

d. a and b above

e. none of the above

22. Given the declarations

struct RecType1

{

int length;

float width;

};

RecType1 myRec;

RecType1 yourRec;

which of the following assignment statements is valid?

a. myRec.length = yourRec.length;

b. myRec = yourRec;

c. myRec.length = yourRec;

d. a and b above

e. none of the above

23. Given the declarations

typedef char String19[20];

struct BrandInfo

{

String19 company;

String19 model;

};

struct DiskType

{

BrandInfo brand;

float capacity;

};

DiskType myDisk;

which of the following assignment statements is valid?

a. myDisk.capacity = 1.44;

b. myDisk.BrandInfo = "Memorex";

c. strcpy(myDisk.BrandInfo, "Memorex");

d. a and c above

e. none of the above

24. Given the declarations

typedef char String19[20];

struct BrandInfo

{

String19 company;

String19 model;

};

struct DiskType

{

BrandInfo brand;

float capacity;

};

DiskType myDisk;

which of the following assignment statements is valid?

a. myDisk.brand[3].company = 'X';

b. myDisk.brand.company[3] = 'X';

c. myDisk[3].brand.company = 'X';

d. a and c above

e. none of the above

25. Given the declarations

typedef char String19[20];

struct BrandInfo

{

String19 company;

String19 model;

};

struct DiskType

{

BrandInfo brand;

float capacity;

};

DiskType myDisk;

what is the type of myDisk.brand?

a. char

b. DiskType

c. String19

d. BrandInfo

e. none of the above

 

26. Given the declarations

typedef char String19[20];

struct BrandInfo

{

String19 company;

String19 model;

};

struct DiskType

{

BrandInfo brand;

float capacity;

};

DiskType myDisk;

what is the type of myDisk.brand.company[2]?

a. char

b. float

c. String19

d. BrandInfo

e. none of the above

27. Consider the following declarations:

typedef char String19[20];

struct BrandInfo

{

String19 company;

String19 model;

};

struct DiskType

{

BrandInfo brand;

float capacity;

};

DiskType myDisk;

If a char value occupies one byte of memory and a float value occupies four bytes, how many bytes of memory does myDisk occupy?

a. 1

b. 5

c. 8

d. 24

e. 44

28. Given the declarations

typedef char String19[20];

struct SupplierType

{

int idNumber;

String19 name;

};

struct PartType

{

String19 partName;

SupplierType supplier;

};

PartType partsList[10000];

which of the following expressions is valid?

a. partsList[72]

b. partsList[72].supplier

c. partsList.supplier[72]

d. a and b above

e. a and c above

29. Given the declarations

typedef char String19[20];

struct SupplierType

{

int idNumber;

String19 name;

};

struct PartType

{

String19 partName;

SupplierType supplier;

};

PartType partsList[10000];

which of the following expressions is valid?

a. partsList[72].supplier[4].idNumber

b. partsList[72].supplier.name[4]

c. partsList[72].partName[4]

d. a and b above

e. b and c above

30. Given the declarations

typedef char String19[20];

struct SupplierType

{

int idNumber;

String19 name;

};

struct PartType

{

String19 partName;

SupplierType supplier;

};

PartType partsList[10000];

what is the type of partsList[1].supplier.name[1]?

a. String19

b. PartType

c. char

d. int

e. SupplierType

31. Consider the following declarations:

typedef char String19[20];

struct SupplierType

{

int idNumber;

String19 name;

};

struct PartType

{

String19 partName;

SupplierType supplier;

};

PartType partsList[1000];

If a char value occupies one byte of memory and an int value occupies two bytes, how many bytes of memory does partsList occupy?

a. 1042

b. 1402

c. 4042

d. 42,000

e. 42,042

 

Fill-In

32. A(n) ____________________ is a structured data type whose components, which may be of different data types, are accessed by name rather than by index.

33. The expression used to access a component of a struct variable is called a(n) ____________________.

34. A record in which at least one of the components is itself a record is called a(n) ____________________ record.

35. The C++ terminology for "field of a record" is " ____________________ of a struct."

 

 

36. A record (struct) is said to be a(n) ____________________ data type because its components can be of different data types.

37. A(n) ____________________ is a C++ record type that physically holds only one of its members at a time during execution.

38. The separation of a data type's logical properties from its implementation is called ____________________.

39. Given the declarations

struct ItemType

{

char description[51];

float price;

};

ItemType inventory[8000];

write an expression that denotes the tenth item in the store's inventory: ____________________

40. Given the declarations

struct ItemType

{

char description[51];

float price;

};

ItemType inventory[8000];

write an expression that denotes the price of the first item in the store's inventory: ____________________

41. Given the declarations

struct ItemType

{

char description[51];

float price;

};

ItemType inventory[8000];

write an expression that denotes the description of the first item in the store's inventory: ____________________

42. Given the declarations

struct ItemType

{

char description[51];

float price;

};

ItemType inventory[8000];

write an expression that denotes the first letter of the description of the first item in the store's inventory: ____________________

43. Given the declarations

typedef char String30[31];

struct BookType

{

String30 title;

String30 author;

String30 publisher;

};

BookType aNovel;

write a statement to print the author of aNovel: ____________________

44. Given the declarations

typedef char String20[21];

enum Light {FULL, PARTIAL, SHADE};

struct BloomType

{

String20 name;

Boolean isAnnual;

Light location;

};

BloomType flower[50];

write a statement to print the name of the flower in the fifth component of the array: ____________________

45. The baby array declared below holds the birth records of the babies born at General Hospital in one day.

typedef char String25[26];

struct BirthRecord

{

String25 lastName;

String25 firstName;

float length;

int pounds;

int ounces;

};

BirthRecord baby[30];

Write a statement to print the last name of the first baby born: ____________________

46. The baby array declared below holds the birth records of the babies born at General Hospital in one day.

typedef char String25[26];

struct BirthRecord

{

String25 lastName;

String25 firstName;

float length;

int pounds;

int ounces;

};

BirthRecord baby[30];

Write a statement to print the second letter of the first name of the twelfth baby born: ____________________

47. Given the declarations

typedef char String25[26];

struct DateType

{

int month;

int day;

int year;

};

struct EmployeeType

{

String25 lastName;

String25 firstName;

String25 department;

float salary;

DateType employmentDate;

};

EmployeeType boss;

write a statement to print the year in which the boss was hired: ____________________

48. Given the declarations

enum WoodKind {HARDWOOD, SOFTWOOD};

struct Size

{

int length;

int width;

int thickness;

};

struct Wood

{

Size dimensions;

WoodKind kind;

int smoothSurfaces;

};

Wood oneBoard;

write a statement to print the width of oneBoard: ____________________