C++ (pointer) MULTIPLE CHOICE

Ace your homework & exams now with Quizwiz!

True/False: It is legal to subtract a pointer variable from another pointer variable. ANS: T

True

38. True/False (tricky): The ampersand (&) is used to dereference a pointer variable in C++.

False

39. 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

A pointer with the value 0 (zero) is called a NULL pointer.

True

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: 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

These can be used as pointers. a. Array names b. Numeric constants c. Punctuation marks d. All of these e. None of these

a. Array names

What will the following code output? int number = 22; int *var = &number; cout << var << endl; a. The address of the number variable c. An asterisk followed by 22 b. 22 d. An asterisk followed by the address of the number variable

a. The address of the number variable

Not all arithmetic operations may be performed on pointers. For example, you cannot ________ or __________ a pointer. a. multiply, divide b. add, subtract c. +=, -= d. increment, decrement e. None of these

a. multiply, divide

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

a. still points to a valid object after the function ends

When using the new operator with an older compiler, it is good practice to: a. test the pointer for the NULL address b. use a preprocessor directive c. clear the data from the old operator d. All of these

a. test the pointer for the NULL address

7. A pointer may be initialized with a. the address of an existing object b. the value of an integer variable c. the value of a floating point variable d. all of these

a. the address of an existing object

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 stored in ptr. d. the address of the variable whose address is stored in ptr.

a. the value stored in the variable whose address is contained in ptr.

With pointer variables, you can __________ manipulate data stored in other variables. a. never b. seldom c. indirectly d. All of these

c. indirectly

What will the following code output? int number = 22; int *var = &number; cout << *var << endl; a. The address of the number variable c. An asterisk followed by 22 b. 22 d. An asterisk followed by the address of the number variable ANS: B

b. 22

What will the following statement output? Int num1 = 3; cout << &num1; a. The value stored in the variable called num1. b. The memory address of the variable called num1. c. The number 1. d. The string "&num1". e. None of these

b. The memory address of the variable called num1.

Every byte in the computer's memory is assigned a unique a. pointer b. address c. dynamic allocation d. name

b. address

The _________ , also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation point ( ! )

b. ampersand ( & )

A pointer variable may be initialized with a. any non-zero integer value within the integer range. b. any address in the computer's memory allowed by the Operating System. c. an address less than 0 d. a and c only.

b. any address in the computer's memory allowed by the Operating System.

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

b. when a new variable is created at runtime

The ______ and _______ operators can be used to increment or decrement a pointer variable. a. addition, subtraction b. modulus, division c. ++, -- d. All of these e. None of these

c. ++, --

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.

The contents of pointer variables may be changed with mathematical statements that perform: a. all mathematical operations that are legal in C++ b. multiplication and division c. addition and subtraction d. b and c

c. addition and subtraction

When this is placed in front of a variable name, it returns the address of that variable. a. asterisk ( * ) b. conditional operator c. ampersand ( & ) d. semicolon ( ; )

c. ampersand ( & )

The statement int *ptr = new int; a. results in a compiler error. b. assigns an integer less than 32767 to the variable named ptr. c. assigns an address to the variable named ptr. d. creates a new pointer named int.

c. assigns an address to the variable named ptr.

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

c. assigns the dereferenced pointer's value, then increments the pointer's address

11. 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

c. created with the new operator

Look at the following statement. int *ptr; In this statement, what does the word int mean? 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 that will store the address of an integer variable d. All of these e. None of these

c. ptr is a pointer variable that will store the address of an integer variable

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] c. ptr will hold the address of numbers[1] b. ptr will hold the address of the 2nd byte within the element numbers[0] d. This code will not compile.

c. ptr will hold the address of numbers[1]

The statement cin >> *num3; a. stores the keyboard input into the variable num3. b. stores the keyboard input into the pointer called num3. c. stores the keyboard input into the variable pointed to by num3.

c. stores the keyboard input into the variable pointed to by num3.

4. When you work with a dereferenced pointer, you are actually working with: a. a variable whose memory has been deallocated 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. All of these

c. the actual value of the variable whose address is stored in the pointer variable

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.

c. the address of the first byte of storage.

When the less than ( < ) operator is used between two pointer variables, 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

c. the address of the first variable comes before the address of the second variable in

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 d. 2 b. 0 e. 1 c. 3

d. 2

What does the following statement do? double *num2; a. Declares a double variable named num2. b. Declares and initializes an pointer variable named num2. c. Initializes a variable named *num2. d. Declares a pointer variable named num2.

d. Declares a pointer variable named num2.

Which statement displays the address of the variable num1? a. cout << num1; b. cout << *num1; c. cin >> &num1; d. cout << &num1;

d. cout << &num1;

The statement int *ptr; has the same meaning as a. int ptr; b. *int ptr; c. int ptr*; d. int* ptr;

d. int* ptr;

Which of the following statements is not valid C++ code (assume num1 was declared as a float)? 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

e. All of these are invalid


Related study sets

art history exam 1 essay questions

View Set

Series 66 Chapter 6 Exam Questions

View Set

EXAM 2 49 Prep-U's Assessment and Management of Patients With Hepatic Disorders

View Set

EAQ [Safety and Infection Control]

View Set

Compensation Administration - Chapter 7

View Set

AMSCO: Chapter 15 Reconstruction 1863-1877

View Set

BLAW 243- PP 24 Employment Law- Exam 3

View Set

Quiz 3: The Reciprocal Functions and Identities

View Set

COTAC II exam 4- fluid & electrolytes and burns

View Set