C++ pointers 2
What does the following declaration declare? int *countPtr, count;
One pointer to an int and one int variable.
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 method does the function use to refer to array elements?
Pointer/offset notation.
Pointers cannot be used to:
Reference values directly.
Which of the following is not true? A string in C++ is an array of characters ending in the null character ('\0'). String literals are written inside of single quotes. A string may include letters, digits and various special characters (i.e., +, -, * ). A string may be assigned in a declaration to either a character array or a variable of type char *.
String literals are written inside of single quotes.
Assuming that string1 = "hello" and string2 = "hello world", which of the following returns 0? strcmp( string1, string2, 6 );. strncmp( string1, string2, 6 );. Strncmp( string1, string2, 5 );. strcmp( string1, string2 );.
Strncmp( string1, string2, 5 );.
Which of the following is not true of pointers to functions? They can be stored in arrays. They are dereferenced in order to call the function. They can not be assigned to other function pointers. They contain the starting address of the function code.
They can not be assigned to other function pointers.
Comparing pointers and performing pointer arithmetic on them is meaningless unless:
They point to elements of the same array.
cin.getline( superstring, 30 ); is equivalent to:
cin.getline( superstring, 30, '\n' );.
A string array is commonly used for:
command line arguments
When a compiler encounters a function parameter for a single-subscripted array of the form int a[], it converts the parameter to:
int * a
The & operator can be applied to:
lvalues
Which of the following gives the number of elements in the int array r[ ]? sizeof r. sizeof ( ~r ). sizeof ( ~r ) / sizeof ( int ). sizeof r / sizeof ( int ).
sizeof r / sizeof ( int ).
Which of the following correctly copies the contents of string2 into string1? Assume that string2 is equal to "goodbye" and string1 is equal to "good morning"? strncpy( string1, string2, 6 );. strcpy( string1, string2, 6 );. Strncpy( string1, string2, 5 );. strcpy( string1, string2 );.
strcpy( string1, string2 );.
( *max )( num1, num2, num3 );: Is a declaration of a pointer to a function called max. Is the header for function max. Is the prototype for function max. Is a call to the function pointed to by max.
Is a call to the function pointed to by max.
Assuming that t is an array and tPtr is a pointer to that array, which expression refers to the address of the fourth element? *( t + 3 ). tPtr[ 3 ]. *( tPtr + 3 ). &t[ 3 ].
&t[3]
Three of the following expressions have the same value. Which of the following expressions has a value different from the others'? ___ *&Ptr. ___**Ptr. ___ &*Ptr. ___ Ptr.
*Ptr
Which of the following can have a pointer as an operand? /. %. *=. ++.
++
A string array: Is actually an array of pointers. Can only provide access to strings of a certain length. Is always less memory efficient than an equivalent double-subscripted array. Stores an actual string in each of its elements.
Is actually an array of pointers
Pointers may be assigned to:
An address. NULL.
Not a valid way to pass arguments to a function in C++? By reference with reference arguments. By value. By value with pointer arguments. By reference with pointer arguments.
By value with pointer arguments.
All of the following could cause a fatal execution-time error except: Dereferencing a variable that is not a pointer. Dereferencing a pointer that has not been initialized properly. Dereferencing a pointer that has not been assigned to point to a specific address. Dereferencing a null pointer.
Dereferencing a variable that is not a pointer.
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?
2012
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 constant pointer to constant data.
An array name is:
A constant pointer to nonconstant data.
To follow the principle of least privilege, the selectionSort function should receive the array to be sorted as:
A constant pointer to nonconstant data.
A function that modifies an array by using pointer arithmetic such as ++ptr to process every value should have a parameter that is:
A nonconstant pointer to nonconstant data.
A pointer can not be assigned to:
A pointer of a type other than its own type and void without using the cast operator.
