C How To Program, 8th Edition By Deitel & deitel – Test Bank

$20.00

Category:

Description

INSTANT DOWNLOAD WITH ANSWERS

C How To Program, 8th Edition By Deitel & deitel – Test Bank

Chapter 6

6.1 Introduction

6.1 Arrays are data structures consisting of related data items of the same __________.

  1. a) sort order
  2. b) subscript
  3. c) type
  4. d) element

ANS: (c)

 

6.2 Arrays and structures are __________ entities in that they remain the same size throughout program execution.

  1. a) dynamic
  2. b) automatic
  3. c) register
  4. d) static

ANS: (d)

 

6.3 Lists, queues, stacks and trees are __________ data structures that may grow and shrink as programs execute.

  1. a) flexible
  2. b) automatic
  3. c) dynamic
  4. d) static

ANS: (c)

6.2 Arrays

6.4 An array is not ________.

(a) a consecutive group of memory locations

(b) indexed by integers

(c) pointer-based

(d) a dynamic entity

ANS: (d)

 

6.5 Which of the following is false?

(a) the first element of an array is the zeroth

(b) the last element of an array is the array size – 1

(c) the position number contained within square brackets is called a subscript

(d) a subscript cannot be an expression.

ANS: (d)

 

6.6 An array is a group of memory locations related by the fact that they all have __________ array name and __________ type.

  1. a) different, different
  2. b) same, different
  3. c) different same
  4. d) same, same

ANS: (d)

 

6.7 The first element in every array is the __________ element.

  1. a) null
  2. b) 1
  3. c) 0
  4. d) empty

ANS: (c)

 

6.8 Which statement is false?

  1. a) The brackets used to enclose the subscript of an array are not an operator in C.
  2. b) To refer to a particular element in an array, we specify the name of the array and the position number of the element in the array.
  3. c) The position number within an array is more formally called a subscript.
  4. d) “Array element seven” and the “seventh element of an array” do not mean the same thing. This is a frequent source of off-by-one errors.

ANS: (a)

6.3 Defining Arrays

6.9 Which statement would be used to define 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)

 

6.10 Which definition tells the computer to reserve 12 elements for integer array c?

  1. a) c[12] int;
  2. b) int c [11];
  3. c) c[11] int;
  4. d) int c[12];

ANS: (d)

6.4 Array Examples

6.11 Which of the following is not a correct way to initialize an 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] = {6, 6, 6};

ANS: (a)

 

6.12 Constant variables

(a) can be assigned values in executable statements

(b) do not have to be initialized when they are defined

(c) can be used to specify array sizes, thereby making programs more scalable

(d) can be used to specify array sizes, but this makes programs harder to understand

ANS: (c)

 

6.13 Referencing elements outside the array bounds

(a) can result in changes to the value of an unrelated variable

(b) is impossible because C checks to make sure it does not happen

(c) is a syntax error

(d) enlarges the size of the array

ANS: (a)

 

6.14 Strings can not

(a) be initialized using string literals

(b) end in a character other than the null character

(c) be initialized with initializer lists

(d) be treated as arrays of characters

ANS: (b)

 

6.15 Assume string1 is a character array. Which of the following operations does not produce a string?

(a) string1[] = “test”;

(b) string1[] = {‘t’, ‘e’, ‘s’, ‘t’, ”};

(c) string1[] = {‘t’, ‘e’, ‘s’, ‘t’};

(d) string1[] = ” “;

ANS: (c)

 

6.16 Suppose a program contains the code

for (i = 1; i < = 10; i++) {

n[i] = 0;
}

Which statement about this code must be true?

  1. a) It contains an off-by-one error.
  2. b) It contains a syntax error.
  3. c) It is initializing the first 10 elements of an array.
  4. d) It is initializing successive elements of an array.

ANS: (d)

 

6.17 What’s wrong with this code?

int[] = (1, 2, 3, 4, 5);

  1. a) The array size must be specified in the square brackets.
  2. b) The parentheses should be square brackets.
  3. c) The square brackets should be curly braces.
  4. d) The parentheses should be curly braces.

ANS: (d)

 

6.18 If there are fewer initializers than elements in the array, the remaining elements are __________.

  1. a) deleted
  2. b) ignored
  3. c) initialized to empty
  4. d) initialized to zero

ANS: (d)

 

