Chapter 5 & 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

5.

1 Intro d u c tio n

5.2 Pro gr a m M o d ul e s in C

5.1 All of the following are true of functions except:


(a) they define specific tasks that can be used at many points in a program
(b) a function call must specify the name and arguments of the function
(c) the definition of a function is always visible to other functions
(d) the implementation of a function is hidden from the caller
ANS: (c)

5.2. Experience has shown that the best way to construct a program is from
small pieces. This is called __________.
a) bottom up
b) the whole is greater than the sum of the parts
c) divide and conquer
d) recursion
ANS: (c)

5.3. Which is not an ANSI standard library function


a) printf
b) main
c) scanf
d) pow
ANS: (b)

5.4. Which one item is most different from the other three?
a) worker function
b) caller
c) calling function
d) boss function
ANS: (a)

5.5. When a called function completes its task, it normally


a) terminates program execution normally
b) aborts program execution
c) logs its results
d) returns to the calling function
ANS: (d)

5.6. Which statement is true?


a) The boss function normally knows how the worker function performs its
designated tasks.
b) A worker function may not call other worker functions.
c) “Hiding” of implementation details makes it difficult to understand
software.
d) The boss function is normally unaware when a worker function calls a
function.
ANS: (d)

5.7. Functions are __________ by a function call.


a) inveigled ,
b) invoked *A
c) internalized ITE

d) inverted #(77
ANS: (b)

5.3 M a th Li br a ry Fu n c tio ns

5.8 All math library functions return the data type


(a) long double
(b) int
(c) float
(d) double
ANS: (d)

5.9. All functions in the math library return the data type __________.
a) float
b) int
c) long
d) double
ANS: (d)

5.10. What happens when you do not include math.h when using functions in
the math library?
a) compilation error
b) execution error
c) logic error
d) strange results may occur
ANS: (d)

5.11. If a = 7.0, b = 7.0 and c = 6.0, then what is printed by


printf( “%.2f”, sqrt( a + b * c ) );
a) 49
b) 7.00
c) 7
d) 49.00
ANS: (b)

5.12. What is the value of fabs( -5.0 )?


a) 5 I double
b) 5.0 output
c) -5
d) –5.0
ANS: (b)

5.4 Fu n c tio ns

5.13 Which of the following is not included in <math.h>?


(a) pow
(b) floor
(c) ln
(d) log10
ANS: (c)

5.14 A valid reason for building programs out of functions is


(a) that the divide-and-conquer approach facilitates program construction
(b) that pre-existing functions can be used to create new programs
(c) the avoidance of code repetition within a program
(d) all of the above
ANS: (d)
5.15. Which is not a motivation for “functionalizing” a program?
a) The divide-and-conquer approach makes program development more
manageable.
b) Software reusability—using existing building blocks to create new
programs.
c) Avoid repeating code.
d) Execution performance—functionalized programs run faster.
ANS: (d)

5.16. Which statement is false?


a) Each function should be limited to performing a single, well-defined
task.
b) If you cannot choose a concise name that expresses what a function does,
it is possible that the function is attempting to perform too many diverse
tasks.
c) Every function should be broken into smaller functions.
d) A function’s parameters are local variables.
In main:
ANS: (c)
int a3;
=

5.5 Fu n c tio n D e finitio ns


intoa
function double
5.17 The function prototype
mamas mysgrt (a);
argument
double mySqrt( ~Parameter
int x ); data
(a) defines a function called mySqrt which takes an integer as an argument
and returns a double
(b) defines a function called double which calculates square roots
(c) defines a function called mySqrt which takes an argument of type x and
returns a double
(d) defines a function called mySqrt which takes a double as an argument and
returns an integer
ANS: (a)

5.18 Using the following function definition, the parameter list is


represented by
A B( C ) {
D
}
(a) A
(b) B
(c) C
(d) D
ANS: (c)

5.19 Using the following function definition, the return value type is
represented by
A B( C ) {
D
}
(a) A
(b) B
(c) C
(d) D
ANS: (a)

5.20 Which of the following will not produce a syntax error?


