CSCI 1 Chapter 9

25 July 2022
4.7 (114 reviews)
47 test answers

Unlock all answers in this set

Unlock answers (43)
question
With pointer variables you can access but not modify data in other variables -true -false
answer
false
question
An array name is a pointer constant because the address stored in it cannot be changed at runtime. -true -false
answer
true
question
In C++11, the nullptr keyword was introduced to represent the address 0. -true -false
answer
true
question
It is legal to subtract a pointer variable from another pointer variable. -true -false
answer
true
question
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 -false
answer
true
question
A pointer can be used as a function argument, giving the function access to the original argument. -true -fasle
answer
true
question
The ampersand (&) is used to dereference a pointer variable in C++ -true -false
answer
false
question
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 << myValue [index] << endl; 2. cout << *(myValues + index) << endl; -true -false
answer
true
question
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 -false
answer
true
question
The weak_ptr can share ownership of a piece of dynamically allocated memory. -true -fasle
answer
false
question
The unique_ptr is the sole owner of a piece of dynamically allocated memory. -true -false
answer
true
question
To use any of the smart pointers in C++11 you must use the following directive in the header file: -true -false
answer
true
question
The ________, also known as the address operator, returns the memory address of a variable. astreric(*) ampersand (&) percent(%) exclamation point(!) none of these
answer
ampersand(&)
question
With pointer variables you can ________ manipulate data stored in other variables. never seldom indirectly All of these None of these
answer
indirectly
question
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
answer
c) the actual value of the variable whose address is stored in the pointer variable
question
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
answer
a)still points to a valid object after the function ends
question
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
answer
d)Declares a pointer variable named num2
question
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
answer
a)nullptr
question
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
answer
c)the address of the first variable comes before the address of the second variable in the computer's memory
question
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
answer
c)created with the new operator
question
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
answer
c)This statement assigns the dereferenced pointer's value, then increments the pointer's address.
question
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
answer
c)ptr is a pointer variable and will store the address of an integer variable.
question
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
answer
a)the value stored in the variable whose address is contained in ptr
question
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
answer
e)All of these are invalid
question
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
answer
c)delete [] array;
question
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
answer
d)cout << &numb;
question
What will the following statement output? a)cout << &num1; b)the value stored in the variable named num1 c)the memory address of the variable named num1 the number 1 d)the string &num1 e)None of these
answer
c)the memory address of the variable named num1 the number 1
question
The following statement ________. cin >> *num3; -stores the keyboard input in the variable num3 -stores the keyboard input into the pointer num3 is illegal in C++ -stores the keyboard input into the variable pointed to by num3 - None of these
answer
-stores the keyboard input into the pointer num3 is illegal in C++
question
The following statement ________. int *ptr = new int; results in a compiler error assigns an integer less than 32767 to the variable ptr assigns an address to the variable ptr creates a new pointer named int None of these
answer
assigns an address to the variable ptr
question
A pointer variable is designed to store any legal C++ value only floating-point values an integer a memory address None of these
answer
memory address
question
The ________ and ________ operators can be used to increment or decrement a pointer variable. ] addition, subtraction ++, -- modulus, division All of these None of these
answer
++, --
question
Not all arithmetic operations can be performed on pointers. For example, you cannot ________ or ________ pointers. multiply, divide +=, -= add, subtract increment, decrement None of these
answer
multipy, divide
question
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
answer
when a new variable is created at runtime
question
Every byte in the computer's memory is assigned a unique pointer address dynamic allocation name None of these
answer
address
question
A pointer variable may be initialized with any nonzero integer value a valid address in the computer's memory an address less than zero any nonzero number None of these
answer
a valid address in the computer's memory
question
If a variable uses more than one byte of memory, for pointer purposes its address is the address of the last byte of storage the average of all the addresses used to store that variable the address of the first byte of storage the address of the second byte of storage None of these
answer
the address of the first byte of storage
question
If you are using an older computer that does NOT support the C++11 standard, you should initialize pointers with the integer 0 or the value NULL the null terminator ' ' a nonzero value Any of these None of these
answer
the integer 0 or the value NULL
question
When you pass a pointer as an argument to a function, you must declare the pointer value again in the function call dereference the pointer value in the function prototype use the #include statement not dereference the pointer in the function's body None of these
answer
none
question
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. null pointer smart pointer dereferenced pointer None of these
answer
smart pointer
question
What will the following code output? int number = 22; int *var = &number; cout << *var << endl; the address of number 22 an asterisk followed by 22 an asterisk followed by the address of number
answer
22
question
What will the following code output? int number = 22; int *var = &number; cout << var << endl; the address of number 22 an asterisk followed by 22 an asterisk followed by the address of number
answer
the address of number
question
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; five memory addresses 0 3 2 1
answer
2
question
After the code shown executes, which of the following statements is TRUE? int numbers[] = {0, 1, 2, 3, 4}; int *ptr = numbers; ptr++; ptr will hold the address of numbers[0] ptr will hold the address of the second byte within the element numbers[0] ptr will hold the address of numbers[1] this code will not compile
answer
ptr will hold the address of numbers[1]
question
Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int? unique_ptr int( new int ); unique_ptr int( new uniq ); unique_ptr uniq( new int ); unique_ptr uniq( new int ); None of these
answer
unique_ptr uniq( new int );
question
Which of the following can be used as pointers? array names numeric constants keywords None of these
answer
array name
question
elect 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 multiplication division addition subtraction modulus
answer
addition, subtraction
question
Select all that apply. Of the following, which statements have the same meaning? int *ptr = nullptr; int ptr = nullptr; *int ptr = nullptr; int* ptr = nullptr; int ptr* = nullptr;
answer
int *ptr = nullptr; int* ptr = nullptr;