6.19 Which statement is true?

  1. a) Arrays are automatically initialized to zero.
  2. b) To initialize all array elements to zeros, the array definition must explicitly initialize each element to zero.
  3. c) Array elements can be initialized to zero by the array definition only at compile time.
  4. d) Definitions for automatic arrays initialize the arrays at execution time.

ANS: (d)

 

6.20 The following array definition

int n[5] = {32, 27, 64, 18, 95, 14};

  1. a) is correct
  2. b) causes a syntax error because there are only five initializers and six array elements.
  3. c) causes a logic error because there are only five elements but there are six initializers.
  4. d) causes a syntax error because there are six initializers but only five array elements.

ANS: (d)

 

6.21 Which statement is true?

  1. a) A symbolic constant is an identifier that is replaced with replacement text by the C preprocessor after the program is compiled.
  2. b) Using symbolic constants to specify array sizes makes programs run faster.
  3. c) Preprocessor directives are not C statements.
  4. d) The #define preprocessor directive must end in a semicolon.

ANS: (c)

 

6.22 Which statement is true?

  1. a) Assigning a value to a symbolic constant in an executable statement is a syntax error.
  2. b) A symbolic constant is a variable.
  3. c) Space is reserved for both symbolic constants and variables that hold values at execution time.
  4. d) Only uppercase letters can be used for symbolic constant names.

ANS: (a)

 

6.23 Which statement is true regarding the statement

++frequency[responses];

  1. a) This statement increases the appropriate frequency counter depending on the value of responses.
  2. b) This statement increases the appropriate answer counter depending on the value of frequency[responses].
  3. c) This statement increases the appropriate responses counter depending on the value of frequency.
  4. d) This statement produces a syntax error because subscripts cannot be nested.

ANS: (a)

 

6.24 Which statement is false?

  1. a) C has no built-in bounds checking to prevent the computer from referring to an array element that does not exist.
  2. b) The programmer is responsible for implementing array bounds checking.
  3. c) Referring to an element outside the array bounds is a syntax error.
  4. d) Referring to an element outside the array bounds is a logic error.

ANS: (d)

 

6.5 Using Character Arrays to Store and Manipulate Strings

 

6.25 Which statement is false?

  1. a) The string termination character is called the null character.
  2. b) The string “string” actually occupies 7 characters in memory.
  3. c) The character representation of the last character in memory of a string is ‘’.
  4. d) A character array representing a string should be defined as large as the actual number of characters in the string minus the terminating character; C automatically provides the space for the terminating character.

ANS: (d)

 

6.26 The definition

char string1[] = “first”;

is equivalent to:

  1. a) character string1[] = {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ”};
  2. b) char string1 = {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ”};
  3. c) char string1[] = {‘f’, ‘i’, ‘r’, ‘s’, ‘t’};
  4. d) char string1[] = {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ”};

ANS: (d)

 

6.27 Which statement is false?

  1. a) Function scanf reads characters into memory from the keyboard until the first input whitespace character is encountered.
  2. b) Function scanf can write beyond the boundary of the array into which input is being placed.
  3. c) Function printf does not care how large the array it is printing is.
  4. d) When using scanf, you must always precede with the & operator the name of each variable into which inputs are being placed.

ANS: (d)

 

6.6 Static Local Arrays and Automatic Local Arrays

6.28 Which statement is false?

  1. a) A static local variable exists for the duration of the program.
  2. b) A static local variable is visible only in the control structure in which it is defined.
  3. c) A static local array is not created and destroyed each time the function is entered and exited, respectively.
  4. d) Arrays that are defined static are automatically initialized once at compile time.

ANS: (b)

6.7 Passing Arrays to Functions

6.29 Unless otherwise specified, entire arrays are passed __________ and individual array elements are passed __________.

(a) call-by-value, call-by-reference

(b) call-by-reference, call-by-value

(c) call-by-value, call-by-value

(d) call-by-reference, call-by-reference

ANS: (b)

 

6.30 Which of the following is false about a function being passed an array?

(a) it knows the size of the array it was passed

(b) it is passed the address of the first element in the array

(c) it is able to modify the values stored in the array

(d) all of the above are true

ANS: (a)

 

6.31 To prevent modification of array values in a function,

(a) the array must be defined static in the function.

(b) the array parameter can be preceded by the const qualifier.

(c) a copy of the array must be made inside the function.