(a) omitting a return type from a function definition
(b) returning a value from a function defined as void
(c) defining a function parameter again inside a function
(d) using the same names for arguments passed to a function and the
corresponding parameters in the function definition
ANS: (d)

5.21 Which of the following is not an indication that a function is too


complex?
(a) it has a large size
(b) it has a large parameter list
(c) its name is a clear reflection of its function
(d) it performs multiple tasks
ANS: (c)

5.22 Which of the following functions does not contain any errors?
(a) void printnum ( int x )
{
print( “%i”, x );
return x;
}
(b) int cube( int s )
{
int s;
return ( s * s * s );
}
(c) double triple ( float n )
return ( 3 * n );
(d) double circumference ( int r );
return ( 5.14 * 2 * r );
ANS: (c)

5.23. int square( int ); is an example of a function __________.


a) datatype
b) stereotype
c) prototype
d) proceduretype
ANS: (c)

5.24. As used in
int square( int );
int is not a(n) __________.
a) data type
b) parameter type
c) return type
d) function prototype
ANS: (d)

5.25. Which statement is true?


a) Omitting the return value type in a function definition causes a syntax
error if the function prototype specifies a return type other than double.
b) Forgetting to return a value from a function that is supposed to return a
value is a compilation error.
c) Returning a value from a function whose return type is void is a syntax
error.
d) The return type for main must be omitted.
ANS: (c)

5.26. The type of a parameter whose type is omitted in a function


defeinition is __________.
a) int
b) double
c) long
d) float
ANS: (a)

5.27. The most concise notation to use to define function parameters x and y
as double is __________.
a) x, y
b) x, double y
c) double x, y
d) double x, double y
ANS: (d)

5.28. Placing a semicolon after the right parenthesis enclosing the


parameter list of a function definition is a __________ error.
a) logic
b) syntax
c) fatal runtime
d) nonfatal runtime
ANS: (b)

5.29. Which statement is true?


a) The type of every parameter in a function parameter list must be
included.
b) The type of every argument in a function call must be included.
c) It is not incorrect to use the same names for the arguments passed to a
function and the corresponding parameters in the function definition.
d) Defining a function parameter again as a local variable within the
function is a logic error.
ANS: (c)

5.30. Which statement is false?


a) Every block is a compound statement.
b) Every compound statement is a block.
c) Blocks can be nested.
d) Compound statements can be nested.
ANS: (b)

5.31. Which statement is true?


a) Programs should be written as collections of small functions.
b) A function must be no longer than one page.
c) The best engineered functions have many parameters and perform many
distinct tasks.
d) Every function must include an explicit return statement.
ANS: (a)
5.6 Fu n c tio n Pro t o ty p e s

5.32 A function prototype does not have to int function (int


(a) include parameter names
(b) terminate with a semicolon
(c) agree with the function definition
(d) match with all calls to the function
ANS: (a)

5.33 A function prototype can always be omitted when:


(a) a function is defined before it is first invoked
(b) a function is invoked before it is first defined
(c) a function takes no arguments
(d) a function does not return a value
ANS: (a)

5.34. Which statement is false?


a) The compiler uses function prototypes to validate function calls.
b) Prior to ANSI C, C did not include function prototypes.
c) A function prototype tells the compiler the type of data returned by the
function, the number of parameters the function expects to receive, the
types of these parameters and the order in which parameters of these types
are expected.
d) The designers of ANSI C++ borrowed the notion of function prototypes
from the developers of C.
ANS: (d)

5.35. The forcing of arguments to the appropriate types is commonly called


__________.
a) conversion
b) casting
c) coercion
d) transmogrification
ANS: (c)

5.7 H e a d e r Fil e s
5.36 Each standard library has a corresponding __________.
(a) function
(b) variable type
(c) header file
(d) cd-rom
ANS: (c)

5.37. Which standard library header file contains function prototypes for
conversions of numbers to text and text to numbers, memory allocation,
random numbers and other utility functions.
a) <stdarg.h>
b) <stdlib.h>
c) <stdutl.h>
d) <stddef.h>
ANS: (b)

5.8 C a llin g Fu n c tio ns: C a ll b y V a lu e a n d C a ll b y R e f e r e n c e


