chapter 8

Ace your homework & exams now with Quizwiz!

8.12 Q2: (*max)(num1, num2, num3);: a. Is the header for function max. b. Is a call to the function pointed to by max. c. Is the prototype for function max. d. Is a declaration of a pointer to a function called max.

ANS: b. Is a call to the function pointed to by max.

8.3 Q3: Three of the following expressions have the same value. Which of the following expressions has a value different from the others'? a. *&ptr b. &*ptr c. *ptr d. ptr

ANS c. *ptr

8.4 Q2: When a compiler encounters a function parameter for a single-subscripted array of the form int a[], it converts the parameter to: a. int a b. int &a c. int *a d. No conversion is necessary.

ANS c. int * a

8.3 Q1: The & operator can be applied to: a. constants. b. string literals. c. lvalues. d. rvalues.

ANS c. lvalues.

8.2 Q2: Pointers may be assigned which of the following values? a. Any integer values. b. An address. c. nullptr. d. Both (b) and (c).

ANS d. Both (b) and (c).

8.3 Q2: All of the following can cause a fatal execution-time error except: a. Dereferencing a pointer that has not been assigned to point to a specific address. b. Dereferencing a pointer that has not been initialized properly. c. Dereferencing a null pointer. d. Dereferencing a variable that is not a pointer.

ANS d. Dereferencing a variable that is not a pointer. (This is a compilation error.)

8.5 Q2: Which statement would be used to declare a 10-element integer array c? a. array c = int[10]; b. c = int[10]; c. int array c[10]; d. int c[10];

ANS d. int c[10];

8.8 Q1: Which of the following can have a pointer as an operand? a. ++ b. *= c. % d . /

ANS: a. ++

8.6 Q1: A function that modifies an array by using pointer arithmetic such as ++ptr to process every value of the array should have a parameter that is: a. A nonconstant pointer to nonconstant data. b. A nonconstant pointer to constant data. c. A constant pointer to nonconstant data. d. A constant pointer to constant data.

ANS: a. A nonconstant pointer to nonconstant data.

8.11 Q2: A string array is commonly used for: a. Command-line arguments. b. Storing an extremely long string. c. Storing multiple copies of the same string. d. Displaying floating-point numbers to the screen.

ANS: a. Command-line arguments.

8.5 Q4: Which of the following is false about a function to which a built-in array is being passed? a. It always knows the size of the built-in array that is being passed. b. It is being passed the address of the first element in the built-in array. c. It is able to modify the values stored in the built-in array. d. The built-in array's name is used as an argument in the function call.

ANS: a. It always knows the size of the built-in array that is being passed.

8.8 Q4: Comparing pointers and performing pointer arithmetic on them is meaningless unless: a. They point to elements of the same array. b. You are trying to compare and perform pointer arithmetic on the values to which they point. c. They point to arrays of equal size. d. They point to arrays of the same type.

ANS: a. They point to elements of the same array.

8.5 Q1: Which of the following is not a correct way to initialize a built-in array? a. int n[5]{0, 7, 0, 3, 8, 2}; b. int n[]{0, 7, 0, 3, 8, 2}; c. int n[5]{7}; d. int n[5]{9, 1, 9};

ANS: a. int n[5]{0, 7, 0, 3, 8, 2};

8.6 Q2: A function that prints a string by using pointer arithmetic such as ++ptr to output each character should have a parameter that is: a. A nonconstant pointer to nonconstant data. b. A nonconstant pointer to constant data. c. A constant pointer to nonconstant data. d. A constant pointer to constant data.

ANS: b. A nonconstant pointer to constant data.

8.2 Q3: What does the following statement declare? int *countPtr, count; a. Two int variables. b. One pointer to an int and one int variable. c. Two pointers to ints. d. The declaration is invalid.

ANS: b. One pointer to an int and one int variable.

8.2 Q1: Pointers cannot be used to: a. Contain memory addresses. b. Reference values directly. c. Pass an argument by reference. d. Manipulate dynamic data structures.

ANS: b. Reference values directly.

8.5 Q3: To prevent modification of a built-in array's values when you pass the built-in array to a function: a. The built-in array must be declared static in the function. b. The built-in array parameter can be preceded by the const qualifier. c. A copy of the built-in array must be made inside the function. d. The built-in array must be passed by reference.

ANS: b. The built-in array parameter can be preceded by the const qualifier.

8.10.1 Q2: cin.getline(superstring, 30); is equivalent to which of the following? a. cin.getline(superstring, 30, '\0'); b. cin.getline(superstring, 30, '\n'); c. cin.getline(superstring, 30, '\s'); d. cin.getline(superstring, 30, '\t');

ANS: b. cin.getline(superstring, 30, '\n');

8.7 Q2: Which of the following gives the number of elements in the array int r[10]? a. sizeof r b. sizeof (*r) c. sizeof r / sizeof (int) d. sizeof (*r) / sizeof (int)

