Chapter 11 Flashcards

1
Q
  1. What is a primitive data type?
A
  1. A data type that is built into the C++ language, such as int, char, float, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Does a structure declaration cause a structure variable to be created?
A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Both arrays and structures are capable of storing multiple values. What is the difference between an array and a structure?
A
  1. The elements of an array must all be of the same data type. The members of a structure may be of different data types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Look at the following structure declaration.
    struct Point
    {
    int x;
    int y;
    };
    Write statements that
    A) define a Point structure variable named center
    B) assign 12 to the x member of center
    C) assign 7 to the y member of center
    D) display the contents of the x and y members of center
A

A) Point center;
B) center.x = 12;
C) center.y = 7;
D) cout &laquo_space;center.x &laquo_space;endl;
cout &laquo_space;center.y &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Look at the following structure declaration.
    struct FullName
    {
    string lastName;
    string middleName;
    string firstName;
    };
    Write statements that
    A) Define a FullName structure variable named info
    B) Assign your last, middle, and first name to the members of the info variable
    C) Display the contents of the members of the info variable
A

A) FullName info;
B) info.lastName = “Smith”;
info.middleName = “Bart”;
info.firstName = “William”;
C) cout &laquo_space;info.lastName &laquo_space;endl;
cout &laquo_space;info.middleName &laquo_space;endl;
cout &laquo_space;info.firstName &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Look at the following code.
    struct PartData
    {
    string partName;
    int idNumber;
    };
    PartData inventory[100];
    Write a statement that displays the contents of the partName member of element 49 of the inventory array.
A
  1. cout &laquo_space;inventory[49].partName &laquo_space;endl;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Look at the following code.
    struct Town
    {
    string townName;
    string countyName;
    double population;
    double elevation;
    };
    Town t = { “Canton”, “Haywood”, 9478 };
    A) What value is stored in t.townName ?
    B) What value is stored in t.countyName ?
    C) What value is stored in t.population ?
    D) What value is stored in t.elevation ?
A

A) “Canton”
B) “Haywood”
C) 9478
D) uninitialized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

structure Rectangle
{
int length;
int width;
};
Rectangle *r = nullptr
Write statements that
A) Dynamically allocate a Rectangle structure variable and use r to point to it.
B) Assign 10 to the structure’s length member and 14 to the structure’s width member.

A

A) r = new Rectangle;
B) r->length = 10;
r->width = 14;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is the difference between a union and a structure?
A
  1. All the members of a union occupy the same area of memory, whereas the members of a structure have their own memory locations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Look at the following code.
    union Values
    {
    int ivalue;
    double dvalue;
    };
    Values v;
    Assuming that an int uses four bytes and a double uses eight bytes, how much memory does the variable v use?
A

8 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What will the following code display?
    enum { POODLE, BOXER, TERRIER };
    cout &laquo_space;POODLE &laquo_space;” “ &laquo_space;BOXER &laquo_space;” “ &laquo_space;TERRIER &laquo_space;endl;
A

0 1 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Look at the following declaration.
    enum Person { BILL, JOHN, CLAIRE, BOB };
    Person p;
    Indicate whether each of the following statements or expressions is valid or invalid.
    A) p = BOB;
    B) p ++ ;
    C) BILL > BOB
    D) p = 0;
    E) int x = BILL;
    F) p = static_cast<Person>(3);
    G) cout << CLAIRE << endl;</Person>
A

A) valid
B) invalid
C) valid
D) invalid
E) valid
F) valid
G) valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Before a structure variable can be created, the structure must be _________.
A

declared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. The _________ is the name of the structure type.
A

tag

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. The variables declared inside a structure declaration are called _________.
A

members

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. A(n) _________ is required after the closing brace of a structure declaration.
A

semicolon

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. In the definition of a structure variable, the _________ is placed before the variable name, just like the data type of a regular variable is placed before its name.
A

tag

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. The _________ operator allows you to access structure members.
A

dot

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
  1. The structure Car is declared as follows:
    struct Car
    {
    string carMake;
    string carModel;
    int yearModel;
    double cost;
    };
    Write a definition statement that defines a Car structure variable initialized with the following data:
    Make: Ford
    Model: Mustang
    Year Model: 1968
    Cost: $20,000
A
  1. Car hotRod = {“Ford”, “Mustang”, 1968, 20000};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
  1. Define an array of 25 of the Car structure variables

struct Car
{
string carMake;
string carModel;
int yearModel;
double cost;
};
Write a definition statement that defines a Car structure variable initialized with the following data:
Make: Ford
Model: Mustang
Year Model: 1968
Cost: $20,000

A

const int SIZE = 25;
Car collection[SIZE];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
  1. Define an array of 35 of the Car structure variables. Initialize the first three elements with the following data:
    Make Model Year Cost
    Ford Taurus 1997 $ 21,000
    Honda Accord 1992 $ 11,000
    Lamborghini Countach 1997 $200,000
A