5.38 When arguments are passed by __________, the caller allows the
called function to modify the original variable’s value.
(a) value
(b) reference
(c) both a and b
(d) none of these
ANS: (c)

5.39. Which statement is true?


a) When an argument is passed call by reference, a copy of the argument’s
value is made and passed to the called function.
b) With call by reference, changes to the passed value do not affect the
original variable’s value in the calling functions.
c) Call by value should be used whenever the called function does not need
to modify the value of the caller’s original value.
d) Call by value should only be used with trusted called functions that need
to modify the original variable.
ANS: (c)

5.9 R a n d o m N u m b e r G e n e r a tio n
5.40 The rand function generates a data value of the type
(a) unsigned int 0- 65535
(b) int
(c) long int
(d) short int
ANS: (a)

5.41 A variable that can only have values in the range 0 to 65535 is a
(a) four-byte int 2
b
65536
=

(b) four-byte unsigned int


(c) two-byte int
(d) two-byte unsigned int
ANS: (d)

5.42 In the expression


n = a + rand() % b;
(a) b is the shifting value
(b) a is the scaling value
(c) b is equal to the width of the desired range of integers
(d) both (a) and (c)
ANS: (d)

5.43 srand
(a) should be called before each call to rand
(b) should be used instead of rand to generate truly random numbers
(c) is unnecessary in C
(d) can use time as an automatically input seed value
ANS: (d)

5.44. Which statement is false?


a) Function rand generates and integer between 0 and MAX.
b) The range of values produced directly by rand is often different than what
is needed in a specific application.
c) The number 6 in the expression rand % 6 is called a scaling factor.
d) The rand function prototype is in <stdlib.h>.
ANS: (a)

5.10 Ex a m p l e : A G a m e o f C h a n c e

5.11 St or a g e C l a sse s
變數名稱
5.45 An identifier’s storage class
(a) determines the period during which that identifier exists in memory
(b) determines whether an identifier is known only in the current source file
or in any source file with proper definitions
(c) determines where the identifier can be referenced in a program
(d) all of the above
ANS: (a)

5.46 A compiler can ignore the storage class specifier


(a) auto
(b) register
(c) static
(d) extern
ANS: (b)

5.47 Which of the following is not true of static local variables?


(a) they are accessible outside of the function in which they are defined
(b) they retain their values when the function is exited
(c) they are initialized to zero if not explicitly initialized by the programmer
(d) they can be pointers
ANS: (a)

特徵
5.48. Which is not an attribute of a variable
a) name
b) definition
c) type
d) value
ANS: (b)

5.49. Which is not a storage class


a) automatic
b) register
sourceasre
c) extern
d) static
ANS: (a)

5.50. Which is not an attribute of a variable?


a) storage class
b) storage duration
c) scope
d) external class
ANS: (d)

5.51. An identifier’s __________ is where the identifier can be referenced


in a program.
a) locality
b) vicinity
c) neighborhood
d) scope
ANS: (d)

5.52. Global variables and function names are of storage class __________
by default.
a) register
b) extern
c) static
d) auto
ANS: (b)

5.12 S c o p e Rul e s

5.53 Labels are the only identifiers with


(a) function scope
(b) file scope
(c) block scope
(d) function-prototype scope
ANS: (a)

5.54 The only identifiers that can be reused elsewhere in a program without
any ambiguity are
(a) global variables
(b) static local variables
(c) those in the parameter list of a function prototype
(d) those in the parameter list of a function definition
ANS: (c)
5.55. Which statement is false?
a) When we define a local variable in a block it can be referenced only in
that block or in blocks in which that block is nested.
b) Labels are the only identifiers with function scope.
c) Labels can be used anywhere in the function in which they appear, but
can not be referenced outside the function body.
d) Labels are used in switch statements and in goto statements.
ANS: (a)

5.56. Which is not a scope for an identifier?


a) function scope
b) record scope
c) block scope
d) function prototype scope
ANS: (b)

5.13 R e c ursio n

5.57 A recursive function is a function that


(a) returns a double
(b) takes 3 arguments
(c) calls itself
(d) is inside of another function
ANS: (c)