(d) the array must be passed call-by-reference.

ANS: (b)

 

6.32 To pass an array to a function, specify

  1. a) the name of the array without any brackets.
  2. b) the name of the array preceded by an empty pair of brackets.
  3. c) the name of the array followed by a pair of brackets including the size of the array.
  4. d) the name of the array followed by a pair of brackets including a number one less than the size of the array.

ANS: (a)

 

6.33 Which statement is false?

  1. a) C automatically passes arrays to functions using simulated call by reference.
  2. b) When C passes an array to a function, the called function normally can modify the element values in the caller’s original array.
  3. c) The name of an array is actually the address of the first element of the array.
  4. d) When an array is passed in a function call, the calling function knows precisely where in the called function’s memory the passed array is.

ANS: (d)

 

6.34 The special conversion specifier for printing addresses is __________.

  1. a) %a
  2. b) %m
  3. c) %p
  4. d) %loc

ANS: (c)

 

6.35 Which statement is true?

  1. a) Entire arrays are passed simulated call by reference and individual array elements are normally passed simulated call by reference.
  2. b) Entire arrays are passed simulated call by reference and individual array elements are normally passed call by value.
  3. c) Entire arrays are passed call by value and individual array elements are normally passed simulated call by reference.
  4. d) Entire arrays are passed call by value and individual array elements are normally passed call by value.

ANS: (b)

6.8 Sorting Arrays

6.36 Which statement about bubble sort is true?

(a) a maximum of n passes are needed to sort the array, where n is the number of elements

(b) swapping values requires two assignments

(c) performance is maximized

(d) the algorithm is very simple compared to other sorting procedures

ANS: (d)

 

 

6.37 Which statement about the bubble sort is false?

  1. a) It is easy to program.
  2. b) It is a high-performance sort.
  3. c) It compares only adjacent elements with one another.
  4. d) The bubble sort compares successive pairs of elements.

ANS: (b)

 

6.38 A bubble sort of 1000 elements requires a maximum of __________ passes.

  1. a) 1001
  2. b) 1000
  3. c) 999
  4. d) 998

ANS: (c)

6.9 Case Study: Computing Mean, Median and Mode Using Arrays

6.39 In order to calculate the __________ of an array of values, the array must be sorted.

(a) median

(b) mode

(c) mean

(d) (a), (b), and (c)

ANS: (a)

 

6.40 The _____ is the average value of a set of data items.

  1. a) mean
  2. b) median
  3. c) mode
  4. d) matrix

ANS: (a)

 

6.41 Calculating which of the following normally requires the data to be sorted first

  1. a) mean
  2. b) median
  3. c) mode
  4. d) total

ANS: (b)

 

6.42 The __________ is the value that occurs most frequently in the data.

  1. a) mean
  2. b) median
  3. c) mode
  4. d) master

ANS: (c)

6.10 Searching Arrays

 

6.43 The binary search technique

(a) is better suited to small arrays

(b) is better suited to unsorted arrays

(c) can only be used on a sorted array

(d) is slower than a linear search

ANS: (c)

 

6.44 The maximum number of comparisons needed for the binary search of a 2000 element array is

(a) 9

(b) 15

(c) 11

(d) 14

ANS: (c)

 

6.45 Which of these is generally thought of as a high-performance technique?

  1. a) bubble sort
  2. b) linear search
  3. c) binary search
  4. d) iteration

ANS: (c)

 

6.46 Which of the following statements is false?

  1. a) The linear searching method works well for small arrays.
  2. b) The linear searching method works well for unsorted arrays.
  3. c) The binary search algorithm eliminates from consideration one half of the elements in a sorted array after each comparison.
  4. d) The binary search terminates only when the search key is equal to the middle element of a subarray.

ANS: (d)

 

6.47 A sorted array of a million elements can be searched by a binary search in __________ or fewer comparisons.

  1. a) 10
  2. b) 20
  3. c) 30
  4. d) 999,999

ANS: (b)

6.11 Multidimensional Arrays

6.48 A two-dimensional array element incorrectly referenced as a[x, y] is actually evaluated as

(a) a[x][y]

(b) a[y]

(c) a[x]

(d) a[0]

ANS: (b)

 

6.49 Given the following definitions, what is the value of b[1][0]?

int b[2][2] = {{1}, {3, 4}};

