Concepts Ch6-8, 10 Flashcards

1
Q

A class is a programmer-defined data type that describes what objects of the class will look like when they are created. * True * False

A

True

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

A constant pointer is a pointer whose address value is constant. The value of the object at the address cannot be changed.

  • True
  • False
A

False. While a constant pointer is a pointer whose address value is constant, the value of the object at the address CAN be changed.

int * const ptr;

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

A program statement outside of a class can access a private member. * True * False

A

False. A compiler error will result.

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

A vector is a data type that allows you to store and manipulate 1D data.

  • True
  • False
A

True

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

A float, string, structure, or a class can be referenced by an address.

  • True
  • False
A

True

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

Arrays are automatically passed by reference.

  • True
  • False
A

True.

Caution: if you modify the array inside the function, you are modifying the actual data and any use of the array after the function returns will see the modifications made in the array.

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

Array names are pointer constants.

  • True
  • False
A

True

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

C++ allows a set of variables to be combined together into a single unit called a structure. * True * False

A

True

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

Arrays of structures are handled the same way as arrays of objects.

  • True
  • False
A

True

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

Class name and scope resolution operator (::) are an extension of the function name. When a function is defined outside the class declaration, you do not need to use the scope resolution operator. * True * False

A

False. When a function is defined outside of the class declaration, these must be present and must be located immediately before the function name in the function header.

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

Constant pointers must be assigned an intial value when created.

  • True
  • False
A

True

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

Constant pointers to constants make the address in the pointer constant and the value of what it points to constant.

  • True
  • False
A

True

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

Declaring function parameters as pointers is another way to pass objects by reference.

  • True
  • False
A

True

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

How are member of a structure accessed?

A

With the dot operator.

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

How are structures passed by default? * by value * by reference * by constant reference

A

Passed by value. A copy of the entire original structure is made and passed to the function.

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

How can you avoid dangling pointers?

A

By setting the pointer to NULL after you call delete on the pointer.

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

How can you increase the size of a vector after it has been initially declared?
vectormyVector(10);

A
  1. You can add values to the end of the vector by calling the push_back function.
    myVector.push_back(1.23);
  2. You can increase the size by calling the resize function.
    myVector.resize(20)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How should structures be passed by normally and what does it do for the function? * by value * by reference * by constant reference

A

Passed to functions by reference, which allows the function to access the member variables of the original structure, allowing it to change them.

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

If a function needs to store or change data in an object’s member variables, the object must be passed to it by value. * True * False

A

False. It must be passed to it by reference.

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

If the pointer is a pointer to a constant, you can use the indirection operator (*) or the structure pointer indirection operator (->) on the left side of an assignment statement.

  • True
  • False
A

False. You cannot use the * or -> operators on the left side of assignment statement.

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

If you do not need to modify the array in a function, you should not use the const keyword in the function declaration.

  • True
  • False
A

False. You should use the const keyword. This can prevent you from accidentally modifying the array values.

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

In an array, the name of an array variable is not a pointer.

  • True
  • False
A

False

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

Pointer variables never store an address.

  • True
  • False
A

False

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

To compare the contents of an array, what must you do?

A

You must compare their individual elements.

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

To declare a function with a 2D array parameter, you must specify the size of the second dimension:

void function2D(int[][31] monthDayData);

  • True
  • False
24
Q

To use any data type, what 2 things do you need to know?

A

What values it can hold

What operations apply to it

25
Q

To use vectors, your program header does not need to indicate that you are using namespace std.

  • True
  • False
A

False. You need to include namespace std.

26
Q

Using the indirection operator (*) allows you to dereference the pointer.

  • True
  • False
27
Q

What are 4 differences in the way a structure is declared to the way a class is declared?

A

the keyword struct is used instead of class * structures rarely include member functions, normally declaration only declares member variables * declarations normally do not include access specifiers public or private * members of structure default to being public

28
Q

What do the following decrement operators do to the array?

  • amount[count]–;
  • amount[count–];
A
  • amount[count]–; decrements the value stored in amount[count]
  • amount[count–]; decrements the variable count, but does nothing to the value stored in amount[count]
29
Q

What do the following vector member functions do?

  • vectorName.size()
  • vectorName.pop_back()
  • vectorName.clear()
A
  • vectorName.size() - returns the number of elements in the vector
  • vectorName.pop_back() - removes the last element of vectorName
  • vectorName.clear() - Clears a vector of all its elements
30
Q

What do you use to access individual elements of the array?

A

Subscripts.

32
Q

What does the vector function pop_back do?

A

The function pop_back is useful for removing the last element of the vector, and decreasing the size by one.

33
Q

