Full Test Bank Pointers Chapter 9 - Test Bank | C++ Control Structures 9e by Tony Gaddis. DOCX document preview.
Starting Out with C++ from Control Structures to Objects, 9e (Gaddis)
Chapter 9 Pointers
TRUE/FALSE
1. With pointer variables you can access but not modify data in other variables.
2. An array name is a pointer constant because the address stored in it cannot be changed at runtime.
3. In C++11, the nullptr keyword was introduced to represent the address 0.
4. It is legal to subtract a pointer variable from another pointer variable.
5. C++ does not perform array bounds checking, making it possible for you to assign a pointer the address of an element out of the boundaries of an array.
6. A pointer can be used as a function argument, giving the function access to the original argument.
7. The ampersand (&) is used to dereference a pointer variable in C++.
8. Assuming myValues is an array of int values and index is an int variable, both of the following statements do the same thing.
1. cout << myValues[index] << endl;
2. cout << *(myValues + index) << endl;
9. In C++11 you can use smart pointers to dynamically allocate memory and not worry about deleting the memory when you are finished using it.
10. The weak_ptr can share ownership of a piece of dynamically allocated memory.
11. The unique_ptr is the sole owner of a piece of dynamically allocated memory.
12. To use any of the smart pointers in C++11 you must use the following directive in the header file:
#include <memory>
MULTIPLE CHOICE
1. The __________, also known as the address operator, returns the memory address of a variable.
a. | asterisk (* ) |
b. | ampersand ( & ) |
c. | percent sign ( % ) |
d. | exclamation point ( ! ) |
e. | None of these |
2. With pointer variables you can __________ manipulate data stored in other variables.
a. | never |
b. | seldom |
c. | indirectly |
d. | All of these |
e. | None of these |
3. When you work with a dereferenced pointer, you are actually working with
a. | a variable whose memory has been allocated |
b. | a copy of the value pointed to by the pointer variable |
c. | the actual value of the variable whose address is stored in the pointer variable |
d. | None of these |
4. A function may return a pointer but the programmer must ensure that the pointer
a. | still points to a valid object after the function ends |
b. | has not been assigned an address |
c. | was received as a parameter by the function |
d. | has not previously been returned by another function |
e. | None of these |
5. What does the following statement do?
double *num2;
a. | Declares a double variable named num2 |
b. | Declares and initializes a pointer variable named num2 |
c. | Initializes a pointer variable named num2 |
d. | Declares a pointer variable named num2 |
e. | None of these |
6. In C++11, the __________ keyword was introduced to represent address 0.
a. | nullptr |
b. | NULL |
c. | weak_ptr |
d. | shared_ptr |
e. | None of these |
7. When the less than operator (<) is used between two pointer values, the expression is testing whether
a. | the value pointed to by the first is less than the value pointed to by the second |
b. | the value pointed to by the first is greater than the value pointed to by the second |
c. | the address of the first variable comes before the address of the second variable in the computer's memory |
d. | the first variable was declared before the second variable |
e. | None of these |
8. Use the delete operator only on pointers that were
a. | never used |
b. | not correctly initialized |
c. | created with the new operator |
d. | dereferenced inappropriately |
e. | None of these |
9. Which of the following is true about this statement:
sum += *array++;
a. | This statement is illegal in C++. |
b. | This statement will cause a compiler error. |
c. | This statement assigns the dereferenced pointer's value, then increments the pointer's address. |
d. | This statement increments the dereferenced pointer's value by one, then assign that value. |
e. | None of these |
10. In the following statement, what does int mean?
int *ptr = nullptr;
a. | The variable named *ptr will store an integer value. |
b. | The variable named *ptr will store an asterisk and an integer value |
c. | ptr is a pointer variable and will store the address of an integer variable. |
d. | The variable named *ptr will store the value in nullptr. |
e. | None of these |
11. Assuming ptr is a pointer variable, what will the following statement output?
cout << *ptr;
a. | the value stored in the variable whose address is contained in ptr |
b. | the string "*ptr" |
c. | the address of the variable whose address is stored in ptr |
d. | the address of the variable stored in ptr |
e. | None of these |
12. Which of the following statements is not valid C++ code?
a. | int ptr = &num1; |
b. | int ptr = int *num1; |
c. | float num1 = &ptr2; |
d. | All of these are valid |
e. | All of these are invalid |
13. Which of the following statements deletes memory that has been dynamically allocated for an array?
a. | int array = delete memory; |
b. | int delete[ ]; |
c. | delete [] array; |
d. | new array = delete; |
e. | None of these |
14. Which of the following statements displays the address of the variable numb?
a. | cout << numb; |
b. | cout << *numb; |
c. | cin >> &numb; |
d. | cout << &numb; |
e. | None of these |
15. What will the following statement output?
cout << &num1;
a. | the value stored in the variable named num1 |
b. | the memory address of the variable named num1 |
c. | the number 1 |
d. | the string &num1 |
e. | None of these |
16. The following statement __________
cin >> *num3;
a. | stores the keyboard input in the variable num3 |
b. | stores the keyboard input into the pointer num3 |
c. | is illegal in C++ |
d. | stores the keyboard input into the variable pointed to by num3 |
e. | None of these |
17. The following statement __________
int *ptr = new int;
a. | results in a compiler error |
b. | assigns an integer less than 32767 to the variable ptr |
c. | assigns an address to the variable ptr |
d. | creates a new pointer named int |
e. | None of these |
18. A pointer variable is designed to store
a. | any legal C++ value |
b. | only floating-point values |
c. | an integer |
d. | a memory address |
e. | None of these |
19. The __________ and __________ operators can be used to increment or decrement a pointer variable.
a. | addition, subtraction |
b. | ++, -- |
c. | modulus, division |
d. | All of these |
e. | None of these |
20. Not all arithmetic operations can be performed on pointers. For example, you cannot __________ or __________ pointers.
a. | multiply, divide |
b. | +=, -= |
c. | add, subtract |
d. | increment, decrement |
e. | None of these |
21. Dynamic memory allocation occurs
a. | when a new variable is created by the compiler |
b. | when a new variable is created at runtime |
c. | when a pointer fails to dereference the right variable |
d. | when a pointer is assigned an incorrect address |
e. | None of these |
22. Every byte in the computer's memory is assigned a unique
a. | pointer |
b. | address |
c. | dynamic allocation |
d. | name |
e. | None of these |
23. A pointer variable may be initialized with
a. | any nonzero integer value |
b. | a valid address in the computer's memory |
c. | an address less than zero |
d. | any nonzero number |
e. | None of these |
24. If a variable uses more than one byte of memory, for pointer purposes its address is
a. | the address of the last byte of storage |
b. | the average of all the addresses used to store that variable |
c. | the address of the first byte of storage |
d. | the address of the second byte of storage |
e. | None of these |
25. If you are using an older computer that does not support the C++11 standard, you should initialize pointers with
a. | the integer 0 or the value NULL |
b. | the null terminator '\0' |
c. | a nonzero value |
d. | Any of these |
e. | None of these |
26. When you pass a pointer as an argument to a function, you must
a. | declare the pointer value again in the function call |
b. | dereference the pointer value in the function prototype |
c. | use the #include<func.ptr.h> statement |
d. | not dereference the pointer in the function's body |
e. | None of these |
27. To help prevent memory leaks from occurring in C++11, a __________ automatically deletes a chunk of dynamically allocated memory when the memory is no longer being used.
a. | null pointer |
b. | smart pointer |
c. | dereferenced pointer |
d. | None of these |
28. What will the following code output?
int number = 22;
int *var = &number;
cout << *var << endl;
a. | the address of number |
b. | 22 |
c. | an asterisk followed by 22 |
d. | an asterisk followed by the address of number |
29. What will the following code output?
int number = 22;
int *var = &number;
cout << var << endl;
a. | the address of number |
b. | 22 |
c. | an asterisk followed by 22 |
d. | an asterisk followed by the address of number |
30. What will the following code output?
int *numbers = new int[5];
for (int i = 0; i <= 4; i++)
*(numbers + i) = i;
cout << numbers[2] << endl;
a. | five memory addresses |
b. | 0 |
c. | 3 |
d. | 2 |
e. | 1 |
31. After the code shown executes, which of the following statements is true?
int numbers[] = {0, 1, 2, 3, 4};
int *ptr = numbers;
ptr++;
a. | ptr will hold the address of numbers[0] |
b. | ptr will hold the address of the second byte within the element numbers[0] |
c. | ptr will hold the address of numbers[1] |
d. | this code will not compile |
32. Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int?
a. | unique_ptr<uniq> int( new int ); |
b. | unique_ptr<int> int( new uniq ); |
c. | unique_ptr<uniq> uniq( new int ); |
d. | unique_ptr<int> uniq( new int ); |
e. | None of these |
MULTIPLE RESPONSE
1. Select all that apply. Which of the following can be used as pointers?
a. | array names |
b. | numeric constants |
c. | keywords |
d. | None of these |
2. Select all that apply. Select as many of the following options that make this sentence true:
The contents of pointer variables may be changed with mathematical statements that perform
a. | multiplication |
b. | division |
c. | addition |
d. | subtraction |
e. | modulus |
3. Select all that apply. Of the following, which statements have the same meaning?
a. | int *ptr = nullptr; |
b. | int ptr = nullptr; |
c. | *int ptr = nullptr; |
d. | int* ptr = nullptr; |
e. | int ptr* = nullptr; |