(a) 0

(b) 1

(c) 3

(d) this isn’t a valid definition

ANS: (c)

 

6.50 Which of the following does not initialize all of the array elements to 0?

(a)
int b[2][2];

b[0][0] = b[0][1] = b[1][0] = b[1][1] = 0;

(b) int b[2][2] = {0};

(c)
int b[2][2];

for (int i = 0; i < 2; ++i) {

for (int j = 0; j < 2; ++j) {

      b[i][j] = 0;
}
}

(d) all of the above initialize all of their elements to 0.

ANS: (d)

 

6.51 Which statement is false about multidimensional arrays?

  1. a) C supports multidimensional arrays.
  2. b) A common use of multidimensional arrays is to arrange data into tables consisting of rows and columns.
  3. c) To identify a particular table element requires two indices.
  4. d) Every ANSI C system can support arrays with as many indices as the programmer chooses.

ANS: (d)

 

6.52 An array containing 3 columns and 4 rows is typically referred to as a __________.

  1. a) 12-element array
  2. b) 3-by-4 array
  3. c) 13-element array, because of the zero element
  4. d) 4-by-3 array

ANS: (d)

 

6.53 Which initialization is not performed by the following definition?

 

int b[2][2] = {{1}, {3, 4}};

 

a)b[0][0] is set to 1

  1. b) b[0][1] is set to 1
  2. c) b[1][0] is set to 3
  3. d) b[1][1] is set to 4

ANS: (b)

6.12 Variable-Length Arrays

No questions.

6.13 Secure C Programming

6.54 Which of the following statements is false?

(a) C provides automatic bounds checking for arrays.

(b) C provides no automatic bounds checking for arrays, so you must provide your own.

(c) Allowing programs to read from or write to array elements outside the bounds of arrays are common security flaws.

(d) Writing to an out-of-bounds element (known as a buffer overflow) can corrupt a program’s data in memory, crash a program and allow attackers to exploit the system and execute their own code.

ANS: (a)

 

6.55 Assuming myString is a 20-element char array, which of the following statements might result in buffer overflow?

(a) scanf(“%19s”, myString);

(b) scanf_s(“%s”, myString, 20);

(c) scanf_s(“%19s”, myString, 20);

(d) scanf(“%s”, myString);

ANS: (d)

 

Chapter 7

7.1 Introduction

7.2 Pointer Variable Definitions and Initialization

7.1 Pointers cannot be used to

(a) find the address of a variable in memory.

(b) reference values directly.

(c) simulate call-by-reference.

(d) manipulate dynamic data structures.

ANS: (b)

 

7.2 Pointers may be assigned which of the following?

(a) all integer values

(b) an address

(c) NULL

(d) both (b) and (c)

ANS: (d)

 

7.3 Pointers are variables that contain __________ as their values.

  1. a) strings
  2. b) flowlines
  3. c) memory addresses
  4. d) directions

ANS: (c)

 

7.4 A non-pointer variable name __________ references a value and a pointer variable name __________ references a value.

  1. a) directly, directly
  2. b) directly, indirectly
  3. c) indirectly, directly
  4. d) indirectly, indirectly

ANS: (b)

 

7.5 The definition

int *count;

  1. a) is a syntax error because only pointers can be defined with *
  2. b) is a compile-time error.
  3. c) is a logic error.
  4. d) is a correct definition of integer pointer count.

ANS: (d)

 

7.6 Referencing a value through a pointer is called __________.

  1. a) interdiction
  2. b) indirection
  3. c) intermediation
  4. d) redirection

ANS: (b)

 

7.7 Which statement about pointers is false?

  1. a) They can be defined to point to objects of any data type.
  2. b) The indirection operator * distributes to all comma-separated variable names in a definition.
  3. c) The letters Ptr in a pointer variable name are optional.
  4. d) A pointer may be initialized to 0, NULL or an address.

ANS: (b)

 

7.8 Which statement about pointers is false?

  1. a) A pointer with the value NULL points to nothing.
  2. b) NULL is a symbolic constant defined in the <stdio.h> header file.
  3. c) Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred.
  4. d) The values 0 and 1 are the only values that can be assigned directly to a pointer variable.

ANS: (d)

7.3 Pointer Operators

7.9 The & operator can be applied to

(a) constants

(b) variables defined with the storage class register

