Chapter 9 C++ LIT
What will the following code output? int number = 22; int *var = &number; cout << var << endl; The address of the number variable 22 An asterisk followed by 22 An asterisk followed by the address of the number variable
The address of the number variable
What will the following statement output? cout << &num1; The value stored in the variable called num1 The memory address of the variable called num1 The number 1 The string "&num1" None of these
The memory address of the variable called num1
Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr;
The value stored in the variable that's address is in ptr.
The ___ and ___ operators can be used to increment or decrement a pointer variable.
++, --
The __, also known as the address operator, returns the memory address of a variable.
&
When this is placed in front of a variable name, it returns the address of that variable.
&
What will the following code output? int number = 22; int *var = &number; cout << *var << endl; The address of the number variable 22 An asterisk followed by 22 An asterisk followed by the address of the number variable
22
Which of the following statements is not valid C++ code? int ptr = &num1; int ptr = int *num1; float num1 = &ptr2; All of these are valid. All of these are invalid.
All of these are invalid.
A pointer variable may be initialized with ________. A) any non-zero integer value B) a valid address in the computer's memory C) an address less than 0 D) A and C only E) None of these
B) a valid address in the computer's memory
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) A and C only E) None of these
B) smart pointer
A pointer variable is designed to store ________. A) any legal C++ value B) only floating-point values C) a memory address D) an integer E) None of these
C) a memory address
Look at the following statement: sum += *array++; This statement ________. A) is illegal in C++ B) will always result in a compiler error C) assigns the dereferenced pointer's value, then increments the pointer's address D) increments the dereferenced pointer's value by one, then assigns that value E) None of these
C) assigns the dereferenced pointer's value, then increments the pointer's address
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
C) created with the new operator
Look at the following code: int numbers[] = {0, 1, 2, 3, 4 }; int *ptr = numbers; ptr++; After this code executes, which of the following statements is true? A) ptr will hold the address of numbers[0]. B) ptr will hold the address of the 2nd byte within the element numbers[0]. C) ptr will hold the address of numbers[1]. D) This code will not compile.
C) ptr will hold the address of numbers[1].
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 the addresses used to store the variable C) the address of the first byte of storage D) general delivery E) None of these
C) the address of the first byte of storage
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
D) 2
Which statement displays the address of the variable num1? A) cout << num1; B) cout << *num1; C) cin >> &num1; D) cout << &num1; E) None of these
D) cout << &num1;
The following statement: cin >> *num3; A) stores the keyboard input into the variable num3 B) stores the keyboard input into the pointer called num3 C) is illegal in C++ D) stores the keyboard input into the variable pointed to by num3 E) None of these
D) stores the keyboard input into the variable pointed to by num3
What does the following statement do? double *num2; Declares a double variable named num2. Declares and initializes an pointer variable named num2. Initializes a variable named *num2. Declares a pointer variable named num2. None of these
Declares a pointer variable named num2.
When you pass a pointer as an argument to a function, you must ________. A) declare the pointer variable again in the function call B) dereference the pointer variable 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
E) None of these
True/False: The ampersand (&) is used to dereference a pointer variable in C++.
False
True/False: With pointer variables you can access, but you cannot modify, data in other variables.
False
True/False: A pointer can be used as a function argument, giving the function access to the original argument.
True
True/False: An array name is a pointer constant because the address stored in it cannot be changed during runtime.
True
True/False: Assuming myValues is an array of int values, and index is an int variable, both of the following statements do the same thing. cout << myValues[index] << endl; cout << *(myValues + index) << endl
True
True/False: 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.
True
True/False: In C++ 11, the nullptr key word was introduced to represent the address 0.
True
True/False: 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.
True
True/False: It is legal to subtract a pointer variable from another pointer variable.
True
True/False: To use any of the smart pointers in C++ 11, you must #include the memory header file with the following directive: #include <memory>
True
The contents of pointer variables may be changed with mathematical statements that perform
addition and subtraction
Every byte in the computer's memory is assigned a unique ________. pointer address dynamic allocation name None of these
address
_____ can be used as pointers
array names
The following statement: int *ptr = new int; results in a compiler error assigns an integer less than 32767 to the variable named ptr assigns an address to the variable named ptr creates a new pointer named int None of these
assigns an address to the variable named ptr
Which of the following statements deletes memory that has been dynamically allocated for an array? int array = delete memory; int delete[ ]; delete [] array; new array = delete; None of these
delete [] array;
With pointer variables, you can ______ manipulate data stored in other variables.
indirectly
The statement: int *ptr = nullptr; has the same meaning as ________. int ptr = nullptr; *int ptr = nullptr; int ptr* = nullptr; int* ptr = nullptr; None of these
int* ptr = nullptr;
Not all arithmetic operations may be performed on pointers. For example, you cannot ___ or ___ a pointer.
multiply or divide
In C++ 11, the _____ key word was introduced to represent the address 0.
nullptr
Look at the following statement: int *ptr = nullptr; In this statement, what does the word int mean? The variable named *ptr will store an integer value. The variable named *ptr will store an asterisk and an integer value. ptr is a pointer variable that will store the address of an integer variable. All of these None of these
ptr is a pointer variable that will store the address of an integer variable.
A function may return a pointer, but the programmer must ensure that the pointer ________. still points to a valid object after the function ends has not been assigned an address was received as a parameter by the function has not previously been returned by another function None of these
still points to a valid object after the function ends
When you work with a dereferenced pointer, you are actually working with
the actual value of the variable whose address is stored in the pointer variable.
When the less than ( < ) operator is used between two pointer variables, the expression is testing whether ________. the value pointed to by the first is less than the value pointed to by the second the value pointed to by the first is greater than the value pointed to by the second the address of the first variable comes before the address of the second variable in the computer's memory the first variable was declared before the second variable None of these
the address of the first variable comes before the address of the second variable in the computer's memory
If you are using an older compiler that does not support the C++ 11 standard, you should initialize pointers with ________. the integer 0, or the value NULL the null terminator '\0' a nonzero value All of these None of these
the integer 0, or the value NULL
Dynamic memory allocation occurs ________. when a new variable is created by the compiler when a new variable is created at runtime when a pointer fails to dereference the right variable when a pointer is assigned an incorrect address None of these
when a new variable is created at runtime