ANS: c sizeof r / sizeof (int)

8.9 Q1: Assuming that t is an array and tPtr is a pointer to that array, which expression refers to the address of element 3 of the array? a. *(tPtr + 3) b. tPtr[3] c. &t[3] d. *(t + 3)

ANS: c. &t[3]

8.8 Q2: Given that k is an integer array starting at location 2000, kPtr is a pointer to k and each integer is stored in 4 bytes of memory, what location does kPtr + 3 point to? a. 2003 b. 2006 c. 2012 d. 2024

ANS: c. 2012

8.8 Q3: A pointer can not be assigned to: a. Another pointer of the same type without using the cast operator. b. A pointer to void without using the cast operator. c. A pointer of a type other than its own type and void without using the cast operator. d. Any other pointer by using the cast operator.

ANS: c. A pointer of a type other than its own type and void without using the cast operator.

8.11 Q1: A string array: a. Stores an actual string in each of its elements. b. Can only provide access to strings of a certain length. c. Is actually an array of pointers. d. Is always less memory efficient than an equivalent double-subscripted array.

ANS: c. Is actually an array of pointers.

8.10.1 Q1: Which of the following is false for pointer-based strings? a. A string may include letters, digits and various special characters (i.e., +, -, *). b. A string in C++ is an array of characters ending in the null character ('\0'). c. String literals are written inside of single quotes. d. A string may be assigned in a declaration to either a character array or a variable of type char *.

ANS: c. String literals are written inside of single quotes.

8.6 Q3: Which of the following best describes the array name n in the declaration int n[10];? a. n is a nonconstant pointer to nonconstant data. b. n is a nonconstant pointer to constant data. c. n is a constant pointer to nonconstant data. d. n is a constant pointer to constant data.

ANS: c. n is a constant pointer to nonconstant data.

8.5 Q5[C++11]: Given a built-in array of ints named values, which of the following statements would sort the array? a. sort(values.begin(), values.end()); b. sort(values.array_begin(), values.array_end()); c. sort(begin(values), end(values)); d. sort(array_begin(values), array_end(values));

ANS: c. sort(begin(values), end(values));

8.6 Q4: What method should be used to pass an array to a function that does not modify the array and only looks at it using array subscript notation: a. A nonconstant pointer to nonconstant data. b. A nonconstant pointer to constant data. c. A constant pointer to nonconstant data. d. A constant pointer to constant data.

ANS: d. A constant pointer to constant data.

8.4 Q1: Which of the following is not a valid way to pass arguments to a function in C++? a. By reference with reference arguments. b. By value. c. By reference with pointer arguments. d. By value with pointer arguments.

ANS: d. By value with pointer arguments.

8.2 Q4 [C++11]: Which of the following statements about pointer initialization and values is false? a. Prior C++11, the value specified for a null pointer was 0 or NULL. b. When 0 is assigned to a pointer, it's converted to a pointer of the appropriate type. c. The value 0 is the only integer value that can be assigned directly to a pointer variable without first casting the integer to a pointer type. d. In the new standard, you should use the constant null_ptr to initialize a pointer instead of 0 or NULL.

ANS: d. In the new standard, you should use the constant null_ptr to initialize a pointer instead of 0 or NULL. [The constant is actually nullptr (without the underscore).]

8.9 Q2: Consider the following function: void reverse(char *string1, const char *string2) { int stringsize{sizeof(string1)/sizeof(char)}; *(string1 + stringsize - 1) = '\0'; string1 = string1 + stringsize - 2; for (; *string2 != '\0'; string1--, string2++) { *string1 = *string2; } } What technique does the function use to refer to array elements? a. Array subscript notation. b. Pointer/offset notation where the pointer is actually the name of the array. c. Pointer subscript notation. d. Pointer/offset notation.

ANS: d. Pointer/offset notation.

8.7 Q1: sizeof: a. Is a binary operator. b. Returns the total number of elements in an array. c. Usually returns a double. d. Returns the total number of bytes in a variable.

ANS: d. Returns the total number of bytes in a variable.

8.12 Q1: Which of the following is not true of pointers to functions? a. They contain the starting address of the function code. b. They are dereferenced in order to call the function. c. They can be stored in arrays. d. They can not be assigned to other function pointers.

ANS: d. They can not be assigned to other function pointers.


Related study sets

AH1 PrepU - Chapter 46: Gastric & Duodenal Disorders

View Set

The Family: 1: The distinction between households and families, and between types of families: lone parent, nuclear and extended.

View Set

One Flew Over the Cuckoo's Nest Characters:

View Set

EMT Chapter 24 Gynecologic Emergencies

View Set

(17)Chapter 18: Epigenetics, Linkage, and Extranuclear Inheritance

View Set

Chapter 4 Statement of Cash Flows

View Set