(c) variables defined with the storage class static

(d) rvalues

ANS: (c)

 

7.10 Three of the following expressions have the same value. Which of the following’s value is different from the others?

(a) *&Ptr

(b) &*Ptr

(c) *Ptr

(d) Ptr

ANS: (c)

 

7.11 The __________, or address operator, is a unary operator that returns the address of its operand.

  1. a) &
  2. b) &&
  3. c) *
  4. d) **

ANS: (a)

 

7.12 The statement

y = &yPtr;

  1. a) assigns the address of the variable y to pointer variable yPtr.
  2. b) assigns the address of the variable yPtr to pointer variable y.
  3. c) is a compilation error.
  4. d) is a logic error.

ANS: (b)

 

7.13 Which statement is false?

  1. a) The operand of the address operator must be a variable.
  2. b) The address operator cannot be applied to constants or to expressions.
  3. c) The address operator can be applied to variables defined with the storage class register.
  4. d) The address operator can be applied to variables defined with the storage class static.

ANS: (c)

 

7.14 Which statement is false?

  1. a) The unary * operator is called the indirection operator or the dereferencing operator.
  2. b) The operand of the unary * operator must be a pointer.
  3. c) The unary * operator returns the value of its operand.
  4. d) Placing a * operator before a pointer dereferences the pointer.

ANS: (c)

 

7.15 Which statement is generally false?

  1. a) Initializing pointers is optional.
  2. b) Dereferencing an uninitialized pointer could lead to a fatal execution time error.
  3. c) Deferencing an uninitialized pointer could accidentally modify important data.
  4. d) Derefencing an uninitialized pointer causes a syntax error.

ANS: (d)

 

7.16 The unary * and __________ are complements of one another.

  1. a) /
  2. b) ^
  3. c) &
  4. d) |

ANS: (c)

7.4 Passing Arguments to Functions by Reference

7.17 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) int * const a

ANS: (c)

 

7.18 Which statement is false?

  1. a) All function calls in C pass arguments call-by-value.
  2. b) Call-by-reference enables a called function to modify variables in the calling function.
  3. c) Call-by-value is always more efficient than call-by-reference.
  4. d) In C, programmers use pointers and the indirection operator to simulate call-by-reference.

ANS: (c)

 

7.19 When calling a function with arguments that should be modified, the __________ of those arguments are passed.

  1. a) memory
  2. b) addresses
  3. c) values
  4. d) complements

ANS: (b)

 

7.20 If array name arrayName is passed to a function, C automatically passes __________.

  1. a) &arrayName[0]
  2. b) arrayName[1]
  3. c) arrayName[0]
  4. d) *arrayName

ANS: (a)

 

7.21 When the __________ of a variable is passed to a function, the indirection operator (*) may be used in the function to modify the __________ at that location in the caller’s memory.

  1. a) address, address
  2. b) value, address
  3. c) value, value
  4. d) address, value

ANS: (d)

 

7.22 Which statement is false?

  1. a) It’s necessary to include names of pointer arguments in function prototypes.
  2. b) A function receiving an address as an argument must define a pointer parameter to receive the address.
  3. c) The compiler does not distinguish between a function that receives a pointer and a function that receives a single-subscripted array.
  4. d) The function must “know” whether it is receiving a single-subscripted array or simply a single variable for which it is to perform simulated call by reference.

ANS: (a)

7.5 Using the const Qualifier with Pointers

7.23 A function that modifies an array by using pointer arithmetic to process every value 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)

 

7.24 A function that prints a string should have a parameter that’s a

(a) nonconstant pointer to nonconstant data.

(b) nonconstant pointer to constant data.

(c) constant pointer to nonconstant data.

(d) constant pointer to constant data.

ANS: (b)

 

7.25 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 constant pointer to constant data.

(b) A constant pointer to nonconstant data.

(c) A nonconstant pointer to constant data.

(d) A nonconstant pointer to constant data.

ANS: (a)

 

 

7.26. Which statement is false?

  1. a) The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified.
  2. b) The const qualifier did not exist in early versions of C; it was added to the language by the ANSI C committee.
  3. c) Using the const qualifier typically violates the principle of least privilege
  4. d) Adding const to C programs that do not use it presents significant opportunities for improvement in the software engineering of that code.

ANS: (c)

 