5.58 What value does function mystery return when called with a value of
4?
int mystery ( int number ) {
if ( number <= 1 )
return 1;
else
return number * mystery( number – 1 );
}
(a) 1
(b) 24
(c) 0
(d) 4
ANS: (b)

5.59 Recursion is memory-intensive because


59
(a) it must occur numerous times before it terminates
(b) previous function calls are still open when the function calls itself and
the arguments of these previous calls still occupy space on the call stack
(c) many copies of the function code are created
(d) it requires large data values
ANS: (b)

5.60. Which statement is false?


a) A recursive function is a function that calls itself either directly or
indirectly through another function.
b) A recursive function knows how to solve only one or more base cases.
c) The recursion step executes after the original call to the function
terminates.
d) In order for the recursion to eventually terminate, each time the function
calls itself with a slightly simpler version of the original problem, this
sequence of smaller and smaller problems must eventually converge on a
base case.
ANS: (c)

5.14 Ex a m p l e Usin g R e c ursio n: Th e Fi b o n a c c i S e ri e s

5.61 Assuming the following pseudocode for the fibonacci series, what is
the value of the 5th fibinacci number? f(4)
fibbonacci( 0 ) = 0
fibonacci( 1 ) = 1
fibonacci( n ) = fibonacci( n – 1 ) + fibonacci( n – 2 )
(a) 0
(b) 1
(c) 3
(d) 5
ANS: (c)

5.62. Which statement is false?


a) The ANSI C standard does not specify the order in which the operands of
most operators are to be evaluated.
b) The ANSI C standard specifies the order of evaluation of operators &&, ||,
comma, and ?:.
c) A program that evaluates Fibonacci numbers recursively achieves high
performance because of exponential complexity.
d) Programs that depend on the order of evaluation of the operands of
operators other than &&, ||, comma, and ?: can function differently on
systems with different compilers.
ANS: (c)

5.15 R e c ursio n vs. It e r a tio n

5.63 Recursion is to the base case as iteration is to what?


(a) the counter
(b) a repetition structure
(c) failure of the loop continuation test
(d) a selection structure
ANS: (c)

5.64 All of the following are reasons to use recursion except:


(a) an iterative solution is not apparent
(b) the resulting program is easier to debug
(c) it more naturally mirrors the problem
(d) it maximizes software performance
ANS: (d)

5.65. Which statement is false?


a) Both recursion and iteration are based on a control structure.
b) Both iteration and recursion involve repetition.
c) Iteration with sentinel-controlled repetition and recursion each gradually
approach termination.
d) Both iteration and recursion can occur infinitely.
ANS: (c)
6.1 Intro d u c tio n

6.1. Arrays are data structures consisting of related data items of the same
__________.
a) sort order
b) subscript
c) type
d) element
ANS: (c)
IEEETr
6.2. Arrays and structures are __________ entities in that they remain the
same size throughout program execution.
a) dynamic
b) automatic
c) register
d) static
ANS: (d)

6.3. Lists, queues, stacks and trees are __________ data structures that may
grow and shrink as programs execute.
a) flexible
b) automatic
c) dynamic
d) static
ANS: (c)

6.2 Arr a ys

6.4 An array is not


(a) a consecutive group of memory locations
(b) subscripted by integers
(c) pointer-based
(d) a dynamic entity
ANS: (d)

6.5 Assuming that int a has a value of 3 and that integer array b has 7
elements, what is the correct way to assign the value of the sum of 3 and the
third element, to the fifth element of the array?
(a) b[ a + 1 ] = b[ a ] + 3;
(b) b[ a + 1 ] = b[ a - 1 ] + 3;
L

(c) b[ a ] + 1 = b[ a + 3];
(d) b[ a + 2 ] = b[ a ] + 3;
ANS: (b)

6.6 Which of the following is not true?


(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 8 Caj-subscript ma
(d) a subscript cannot be an expression.
ANS: (d)

6.7. An array is a group of memory locations related by the fact that they all
have __________ name and __________ type.
a) different, different
b) same, different
c) different same
d) same, same
ANS: (d)

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