const int SIZE = 35;
Car forSale[SIZE] = {
{“Ford”, “Taurus”, 1997, 21000},
{“Honda”, “Accord”, 1992, 11000},
{“Lamborghini”, “Countach”, 1997, 200000} };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  1. Write a loop that will step through the array you defined in Question 21, displaying the contents of each element.
A

for (int x = 0; x < SIZE; x++)
{
cout &laquo_space;forSale[x].carMake &laquo_space;endl;
cout &laquo_space;forSale[x].carModel &laquo_space;endl;
cout &laquo_space;forSale[x].yearModel &laquo_space;endl;
cout &laquo_space;forSale[x].cost &laquo_space;endl &laquo_space;endl;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. Declare a structure named TempScale, with the following members:
    fahrenheit: a double
    centigrade: a double
    Next, declare a structure named Reading, with the following members:
    windSpeed: an int
    humidity: a double
    temperature: a TempScale structure variable
    Next define a Reading structure variable.
A

struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
Reading today;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
  1. Write statements that will store the following data in the variable you defined in
    Question 23.
    Wind Speed: 37 mph
    Humidity: 32%
    Fahrenheit temperature: 32 degrees
    Centigrade temperature: 0 degrees
A

today.windSpeed = 37;
today.humidity = .32;
today.temperature.fahrenheit = 32;
today.temperature.centigrade = 0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
  1. Write a function called showReading . It should accept a Reading structure variable (see Question 23) as its argument. The function should display the contents of the variable on the screen.
A

void showReading(Reading values)
{
cout &laquo_space;“Wind speed: “ &laquo_space;values.windSpeed &laquo_space;endl;
cout &laquo_space;“Humidity: “ &laquo_space;values.humidity &laquo_space;endl;
cout &laquo_space;“Fahrenheit temperature: “ «
values.temperature.fahrenheit &laquo_space;endl;
cout &laquo_space;“Centigrade temperature: “ «
values.temperature.centigrade &laquo_space;endl;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
  1. Write a function called findReading . It should use a Reading structure reference variable (see Question 23) as its parameter. The function should ask the user to enter values for each member of the structure.
A

void findReading(Reading &r)
{
cout &laquo_space;“Enter the wind speed: “;
cin&raquo_space; r.windSpeed;
cout &laquo_space;“Enter the humidity: “;
cin&raquo_space; r.humidity;
cout &laquo_space;“Enter the fahrenheit temperature: “;
cin&raquo_space; r.temperature.fahrenheit;
cout &laquo_space;“Enter the centigrade temperature: “;
cin&raquo_space; r.temperature.centigrade;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
  1. Write a function called getReading , which returns a Reading structure (see Question
A

Reading getReading()
{
Reading local;

cout &laquo_space;“Enter the following values:\n”;
cout &laquo_space;“Wind speed: “;
cin&raquo_space; local.windSpeed;
cout &laquo_space;“Humidity: “;
cin&raquo_space; local.humidity;
cout &laquo_space;“Fahrenheit temperature: “;
cin&raquo_space; local.temperature.fahrenheit;
cout &laquo_space;“Centigrade temperature: “;
cin&raquo_space; local.temperature.centigrade;
return local;
}

28
Q
  1. Write a function called recordReading . It should use a Reading structure pointer variable (see Question 23) as its parameter. The function should ask the user to enter values for each member of the structure pointed to by the parameter.
A

void recordReading(Reading *r)
{
cout &laquo_space;“Enter the wind speed: “;
cin&raquo_space; r->windSpeed;
cout &laquo_space;“Enter the humidity: “;
cin&raquo_space; r->humidity;
cout &laquo_space;“Enter the fahrenheit temperature: “;
cin&raquo_space; r->temperature.fahrenheit;
cout &laquo_space;“Enter the centigrade temperature: “;
cin&raquo_space; r->temperature.centigrade;
}

29
Q
  1. Rewrite the following statement using the structure pointer operator:
    (*rptr).windSpeed = 50;
A
  1. rptr->WindSpeed = 50;
30
Q
  1. Rewrite the following statement using the structure pointer operator:
    (strPtr).num = 10;
A
  1. strPtr->num = 10;
31
Q
  1. Write the declaration of a union called Items with the following members:
    alpha a character
    num an integer
    bigNum a long integer
    real a float
    Next, write the definition of an Items union variable.
A

union Items
{
char alpha;
int num;
long bigNum;
float real;
};
Items x;

32
Q
  1. Write the declaration of an anonymous union with the same members as the union you declared in Question 31.
A

union
{
char alpha;
int num;
long bigNum;
float real;
};

33
Q
  1. Write a statement that stores the number 452 in the num member of the anonymous union you declared in Question 32.
A
  1. num = 452;
34
Q
  1. Look at the following statement.
    enum Color { RED, ORANGE, GREEN, BLUE };
    A) What is the name of the data type declared by this statement?
    B) What are the enumerators for this type?
    C) Write a statement that defines a variable of this type and initializes it with a valid value.
A

A) Color
B) RED, ORANGE, GREEN, and BLUE
C) Color myColor = BLUE;

35
Q
  1. A pet store sells dogs, cats, birds, and hamsters. Write a declaration for an anonymous enumerated data type that can represent the types of pets the store sells