7.27 Which of the following most closely reflects the principle of least privilege.

  1. a) Always grant a function at least enough access to the data in its parameters to accomplish its specified task.
  2. b) Always grant a function enough access to the data in its parameters to accomplish its specified task, but no more.
  3. c) Always grant a function the least amount of access to the data in its parameters.
  4. d) Never grant a function access to the data in its parameters.

ANS: (b)

 

7.28 Which statement is false?

  1. a) If a value should not change in the body of a function to which it is passed, the value should be defined const to ensure that it is not accidentally modified.
  2. b) Attempts to modify the value of a variable defined const are caught at execution time.
  3. c) One way to pass a pointer to a function is to use a non-constant pointer to non-constant data.
  4. d) It is dangerous to pass a non-pointer into a pointer argument.

ANS: (b)

 

7.29 The highest level of data access is granted by a

  1. a) non-constant pointer to non-constant data.
  2. b) non-constant pointer to constant data.
  3. c) constant pointer to non-constant data.
  4. d) constant pointer to constant data.

ANS: (a)

 

7.30 Which statement is false?

  1. a) Arrays and structures are aggregate data types.
  2. b) Structures in C are sometimes called records in other languages.
  3. c) A structure is capable of storing many related data items of different data types.
  4. d) Structures are always passed call by reference.

ANS: (d)

 

7.31 When a structure must be passed to a function, we can use pointers to constant data to get the performance of a call by __________ and the protection of a call by __________.

  1. a) value, value
  2. b) value, reference
  3. c) reference, value
  4. d) reference, reference

ANS: (c)

 

 

7.32 The least access privilege is granted by a __________ pointer to __________ data.

  1. a) non-constant, non-constant
  2. b) non-constant, constant
  3. c) constant, non-constant
  4. d) constant, constant

ANS: (d)

7.6 Bubble Sort Using Pass-by-Reference

7.33 Which statement is false?

  1. a) The notations int *array and int array[] are interchangeable.
  2. b) Function prototypes may not be placed inside functions.
  3. c) Good advice: To encourage software reusability, when passing an array, also pass the size of the array.
  4. d) Global variable violate the principle of least privilege.

ANS: (b)

7.7 sizeof Operator

7.34 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 an array

ANS: (d)

 

7.35 Which of the following gives the number of elements in the array int r[]?

(a) sizeof (r)

(b) sizeof (*r)

(c) sizeof (r) / sizeof (int)

(d) sizeof (*r) / sizeof (int)

ANS: (c)

 

7.36 Which statement is false?

  1. a) Using global variables, instead of passing arguments, can increase program performance, but reduce effective software engineering.
  2. b) Hardcoding into a function the size of an array that will be passed as an argument rather than passing the size of the array as a parameter, reduces the software reusability of that function.
  3. c) sizeof is an operator that calculates the size of its argument in bytes at compile time.
  4. d) When applied to the name of an array, sizeof returns the number of elements in the array.

ANS: (d)

 

7.37 An expression such as

sizeof(arrayName) / sizeof(double)

might typically be used to determine

  1. a) the size of an array
  2. b) the number of elements in an array
  3. c) the number of elements in half an array
  4. d) the size of an element of an array

ANS: (b)

7.8 Pointer Expressions and Pointer Arithmetic

7.38 Which of the following can have a pointer as an operand?

(a) ++

(b) *=

(c) %

(d) /

ANS: (a)

 

7.39 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)

 

7.40 A pointer cannot be assigned to

(a) another pointer of the same type

(b) a pointer to void

(c) a pointer of a type other than its own type and void

(d) any other pointer by using the cast operator

ANS: (c)

 

7.41 Comparing pointers and performing arithmetic on them is meaningless unless

(a) they point to members of the same array

(b) they point to arrays of the same type

(c) they point to arrays of equal size

(d) they point to different locations

ANS: (a)

 

 

7.42 Which statement is false?

  1. a) A pointer may be incremented or decremented.
  2. b) An integer may be added to a pointer.
  3. c) All operators normally used in arithmetic expressions, assignment expressions and comparison expressions are valid in conjunction with pointer variables.
  4. d) A pointer may not be added to a pointer.

ANS: (c)

 

7.43 Which statement is false?

  1. a) A pointer can always be assigned to another pointer of the same type.
  2. b) A cast operator must always be used to convert the pointer on the right of an assignment to the pointer type on the left of the assignment.
  3. c) Variables of all pointer types can be assigned a pointer to void.
  4. d) A pointer to void can be assigned a pointer of any type.

