CIS-265 Test 1 Cheat Sheet
Chapter 6
1. Why do local variables lose their values between calls to the function in which they are defined?
They are created in memory when the function begins execution and are destroyed when the function ends.
2. What is the difference between an argument and a parameter variable?
An argument is a value passed to a function and a parameter variable is a variable local to the function which receives the argument. The argument’s value is copied into the parameter variable.
3. Where do you define parameter variables?
Inside the parentheses of a function header.
4. If you are writing a function that accepts an argument and you want to make sure the function cannot change the value of the argument, what do you do?
If the argument is passed by value, nothing needs to be done. The function cannot access the argument. If the argument is passed by reference, the parameter should be defined with the const key word.
5. When a function accepts multiple arguments, does it matter in what order the arguments are passed?
Yes, the first argument is passed into the parameter variable that appears first inside the function header’s parentheses. Likewise, the second argument into the second parameter, and so on.
6. How do you return a value from a function?
With the return statement. For example, the following statement returns the value in the variable x.
return x;
7. What is the advantage of breaking your application’s code into several small procedures?
It makes the program easier to manage. Imagine a book that has a thousand pages but isn’t divided into chapters or sections. Trying to find a single topic in the book would be very difficult. Real-world programs can easily have thousands of lines of code, and unless they are modularized, they can be very difficult to modify and maintain.
8. How would a static local variable be useful?
It would enable a function to retain a value between function calls. For example, it can keep track of the number of times the function has been called.
9. Give an example where passing an argument by reference would be useful.
A function such as the following could be written to get user input. The input is stored in the variables that are passed as arguments.
void getValues (int &x, int &y)
{
cout cin >> x;
cout cin >> y;
}
10. The ____ is the part of a function definition that shows the function name, return type, and parameter list.
Header
11. If a function doesn’t return a value, the word ____ will appear as its return type.
Void
12. Either a function’s ____ or its ____ must precede all calls to the function.
Definition, prototype
13. Values that are sent into a function are called ____
Arguments
14. Special variables that hold copies of function arguments are called ____
Parameters
15. When only a copy of an argument is passed to a function, it is said to be passed by ____
Value
16. A(n) ____ eliminates the need to place a function definition before all calls to the function.
Prototype
17. A(n) ____ variable is defined inside a function and is not accessible outside the function
Local
18. ____ variables are defined outside all functions and are accessible to any function within their scope.
Global
19. ____ variables provide an easy way to share large amounts of data among all the functions in a program.
Global
20. Unless you explicitly initialize global variables, they are automatically initialized to ____
0
21. If a function has a local variable with the same name as a global variable, only the ____ variable can be seen by the function.
Local
22. ____ local variables retain their value between function calls.
Static
23. The ____ statement causes a function to end immediately.
Return
24. ____ arguments are passed to parameters automatically if no argument is provided in the function call.
Default
25. When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined ____
Last
26. The value of a default argument must be a(n) ____
Constant
27. When used as parameters, ____ variables allow a function to access the parameter’s original argument.
Reference
28. Reference variables are defined like regular variables, except there is a(n) ____ in front of the name.
&
29. Reference variables allow arguments to be passed by ____
Reference
30. The ____ function causes a program to terminate.
Exit
31. Two or more functions may have the same name, as long as their ____ are different.
Parameter lists
32. Examine the following function header, then write an example call to the function.
void showValue(int quantity)
showValue(x);
33. The following statement calls a function named half. The half function returns a value that is half that of the argument. Write the function.
result = half(number);
double half (double num)
{
return num / 2;
}
34. A program contains the following function.
int cube (int num)
{
return num * num * num;
}
Write a statement that passes the value to 4 to this function and assigns its return value to the variable result.
result = cube (4);
35. Write a function named timesTen that accepts an argument. When the function is called, it should display the product of its argument multiplied times 10.
void timesTen (int num)
{
cout }
36. A program contains the following function.
void display (int arg1, double arg2, char arg3)
{
cout }
Write a statement that calls the procedure and passes the following variables to it:
int age;
double income;
char initial;
display (age, income, initial);
37. Write a function named getNumber that uses a reference parameter variable to accept an integer argument. The function should prompt the user to enter a number in the range of 1 through 100. The input should be validated and stored in the parameter variable.
void getNumber (int &num)
{
cout cin >> num;
while (num 100)
{
cout cout cin >> num;
}
}
38. Functions should be given names that reflect their purpose.
True
39. Function headers are terminated with a semicolon.
False
40. Function prototypes are terminated with a semicolon.
True
41. If other functions are defined before main, the program still starts executing at function main.
True
42. When a function terminates, it always branches back to main, regardless of where it was called from.
False
43. Arguments are passed to the function parameters in the order they appear in the function call.
True
44. The scope of a parameter is limited to the function which uses it.
True
45. Changes to a function parameter always affect the original argument as well.
False
46. In a function prototype, the names of the parameter variables may be left out.
True
47. Many functions may have local variables with the same name.
True
48. Overuse of global variables can lead to problems.
True
49. Static local variables are not destroyed when a function returns.
True
50. All static local variables are initialized to -1 by default.
False
51. Initialization of static local variables only happens once, regardless of how many times the function in which they are defined is called.
True
52. When a function with default arguments is called and an argument is left out, all arguments that come after it must be left out as well.
True
53. It is not possible for a function to have some parameters with default arguments and some without.
False
54. The exit function can only be called from main.
False
55. A stub is a dummy function that is called instead of the actual function it represents.
True
56. void total (int value1, value2, value3)
{
return value1 + value2 + value3;
}
The data type of value2 and value3 must be defined.
The function is declared void but returns a value.
57. double average (int value1, int value2, int value3)
{
double average;
average = value1 + value2 + value3 / 3;
}
The assignment statement should read:
average = (value1 + value2 + value3) / 3.0;
58. void area (int length = 30, int width)
{
return length * width;
}
Width should have a default argument value.
The function is defined as a float but returns no value.
59. void getValue (int value&)
{
cout cin >> value&;
}
The parameter should be defined as: int &value
The cin statement should read: cin >> value;
60. (Overloaded functions)
int getValue()
{
int inputValue;
cout cin >> inputValue;
return inputValue;
}
The functions must have different parameter lists.
Chapter 7
1. What is the difference between a size declarator and a subscript?
The size declarator is used in a definition of an array to indicate the number of elements the array will have. A subscript is used to access a specific element in an array.
2. Look at the following array definition.
int values [10];
How many elements does the array have?
What is the subscript of the first element in the array?
What is the subscript of the last element in the array?
Assuming that an int uses four bytes of memory, how much memory does the array use?
The array has 10 elements.
The subscript of the first element is 0.
The subscript of the last element is 9.
Using four-byte integers, this array uses 40 bytes of memory.
3. Why should a function that accepts an array as an argument, and processes that array, also accept an argument specifying the array’s size?
With the array alone, the function has no way of determining the number of elements it has.
4. Consider the following array definition: int values [5] = {4, 7, 6, 8, 2};
What does each of the following statements display?
cout 2
cout 14
cout 8
5. How do you define an array without providing a size declarator?
By providing an initialization list. The array is sized to hold the number of values in the list.
6. Look at the following array definition: int numbers [5] = {1, 2, 3};
What value is stored in numbers [2]?
What value is stored in numbers [4]?
3
0
7. Assuming that array1 and array2 are both arrays, why is it not possible to assign the contents of array2 to array1 with the following statement?
array1 = array2;
An array name without brackets and a subscript represents the array’s beginning memory address. The statement shown attempts to assign the address of array2 to array1, which is not permitted.
8. Assuming that numbers is an array of doubles, will the following statement display the contents of the array?
cout
No
9. Is an array passed to a function by value or by reference?
By reference
10. When you pass an array name as an argument to a function, what is actually being passed?
The array’s beginning memory address.
11. How do you establish a parallel relationship between two or more arrays?
By using the same subscript value for each array.
12. Look at the following array definition: double sales [8][10];
How many rows does the array have?
How many columns does the array have?
How many elements does the array have?
Write a statement that stores a number in the last column of the last row in the array.
8 rows
10 columns
80 elements
sales [7][9] = 123.45;
13. When writing a function that accepts a two-dimensional array as an argument, which size declarator must you provide in the parameter for the array?
The second size declarator, which is for the number of columns.
14. What advantages does a vector offer over an array?
You do not have to declare the number of elements that a vector will have.
If you add a value to a vector that is already full, the vector will automatically increase its size to accommodate the new value.
A vector can report the number of elements it contains.
15. The ____ indicates the number of elements, or values, an array can hold.
Size declarator
16. The size declarator must be a(n) ____ with a value greater than ____
Integer, 0
17. Each element of an array is accessed and indexed by a number known as a(n) ____
Subscript
18. Subscript numbering in C++ always starts at ____
0
19. The number inside the brackets of an array definition is the ____, but the number inside an array’s brackets in an assignment statement, or any other statement that works with the contents of the array, is the ____
Size declarator, subscript
20. C++ has no array ____ checking, which means you can inadvertently store data past the end of an array.
Bounds
21. Starting values for an array may be specified with a(n) ____ list.
Initialization
22. If an array is partially initialized, the uninitialized elements will be set to ____
0
23. If the size declarator of an array definition is omitted, C++ counts the number of items in the ____ to determine how large the array should be.
Initialization list
24. By using
the same ____ for multiple arrays, you can build relationships between the data
stored in arrays.
Subscript
25. You
cannot use the ____ operator to copy data from one array to another in a single
statement.
=
26. Any time
the name of an array is used without brackets and a subscript, it is seen as
____
An address
27. To pass
an array to a function, pass the ____ of the array.
Address, or name
28. A(n)
____ array is like several arrays of the same type put together.
Multi-dimensional
29. It’s
best to think of a two-dimensional as having ____ and ____
Rows, columns
30. To
define a two-dimensional array, ____ size declarators are required.
2
31. When
initializing a two-dimensional array, it helps to enclose each row’s
initialization list in ____
Braces
32. When a
two-dimensional array is passed to a function the ____ size must be specified.
Column
33. The ____
is a collection of programmer-defined data types and algorithms that you may
use in your programs.
Standard Template Library (STL)
34. The two
types of containers defined by the STL are ____ and ____
Sequence and associative
35. The
vector data type is a(n) ____ container.
Sequence
36. To
define a vector in your program, you must #include the ____ header file.
Vector
37. To store
a value in a vector that does not have a starting size, or that is already
full, use the ____ member function.
Push_back
38. To
determine the number of elements in a vector, use the ____ member function.
Size
39. Use the
____ member function to remove the last element from a vector.
Pop_back
40. To
completely clear the contents of a vector, use the ____ member function.
Clear
41. names is an integer array with 20 elements. Write a regular for loop, as well as a range-based for loop that prints each element of the array.
const int SIZE = 20;
for (int i = 0; i cout
42. The arrays numberArray1 and numberArray2 have 100 elements. Write code that copies the values in numberArray1 to numberArray2.
Const int SIZE = 10;
for (int i = 0; i numberArray2 [i] = numberArray1
[i];
43. In a program you need to store identification numbers of 10 employees (as ints) and weekly gross pay (as doubles).
a) Define
two arrays that may be used in parallel to store the 10 employee identification
numbers and gross pay amounts.
b) Write a loop that uses these arrays to print each employee’s identification
number and weekly gross pay.
const int SIZE = 10;
int id [SIZE];//
To hold ID numbers
double weeklyPay [SIZE];// TO
hold weekly pay
for (int i = 0; i
{
cout cout cout cout
}
44. Define a two-dimensional array of integers named grades. It should have 30 rows and 10 columns.
const int ROWS = 30, COLS = 10;
int grades [ROWS] [COLS];
45. In a program you need to store the population of 12 countries.
a) Define
two arrays that may be used in parallel to store the names of the countries and
their populations.
b) Write a loop that uses these arrays to print each country’s name and its
population.
const int SIZE = 12;
const int NAME_SIZE = 25;
// A 2D array to hold the country names
char countries [SIZE] [NAME_SIZE];
// An array to hold populations
long populations [SIZE];
// Display each country’s name and
population
for (int i = 0; i
{
cout cout
}
46. The following code totals the values in two arrays: numberArray1 and numberArray2. Both arrays have 25 elements. Will the code print the correct sum of values for both arrays? Why or why not?
int total =
0;//
Accumulator
int count;//
Loop counter
// Calculate and display the total of the first array.
for (count = 0; count total += numberArray1 [count];
cout
// Calculate
and display the total of the second array.
for (count = 0; count total += numberArray2 [count];
cout
No, the correct sum will not be printed for numberArray2 because the accumulator (total) is not set back to zero before the second loop executes.
47. Look at the following array definition: int numberArray [9] [11];
Write a
statement that assigns 145 to the first column of the first row of this array.
Write a statement that assigns 18 to the last column of the last row of this
array.
numberArray [0] [0] = 145;
numberArray [8] [10] = 18;
48. values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total.
const int ROWS = 10;
const int COLS = 20;
int row, col;
float total = 0.0;
// Sum the values in the array.
for (row = 0; row
{
for (col = 0; col total += values [row]
[col];
}
49. An application
uses a two-dimensional array defined as follows:
int days [29] [5];
Write code
that sums each row in the array and displays the results.
Write code that sums each column in the array and displays the results.
const int NUM_ROWS = 29;
const int NUM_COLS = 5;
int row, col,
total;
// Display the sum of each row.
for (row = 0; row
{
// Set the accumulator.
total = 0;
// Sum a row.
for (col = 0; col total += days [row]
[col];
// Display the row’s total.
cout cout
}
// Display the sum of each column.
for (col = 0; col
{
// Set the accumulator.
total = 0;
// Sum a column.
for (row = 0; row total += days [row]
[col];
// Display the column’s total.
cout cout
}
50. An
array’s size declarator can be either a literal, a named constant, or a
variable.
False
51. To
calculate the amount of memory used by an array, multiply the number of
elements by the number of bytes each element uses.
True
52. The
individual elements of an array are accessed and indexed by unique numbers.
True
53. The
first element in an array is accessed by the subscript 1.
False
54. The
subscript of the last element in a single-dimensional array is one less than
the total number of elements in the array.
True
55. The
contents of an array element cannot be displayed with cout.
False
56.
Subscript numbers may be stored in variables.
True
57. You can
write programs that use invalid subscripts for array.
True
58. Arrays
cannot be initialized when they are defined. A loop or other means must be
used.
False
59. The
values in an initialization list are stored in the array in the order they
appear in the list.
True
60. C++
allows you to partially initialize an array.
True
61. If an
array is partially initialized, the uninitialized elements will contain
“garbage.”
False
62. If you
leave an element uninitialized, you do not have to leave all the ones that
follow it uninitialized.
False
63. If you
leave out the size declarator of an array definition, you do not have to
include an initialization list.
False
64. The
uninitialized elements of a string array will automatically be set to the value
“0”.
False
65. You
cannot use the assignment operator to copy one array’s contents to another in a
single statement.
True
66. When an
array name is used without brackets and a subscript, it is seen as the value of
the first element in the array.
False
67. To pass
an array to a function, pass the name of the array.
True
68. When
defining a parameter variable to hold a single-dimensional array argument, you
do not have to include the size declarator.
True
69. When an
array is passed to a function, the function has access to the original array.
True
70. A
two-dimensional array is like several identical arrays put together.
True
71. It’s
best to think of two-dimensional arrays as having rows and columns.
True
72. The
first size declarator (in the declaration of a two-dimensional array)
represents the number of columns. The second size definition represents the
number of rows.
False
73.
Two-dimensional arrays may be passed to functions, but the row size must be
specified in the definition of the parameter variable.
False
74. C++
allows you to create arrays with three or more dimensions.
True
75. A vector
is an associative container.
False
76. To use a
vector, you must include the vector header file.
True
77. Vectors
can report the number of elements they contain.
True
78. You can
use the [] operator to insert a value into a vector that has no elements.
False
79. If you
add a value to a vector that is already full, the vector will automatically
increase its size to accommodate the new value.
True
80. int
size;
double values [size];
The size declarator cannot be a variable.
81. int collection [-20];
The size declarator cannot be negative.
82. int
table [10];
for (int x = 0; x {
cout
cin >> table [x];
}
The loop will write data past the end of the array.
83. int hours [3] = 8, 12, 16;
The initialization list must be enclosed in braces.
84. int numbers [8] = {1, 2, , 4, , 5};
Two of the initialization values are left out.
85. float ratings [];
For the array to be implicitly sized there must be an initialization list.
86. char
greeting [] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};
cout
A null terminator must be specified in the initialization list.
87. int
array1 [4], array2 [4] = {3, 6, 9, 12};
array1 = array2;
The assignment operator cannot be used to assign the contents of one array to another, in a single statement.
88. void showValues
(int nums)
{
for
(int count = 0; count cout }
The parameter should be declared as int nums []. Also, the function should have a parameter to hold the size of the array.
89. void
showValues (int nums [4] [])
{
for (rows = 0; rows for (cols = 0; cols cout }
The parameter must specify the number of columns, not the number of rows.
90.
vector numbers = {1, 2, 3, 4};
You do not use an = operator before the
initialization list.
Chapter 9
1. What does
the indirection operator do?
It dereferences a point, allowing code
to work with the value that the pointer points to.
2. Look at the following code.
int x = 7;
int *iptr = &x;
What will be displayed if you send the expression *iptr to cout? What happens if you send the expression ptr to cout?
The value 7 will be displayed if the expression *iptr is sent to cout. If the expression iptr is sent to cout, the address of the variable x will be displayed.
3. So far
you have learned three different uses for the * operator. What are they?
Multiplication operator, definition of a
pointer variable, and the indirection operator.
4. What math
operations are allowed on pointers?
Addition and subtraction
5. Assuming
that ptr is a pointer to an int, what happens when you add 4 to ptr?
It adds 4 times the size of an int to
the address stored in ptr.
6. Look at the following array definition: int numbers [] = {2, 4, 6, 8, 10};
What will
the following statement display?
cout
8
7.What is
the purpose of the new operator?
To dynamically allocate memory.
8. What
happens when a program uses the new operator to allocate a block of memory, but
the amount of requested memory isn’t available? How do programs written with
older compilers handle this?
An exception is thrown, which causes the
program to terminate. Under older compilers, the new operator returns the null
address (address 0) when it cannot allocate the requested amount of memory.
9. What is
the purpose of the delete operator?
To free memory that has been dynamically
allocated with the new operator.
10. Under
what circumstances can you successfully return a pointer from a function?
You should only return a point from a
function if it is:
A pointer to an object that was
passed into the function as an argument.
A pointer to a dynamically allocated object.
11. What is
the difference between a pointer to a constant and a constant pointer?
A pointer to a constant point to a
constant item. The data that the pointer points to cannot change, but the
pointer itself can change. With a constant pointer, it is the pointer itself
that is constant. Once the pointer is initialized with an address, it cannot
point to anything else.
12. What are
two advantages of declaring a pointer parameter as a constant pointer?
Not only will this protect you from
writing code in the function that accidentally changes the argument, but the
function will be able to accept the addresses of both constant and non-constant
arguments.
13. Each
byte in memory is assigned a unique ____
Address
14. The ____
operator can be used to determine a variable’s address.
Address (&)
15. ____
variables are designed to hold addresses.
Pointer
16. The ____
operator can be used to work with the variable a pointer points to.
Indirection (*)
17. Array
names can be used as ____ and vice versa.
Pointers
18. Creating
variables while a program is running is called ____
Dynamic memory allocation
19. The ____
operator is used to dynamically allocate memory.
New
20. Under
older compilers, if the new operator cannot allocate the amount of memory
requested, it returns ____
0 or null
21. A
pointer that contains the address 0 is called a(n) ____ pointer.
Null
22. When a program
is finished with a chunk of dynamically allocated memory, it should free it
with the ____ operator.
Delete
23. You
should only use pointers with delete that were previously used with ____
New
24. Look at the following code
double value
= 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value variable.
cout
25. Look at the following array definition: int set [10];
Write a statement using pointer notation that stores the value 99 in set [7]?
*(set + 7) = 99;
26. Write code that dynamically allocates an array of 20 integers, then uses a loop to allow the user to enter values for each element in the array.
const int SIZE = 20;
int *ptr;
ptr = new int [SIZE];
for (int i = 0; i
{
cout cin >> ptr [i];
}
27. Assume
that tempNumbers is a pointer that points to a dynamically allocated array.
Write code that releases the memory used by the array.
delete [] tempNumbers;
28. Look at the following function definition:
void
getNumber (int &n)
{
cout cin >> n;
}
In this function, the parameter n is a reference variable. Rewrite the function so that n is a pointer.
void getNumber (int *n)
{
cout cin >> *n;
}
29. Write
the definition of ptr, a pointer to a constant int.
const int *ptr;
30. Write
the definition of ptr, a constant pointer to an int.
int * const ptr;
31.Each byte of memory is assigned a unique
address.
True
32. The *
operator is used to get the address of a variable.
False
33. Pointer
variables are designed to hold addresses.
True
34. The
& symbol is called the indirection operator.
False
35. The
& operator dereferences a pointer.
False
36. When the
indirection operator is used with a pointer variable, you are actually working
with the value the pointer in pointing to.
True
37. Array
names cannot be dereferenced with the indirection operator.
False
38. When you
add a value to a pointer, you are actually adding that number times the size of
the data type referenced by the pointer.
True
39. The
address operator is not needed to assign an array’s address to a pointer.
True
40. You can
change the address that an array name points to.
False
41. Any
mathematical operation, including multiplication and division, may be performed
on a pointer.
False
42. Pointers
may be compared using the relational operators.
True
43. When
used as function parameters, reference variables are much easier to work with
than pointers.
True
44. The new
operator dynamically allocates memory.
True
45. A
pointer variable that has not been initialized is called a null pointer.
False
46. The
address 0 is generally considered unusable.
True
47. In using
a pointer with the delete operator, it is not necessary for the pointer to have
been previously used with the new operator.
False
48. int ptr* = nullptr;
The variable should be declared as int *ptr;
49. int x,
*ptr = nullptr;
&x = ptr;
The assignment statement should read ptr = &x;
50. int x,
*ptr = nullptr;
*ptr = &x;
The assignment statement should read ptr = &x;
51. int x,
*ptr = nullptr;
ptr = &x;
ptr = 100;// Store 100 in x
cout
The assignment statement should read *ptr = 100;
52. int
numbers [] = {10, 20, 30, 40, 50};
cout cout
The last line should read: cout
53. int
values [20], *iptr = nullptr;
iptr = values;
iptr *= 2;
Multiplication cannot be performed on pointers.
54. float
level;
int fptr = &level;
An int pointer is being used to reference a float value.
55. int
*iptr = &ivalue;
int ivalue;
iptr cannot be initialized with the address of ivalue. ivalue is defined after iptr.
56. void
doubleVal (int val)
{
*val
*= 2;
}
The * operator is used on the parameter, but it is not a pointer.
57. int
*pint = nullptr;
new pint;
The second statement should read pint = new int;
58. int
*pint = nullptr;
pint = new int;
if (pint == nullptr)
*pint
= 100;
else
cout
The program segment is storing a value at the address 0.
59. int
*pint = nullptr;
pint = new int [100];// Allocate memory
delete pint;// Free memory
The last line should read delete [] pint;
60. int
*getNum()
{
int
wholeNum;
cout cin >> wholeNum;
return &wholeNum;
}
The function returns the address of a variable that no longer exists.
61. const
int arr [] = {1, 2, 3};
int *ptr = arr;
The pointer definition should read: const int *ptr = array;
62. void
doSomething (int * const ptr)
{
int
localArray [] = {1, 2, 3};
ptr = localArray;
}
The function tries to change the contents of ptr, which is a constant pointer. During the function’s execution, the value in ptr cannot be changed.