What is an accessor function?What is a mutator function?

A

An accessor function uses the value of a class variable, but does not change it. Commonly known as get/getter functions because they just retrieve or use the value.A mutator function stores a value in a member variable or changes its value. Commonly known as set/setter functions because they set the value of a class variable.

33
Q

What does this return?

float* myBigNumbers = new float(3);

A

This returns a pointer to a section of memory with space for 3 type float objects. (12 total bytes)

34
Q

What is the difference between the array size declarator and a subscript?

A

The number inside the brackets in an array definition is the size declarator and it specifies how many elements the array holds.

The number inside the brackets in an assignment statement is a subscript and it specifies which element is being accessed.

36
Q

What is the difference when an array holding numeric values is defined globally and when it is defined locally?

A

When defined globally, all of its elements are initialized to zero by default.

Local arrays, however, have no default initialization value.

37
Q

What is the primary advantage of a vector over a 1D array?

A

Vectors are not fixed in size or length. You can add items to the end of the vector and the vector will automatically allocate more memory if needed to store the new item.

38
Q

What is the purpose of a class declarator?What is each object created called?

A

To describe what the objects created from the class will look like when they are constructed.Each object created from it is called an instance of a class, and defining a class object is called instantiating the class.

39
Q

What is the subscript of the last element in the array?

A

The subscript of the last element in the array is one less than the total number of elements in the array.

40
Q

What makes a union unique to a structure?

A

All member variables occupy the same memory area, so only one member can be used at a time. Unions conserve memory by storing all of their members in the same memory location.

41
Q

What operator is used to get the address of a variable.

A

The & (address) operator.

42
Q

When declaring pointer variables, never initialize them to NULL.

  • True
  • False
A

False, unless assigning an address during the declaration statement.

43
Q

When do you use the const keyword?

A

When you want to define a variable or a value that cannot be changed.

44
Q

When specifying a starting size for a vector, the size declarator is enclosed in square brackets.

  • True
  • False
A

False. It is enclosed in parentheses.

45
Q

When specifying higher dimensional arrays as parameters for functions, you must define the size of every dimension.

  • True
  • False
A

False. You must define the size of every dimension EXCEPT the first dimension.

46
Q

When using the new operator for dynamic memory allocation, the left side of the assignment operator must never be a pointer variable.

  • True
  • False
A

False. Always because the new operator returns the memory address of the space it has allocated for the new object.

48
Q

When you dereference a pointer, your program gets a copy of the actual object.

  • True
  • False
A

False.

Program gets the actual object at the adress, not a copy.

48
Q

When you add or subtract from a pointer, you move the pointer forward or backwards by an amount equal to the size of type of the object that pointer points to.

  • True
  • False
50
Q

Which has a higher operator predecedence?

  • * operator
  • ”.” operator
A

”.” operator. Which requires the use of parentheses around the dereferencing operator.

51
Q

Which is the correct way to compare a structure? * if (employee1.hours == employee2.hours) * if (employee1 == employee2

A

if (employee1.hours == employee2.hours)

52
Q

Which of the following are true about destructors? * have no return type * cannot accept arguments * there can only be one destructor

A

All are true.

53
Q

Which of the following initialization statements are valid?

  • int numbers[7] = {1, 2, 3, 4, 5, 6, 7, 9};
  • int numbers[7] = {1, , 4, , 3, 5, 7};
A

Neither. They are both illegal. The initialization list cannot have more values than the array can hold.

and

If you leave an element uninitialized, you must leave all the elements that follow it uninitialized as well.

54
Q

Which pointer declaration is invalid?

  • double *myDoublePointer;
  • double* myDoublePointer2;
  • double * myDoublePointer3;
A

None. They are all valid.

55
Q

Which vector declaration is valid?

  • vector exampleVector;
  • vector exampleVector(10);
  • vector exampleVector(10, 2);
  • vector vectorOne(10, 2);
    vector vectorTwo(vectorOne);
A

All four are valid.

56
Q

You can add data into an array by using the initializer method at any time.

  • True
  • False
A

False. The initializer method can only be used at the time you declare the array.

57
Q

You can enter or get data from an array in any order.

  • True
  • False
58
Q

You can only use the -> operator with structure pointers or class pointers.

  • True
  • False
59
Q

You define a pointer to a constant when you want the pointer to point to an object that cannot or should not be changed.

What happens in this case?

const double *rates;

A

rates is a pointer to a constant double

or

rates points to a constant double

and

The address of a variable must be assigned to the pointer to a constant.

60
Q

You do not need to specify an initialization list when you leave out the size declarator.

  • True
  • False
A

False. If you leave out the size declarator, you must specify an initialization list.