Concepts Review Ch 4-8, 10 Flashcards Preview

CS 161 > Concepts Review Ch 4-8, 10 > Flashcards

Flashcards in Concepts Review Ch 4-8, 10 Deck (84)
Loading flashcards...
1
Q

Precedence of relational operators:

> >= <

== !=

  • True
  • False
A

True

1
Q

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

  • True
  • False
A

True

1
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.

1
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.

2
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.

4
Q

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

A
  1. What values it can hold
  2. What operations apply to it
5
Q

When an argument is passed into a parameter by _____, only a copy of the argument’s value is passed. Changes to the parameter do not affect the original argument.

A

value

5
Q

How can you avoid dangling pointers?

A

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

5
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.

6
Q

When would you use a for loop?

A

Ideal in situations where the exact number of iterations is known.

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.

8
Q

Which pointer declaration is invalid?

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

None. They are all valid.

9
Q

What is a mutator function?

A

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.

10
Q

What is an accessor 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.

11
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.

12
Q

A function cannnot send a value back to the part of the program that called the function.

  • True
  • False
A

False.

12
Q

Functions may return true or false values.

  • True
  • False
A

True

12
Q

When would you use a while loop?

A

Where you do not want the loop to iterate if the test condition is false from the beginning.

Ex: validating input that has been read and reading lists of data terminated by a sentinel value

13
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.

14
Q

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

  • True
  • False
A

True

15
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.

16
Q

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

A

Subscripts.

17
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)
18
Q

This eliminates the need to place a function definition before all calls to the function.

A

Function prototype

aka

function declarations

18
Q

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

  • True
  • False
A

False

18
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.

19
Q

_________ _________ are passed to parameters automatically if no argument is provided in the function call.

Where should they be assigned?

A

Default Arguments

A function’s default arguments should be assigned in the earliest occurrence of the function name. This will usually be the function prototype. If a function doesn’t have a prototype, default arguments may be specified in the function header.

20
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
21
Q

A __________ __________ is a statement that causes a function to execute.

A __________ __________ contains the statements that make up the function.

A

function call

function definition

22
Q

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

  • True
  • False
A

True

23
Q

What operator allows you to access members of a structure?

A

The dot operator.

24
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.

25
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
26
Q

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

  • True
  • False
A

False. A compiler error will result.

27
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.

28
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.

29
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.

30
Q

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

  • True
  • False
A

True

31
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.

33
Q

When do you use the const keyword?

A

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

34
Q

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

A

The & (address) operator.

35
Q

When would you use a do-while loop?

A

Where you always want the loop to iterate at least once. Ex: good choice for repeating a menu or for asking user if they want to repeat a set of actions.

37
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
A

True

38
Q

Which is the correct way to compare a structure?

  • if (employee1.hours == employee2.hours)
  • if (employee1 == employee2
A

if (employee1.hours == employee2.hours)

40
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.

41
Q

When declaring pointer variables, never initialize them to NULL.

  • True
  • False
A

False, unless assigning an address during the declaration statement.

42
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.

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

Constant pointers must be assigned an intial value when created.

  • True
  • False
A

True

46
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.

47
Q

What does the return statement do?

A

It causes a function to end immediately.

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
A

True

49
Q

When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be declared last.

  • True
  • False
A

True

50
Q

Array names are pointer constants.

  • True
  • False
A

True

51
Q

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

  • True
  • False
A

True

52
Q

What does the exit() function do?

A

Causes the a program to terminate, regardless of which function or control mechanism is executing.

53
Q

Pointer variables never store an address.

  • True
  • False
A

False

54
Q

A static local variable is destroyed when a function returns and exists for the entire life of the program.

  • True
  • False
A

False

55
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.

57
Q

When to should you use if(s)?

When should you use if/else?

When should you use if/else if? Trailing else?

A
  • If both conditions could be true or both could be false, use two separate if statements.
  • If the two conditions are mutually exclusive, such that one must be true and the other false, and if/else should be used.
  • Use this chain of if statements to perform tests, one after the other, until one of them is found to be true. A trailing else provides a default action, when none of the if expressions are true
58
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.

59
Q

You cannot have two local variables with the same name in the same function.

  • True
  • False
A

True

60
Q

What do the following vector member functions do?

  • vectorName.at(5)
  • vectorName.swap(vector2)
  • vectorName.reverse()
A
  • vectorName.at(5) - Returns the value if the element at position 5 in the vector
  • vectorName.swap(vector2) - Swaps the contents of the vectorName with the contents of vector2
  • vectorName.reverse() - Reverses the order of the elements in the vector (the last element becomes the first element, and the first element becomes the last element
61
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;

62
Q

What are overloading functions?

A

Two or more functions may have the same name, as long as their parameter lists are different.

64
Q

What is a reference variable?

A

A reference variable references the memory location of another variable. Any change made to the reference variable is actually made to the one it references. Only variables can be passed by reference.

65
Q

A local variable is defined inside a function and can be accessible outside the function.

  • True
  • False
A

False it is not accessible outside the function.

66
Q

Pretest or Post-test?

  • While loop
  • do-while loop
  • For loop
A
  • While loop – pretest
  • do-while loop – post-test
  • For loop – pretest
67
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.

68
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

69
Q

A program may be broken up into a set of manageable functions, or modules.

A

Modular programming.

70
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

.

71
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.

72
Q

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

  • True
  • False
A

True

73
Q

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

  • True
  • False
A

True

74
Q

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

A

You must compare their individual elements.

75
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.

76
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

77
Q

In a union, all member variables occupy the same memory area and only one member can be used at a time. This is not the same for structures.

  • True
  • False
A

True

78
Q

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

  • True
  • False
A

True.

79
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]
80
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)

81
Q

Which has a higher operator predecedence?

  • * operator
  • ”.” operator
A

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

82
Q

What must be assigned to the pointer to a constant?

A

The address of a variable.