a) null
b) 1
c) 0
d) empty
ANS: (c)

6.9. Which statement is false?


a) The brackets used to enclose the subscript of an array are not an operator
in C.
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.
c) The position number within an array is more formally called a subscript.
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 D e finin g Arr a ys

6.10 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.11. Which definition tells the computer to reserve 12 elements for integer
array c?
a) c[ 12 ] int;
b) int c [ 11 ];
c) c[ 11 ] int;
d) int c[ 12 ];
ANS: (d)

6.6.4 Ex a m p l e s Usin g Arr a ys


6.12 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.13 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.14 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.15 Strings can not


(a) be initialized using string literals
(b) end in a character other than the null character
-
GEE
(c) be initialized with initializer lists
(d) be treated as arrays of characters
ANS: (b)

6.16 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’, ‘\0’ };
(c) string1[] = { ‘t’, ‘e’, ‘s’, ‘t’ };
(d) string1[] = “ ”;
ANS: (c)

6.17. Suppose a program contains the code


for (i = 1; i < = 10; i++)
n[ i ] = 0;
Which statement about this code is true?
a) It contains an off-by-one error.
b) It contains a syntax error.
c) It is initializing the first 10 elements of an array.
d) It is initializing successive elements of an array.
ANS: (d)

6.18. What’s wrong with this code?


int[] = ( 1, 2, 3, 4, 5);
a) The array size must be specified in the square brackets.
b) The parentheses should be square brackets.
c) The square brackets should be curly braces.
d) The parentheses should be curly braces.
ANS: (d) \3
6.19. If there are fewer initializers than elements in the array, the remaining
elements are __________.
a) deleted
b) ignored
c) initialized to empty
d) initialized to zero
ANS: (d)

6.20. Which statement is true?


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

6.21. The following array definition


int n[ 5 ] = { 32, 27, 64, 18, 95, 14 };
a) is correct
b) causes a syntax error because there are only five initializers and six array
elements.
c) causes a logic error because there are only five elements but there are six
initializers.
d) causes a syntax error because there are six initializers but only five array
elements.
ANS: (d)

6.22. Which statement is true? Symbolic constant example: #define N 60


a) A symbolic
-
constant is an identifier that is replaced with replacement text
by the C preprocessor after the program is compiled. Fiatπ*F9
b) Using symbolic constants to specify array sizes makes programs run
faster.
c) Preprocessor directives are not C statements. So they do not end with a semicolon!
d) The #define preprocessor directive must end in a semicolon.
ANS: (c)

6.23. Which statement is true?


a) Assigning a value to a symbolic constant in an executable statement is a
syntax error.
b) A symbolic constant is a variable.
c) Space is reserved for a symbolic constant as with variables that hold
values at execution time.
d) Only uppercase letters can be used for symbolic constant names.
ANS: (a)

6.24. Which statement is true regarding the statement


++frequency[ responses[ answer ] ];
a) This statement increases the appropriate frequency counter depending on
the value of responses[ answer ].
b) This statement increases the appropriate answer counter depending on the
value of frequency[ responses ].
c) This statement increases the appropriate responses counter depending on
the value of frequency[ answer ].
d) This statement produces a syntax error because subscripts cannot be
nested.
ANS: (a)

6.25. Which statement is false?


a) C has no built-in bounds checking to prevent the computer from referring
to an array element that does not exist.
b) The programmer is responsible for implementing array bounds checking.
c) Referring to an element outside the array bounds is a syntax error.
d) Referring to an element outside the array bounds is a logic error.
ANS: (d) syntax
6.26. Which statement is false?
a) The string termination character is called the null character. O
b) The string “string” actually occupies 7 characters in memory. 6+ 0
c) The character representation of the last character in memory of a string is
‘\0’.
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.27. The definition


char string1[] = “first”;
is equivalent to
a) character string1[] = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘\0’ };
b) char string1 = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘\0’ };
c) char string1[] = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’ };
d) char string1[] = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘\0’ };
ANS: (d)

6.28. Which statement is false?


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

6.29. Which statement is false?


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

6.5 P a ssin g Arr a ys t o Fu n c tio ns


