Deck 9: Pointers
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/47
Play
Full screen (f)
Deck 9: Pointers
1
The weak_ptr can share ownership of a piece of dynamically allocated memory.
False
2
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
A) asterisk (* )
B) ampersand ( & )
C) percent sign ( % )
D) exclamation point ( ! )
E) None of these
B
3
The unique_ptr is the sole owner of a piece of dynamically allocated memory.
True
4
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
5
To use any of the smart pointers in C++11 you must use the following directive in the header file:
#include
#include
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
6
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
A) never used
B) not correctly initialized
C) created with the new operator
D) dereferenced inappropriately
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
7
Assuming myValues is an array of int values and index is an int variable, both of the following statements do the same thing.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
8
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.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
9
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
A) nullptr
B) NULL
C) weak_ptr
D) shared_ptr
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
10
In C++11, the nullptr keyword was introduced to represent the address 0.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
11
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
12
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
13
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
A) never
B) seldom
C) indirectly
D) All of these
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
14
With pointer variables you can access but not modify data in other variables.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
15
A pointer can be used as a function argument, giving the function access to the original argument.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
16
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.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
17
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
18
It is legal to subtract a pointer variable from another pointer variable.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
19
An array name is a pointer constant because the address stored in it cannot be changed at runtime.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
20
The ampersand (&) is used to dereference a pointer variable in C++.
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
21
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
22
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
23
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
A) cout << numb;
B) cout << *numb;
C) cin >> &numb;
D) cout << &numb;
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
24
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
25
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 statement
D) not dereference the pointer in the function's body
E) None of these
A) declare the pointer value again in the function call
B) dereference the pointer value in the function prototype
C) use the #include
D) not dereference the pointer in the function's body
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
26
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
27
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
28
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
A) null pointer
B) smart pointer
C) dereferenced pointer
D) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
29
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
A) multiply, divide
B) +=, -=
C) add, subtract
D) increment, decrement
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
30
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
31
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
32
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
33
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
A) addition, subtraction
B) ++, --
C) modulus, division
D) All of these
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
34
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
35
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
36
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
A) int array = delete memory;
B) int delete[ ];
C) delete [] array;
D) new array = delete;
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
37
Every byte in the computer's memory is assigned a unique
A) pointer
B) address
C) dynamic allocation
D) name
E) None of these
A) pointer
B) address
C) dynamic allocation
D) name
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
38
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
39
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
40
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
A) any legal C++ value
B) only floating-point values
C) an integer
D) a memory address
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
41
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
42
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
The contents of pointer variables may be changed with mathematical statements that perform
A) multiplication
B) division
C) addition
D) subtraction
E) modulus
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
43
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;
A) int *ptr = nullptr;
B) int ptr = nullptr;
C) *int ptr = nullptr;
D) int* ptr = nullptr;
E) int ptr* = nullptr;
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
44
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
A) array names
B) numeric constants
C) keywords
D) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
45
Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int?
A) unique_ptr int( new int );
B) unique_ptr int( new uniq );
C) unique_ptr uniq( new int );
D) unique_ptr uniq( new int );
E) None of these
A) unique_ptr
B) unique_ptr
C) unique_ptr
D) unique_ptr
E) None of these
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
46
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck
47
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
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
Unlock Deck
Unlock for access to all 47 flashcards in this deck.
Unlock Deck
k this deck