A
  1. enum Pets { DOGS, CATS, BIRDS, HAMSTERS };
36
Q
  1. T F A semicolon is required after the closing brace of a structure or union declaration.
A

true

37
Q
  1. T F A structure declaration does not define a variable.
A

true

38
Q
  1. T F The contents of a structure variable can be displayed by passing the structure variable to the cout object.
A

false

39
Q
  1. T F Structure variables may not be initialized.
A

false

40
Q
  1. T F In a structure variable’s initialization list, you do not have to provide initializers for all the members.
A

true

41
Q
  1. T F You may skip members in a structure’s initialization list.
A

false

42
Q
  1. T F The following expression refers to element 5 in the array carInfo :
    carInfo.model[5]
A

false

43
Q
  1. T F An array of structures may be initialized.
A

true

44
Q
  1. T F A structure variable may not be a member of another structure.
A

false

45
Q
  1. T F A structure member variable may be passed to a function as an argument.
A

true

46
Q
  1. T F An entire structure may not be passed to a function as an argument.
A

false

47
Q
  1. T F A function may return a structure.
A

true

48
Q
  1. T F When a function returns a structure, it is always necessary for the function to have a local structure variable to hold the member values that are to be returned.
A

true

49
Q
  1. T F The indirection operator has higher precedence than the dot operator.
A

false

50
Q
  1. T F The structure pointer operator does not automatically dereference the structure pointer on its left.
A

false

51
Q
  1. T F In a union , all the members are stored in different memory locations.
A

false

52
Q
  1. T F All the members of a union may be used simultaneously.
A

false

53
Q
  1. T F You may define arrays of union s.
A

true

54
Q
  1. T F You may not define pointers to union s.
A

false

55
Q
  1. T F An anonymous union has no name.
A

true

56
Q
  1. T F If an anonymous union is defined globally (outside all functions), it must be declared static .
A

true

57
Q
  1. struct
    {
    int x;
    float y;
    };

Find the Errors

A
  1. The structure declaration has no tag.
58
Q
  1. struct Values
    {
    string name;
    int age;
    }

Find the Errors

A
  1. The semicolon is missing after the closing brace.
59
Q
  1. struct TwoVals
    {
    int a, b;
    };
    int main ()
    {
    TwoVals.a = 10;
    TwoVals.b = 20;
    return 0;
    }

Find the Errors

A
  1. No structure variable has been declared. TwoVals is the structure tag.
60
Q
  1. # include <iostream></iostream>using namespace std;
    struct ThreeVals
    {
    int a, b, c;
    };
    int main()
    {
    ThreeVals vals = {1, 2, 3};
    cout &laquo_space;vals &laquo_space;endl;
    return 0;
    }

Find the Errors

A
  1. In the definition of vals, TwoVals is used as the type instead of ThreeVals.

An entire struct cannot be sent to cout.

61
Q
  1. # include <iostream></iostream>#include <string>
    using namespace std;
    struct names
    {
    string first;
    string last;
    };
    int main ()
    {
    names customer = "Smith", "Orley";
    cout << names.first << endl;
    cout << names.last << endl;
    return 0;
    }</string>

Find the Errors

A
  1. The initialization list of the customer variable must be enclosed in braces.
62
Q
  1. struct FourVals
    {
    int a, b, c, d;
    };
    int main ()
    {
    FourVals nums = {1, 2, , 4};
    return 0;

Find the Errors

A
  1. An initializer cannot be skipped before the end of the initialization list.
63
Q

using namespace std;
struct TwoVals
{
int a = 5;
int b = 10;
};
int main()
{
TwoVals v;
cout &laquo_space;v.a &laquo_space;” “ &laquo_space;v.b;
return 0;
}

Find the Errors

A
  1. Structure members cannot be initialized in the structure definition.
64
Q
  1. struct TwoVals
    {
    int a = 5;
    int b = 10;
    };
    int main()
    {
    TwoVals varray[10];
    varray.a[0] = 1;
    return 0;
    }

Find the Errors

A

Structure members cannot be initialized in the structure definition.
The statement varray.a[0] = 1 should read:
varray[0].a = 1;

65
Q
  1. struct TwoVals
    {
    int a;
    int b;
    };
    TwoVals getVals()
    {
    TwoVals.a = TwoVals.b = 0;
    }

Find the Errors

A
  1. The function must define a variable of the TwoVals structure. The variable, then, should be used in the assignment statement.
66
Q
  1. struct ThreeVals
    {
    int a, b, c;
    };
    int main ()
    {
    TwoVals s, *sptr = nullptr;
    sptr = &s;
    *sptr.a = 1;
    return 0;
    }

Find the Errors

A
  1. In the declaration of the structure variables in main, TwoVals is used as the type instead of ThreeVals.

The statement that reads
*sptr.A = 1;
should read
sptr->A = 1;

67
Q
  1. # include <iostream></iostream>using namespace std;
    union Compound
    {
    int x;
    float y;
    };

Find the Errors

A
  1. Both x and y cannot be meaningfully used at the same time.