ANS: (b)

7.9 Relationship Between Pointers and Arrays

7.44 Assuming that t is an array and tPtr is a pointer to that array, what expression refers to the address of element 3?

(a) *(tPtr + 3)

(b) tPtr[3]

(c) &t[3]

(d) *(t + 3)

ANS: (c)

 

7.45 Consider the following function:

void reverse(char * string1, const char * string2)

{

int stringsize = sizeof(string1)/sizeof(char);

 

*(string1 + stringsize – 1) = ”;

string1 = string1 + stringsize – 2;

 

for (; *string2 != ”; –string1, ++string2) {

*string1 = *string2;
}

}

What method does the function use to refer to array elements?

(a) array/subscript notation

(b) array/offset notation

(c) pointer/subscript notation

(d) pointer/offset notation

ANS: (d)

 

7.46 If bPtr is assigned b (the name of an array), then array element b[3] can alternatively be referenced with the pointer expression __________.

  1. a) bPtr + 3
  2. b) b[bPtr + 3]
  3. c) *b [bPtr + 3]
  4. d) *(bPtr + 3)

ANS: (d)

7.10 Arrays of Pointers

7.47 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)

 

7.48 Which statement is false?

  1. a) In C, a string is essentially a pointer to its first character.
  2. b) Arrays may contain pointers.
  3. c) Each entry in an array of strings is actually a pointer to the first character of a string.
  4. d) The size of an array of strings is the sum of the lengths of the strings.

ANS: (d)

7.11 Case Study: Card Shuffling and Dealing Simulation

7.49 An algorithm that could execute for an unknown amount of time because it depends on random numbers to exit a function may

(a) have a redundancy.

(b) get caught in an infinite loop.

(c) suffer from indefinite postponement.

(d) issue a compiler error.

ANS: (c)

 

7.50 Which statement about the algorithm we presented in Section 7.10 of the text for shuffling a deck of cards is true?

  1. a) It’s guaranteed to terminate.
  2. b) It could execute indefinitely.
  3. c) It’s efficient.
  4. d) It uses a one-dimensional array of 52 cards.

ANS: (b)

7.12 Pointers to Functions

7.51 Which of the following is false with regard to pointers to functions?

(a) They contain the starting address of the function code.

(b) They’re dereferenced in order to call the function.

(c) They can be stored in arrays.

(d) They cannot be assigned to other function pointers

ANS: (d)

 

7.52 (*max)(num1, num2, num3);

(a) is the header for function max

(b) is a call to function max

(c) is the prototype for function max

(d) is part of a definition of a pointer to the function max

ANS: (b)

 

7.53 A pointer to a function contains __________.

  1. a) the address of the function’s automatic variables
  2. b) the address of the function on the stack
  3. c) the address of the entry for that function in the System Function Table
  4. d) the address of the function in memory

ANS: (d)

 

7.54 Which statement about the parameter definition

int (*compare)(int, int)

is false?

 

  1. a) It defines a parameter that is a pointer to a function that receives two integer arguments and returns a pointer to an integer as a result.
  2. b) Parentheses are needed around *compare because * has a lower precedence than the parentheses enclosing the function parameters.
  3. c) Without the parentheses it would have defined a function that receives two integers and returns a pointer to an integer.
  4. d) The corresponding parameter in the function prototype would ordinarily be

int (*)(int, int)

ANS: (a)

7.13 Secure C Programming

7.55 Which of the following statements is false?

(a) A key feature of functions like printf_s and scanf_s that makes them more secure is that they have runtime pointer constraints that are checked after attempting to use the pointers.

(b) In a scanf_s, if any of the pointer arguments (including the format-control string) are NULL, the function returns EOF.

(c) In a printf_s, if the format-control string or any argument that corresponds to a %s is NULL, the function stops outputting data and returns a negative number.

(d) None of the above.

ANS: (a)

 

7.56 Which of the following statements is false?

(a) Dereferencing NULL pointers typically causes programs to crash.

(b) If a function parameter points to a value that will not be changed by the function, const should be used to indicate that the data is constant.

(c) Attackers cannot execute code by dereferencing a NULL pointer.

(d) None of the above.

ANS: (c)