6.30 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.31 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.32 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.33. To pass an array to a function, specify


a) the name of the array without any brackets.
b) the name of the array preceded by an empty pair of brackets.
c) the name of the array followed by a pair of brackets including the size of
the array.
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.34. Which statement is false?


a) C automatically passes arrays to functions using simulated call by
reference.
b) When C passes an array to a function, the called function normally can
modify the element values in the caller’s original array.
c) The name of an array is actually the address of the first element of the
array.
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.35. The special conversion specifier for printing addresses is


__________.
a) %a
b) %m
c) %p Hexadecimal: ⼗六進位制
d) %loc
ANS: (c)

6.36. Which statement is true?


a) Entire arrays are passed simulated call by reference and individual array
elements are normally passed simulated call by reference.
b) Entire arrays are passed simulated call by reference and individual array
elements are normally passed call by value.
c) Entire arrays are passed call by value and individual array elements are
normally passed simulated call by reference.
d) Entire arrays are passed call by value and individual array elements are
normally passed call by value.
ANS: (b)

6.6 Sortin g Arr a ys


6.37 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)
Q performance is maximized

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


ANS: (d)

6.38. Which statement about the bubble sort is false?


a) It is easy to program.
b) It is a high-performance sort.
c) It compares only adjacent elements with one another.
d) The bubble sort compares successive pairs of elements.
ANS: (b)

6.39. A bubble sort of 1000 elements requires a maximum of __________


passes.
a) 1001
b) 1000
c) 999
d) 998
ANS: (c)

6.7 C a s e Stu d y: C o m p utin g M e a n, M e d i a n a n d M o d e Usin g


Arr a ys
6.40 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.41. The _____ is the average value of a set of data items.


a) mean
b) median
c) mode
d) matrix
ANS: (a)

6.42. Calculating which of the following normally requires the data to be


sorted first
a) mean
b) median
c) mode
d) total
ANS: (b)

6.43. The __________ is the value that occurs most frequently in the data.
a) mean
b) median
c) mode
d) master
ANS: (c)

6.8 S e a r c hin g Arr a ys

6.44 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.45 The maximum number of comparisons needed for the binary search of
a 2000 element array is
(a) 9 2"= 2048 < 2000 2"= 1024
(b) 15
(c) 11
(d) 14
ANS: (c)

6.46. Which of these is generally thought of as a high-performance


technique?
a) bubble sort
b) linear search
c) binary search
d) iteration
ANS: (c)

6.47. Which of the following statements is false?


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

6.48. A sorted array of a million elements can be searched by a binary


search in __________ or fewer comparisons.
a) 10
100 1000x 1000 < 2". 2' 1024 x 1024
=

b) 20
=

c) 30
d) 999,999
ANS: (b)

6.9 M ulti p l e -Su bs cri p t e d Arr a ys


6.49 A double subscripted 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: (a)

6.50 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.51 Which of the following does not initialize all of its elements to 0?
(a) int b[ 2 ][ 2 ];
b[ 0 ][ 0 ] = b[ 0 ][ 1 ] = b[ 1 ][ 0 ] = b[ 1 ][ 1 ];
(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.52. Which statement is false about multiple-subscripted arrays?


a) C supports multiple-subscripted arrays.
b) A common use of multiple-subscripted arrays is to arrange data into
tables consisting of rows and columns.
c) To identify a particular table element requires two subscripts.
d) Each ANSI C system can support arrays with as many subscripts as the
programmer chooses.
ANS: (d) Each ANSI C system can support arrays with as many subscripts
as the programmer chooses.

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


__________.
a) 12-element array
b) 3-by-4 array
c) 13-element array, because of the zero element
d) 4-by-3 array
ANS: (d) 4-by-3 array

6.54. Which of the following initializations is not performed by the


definition
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
is false?
a)b[ 0 ][ 0 ] is set to 1
b) b[ 0 ][ 1 ] is set to 1
c) b[ 1 ][ 0 ] is set to 3
d) b[ 1 ][ 1 ] is set to 4
ANS: (b) b[ 0 ][ 1 ] is set to 1

You might also like