Professional Documents
Culture Documents
Chapter 1
Program Specification, Design, and Analysis
Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch
ISBN 0-8053-7470-1, Softcover, 784 pages, 1997
Short Answers
Section 1.2
Running Time Analysis
Give a concise formula that gives the approximate number of digits in a
positive integer. The integer is written in base 10.
Why is the order of an algorithm generally more important than the speed of
the processor?
Convert each time formula to the best possible big-O notation. Do not include
any spurious constants in your big-O answer. Time FormulaBig-O
10n.
2n�.
3 times log (base 2) of n.
2n� + 10n.
Multiple Choice
Multiple Choice
Section 1.1
Specification and Design
Why is writing easily modifiable code important?
A. Easily modifiable code generally has a quicker run time.
B. Most real world programs require change at some time.
C. Most text editors make it easy to modify code.
D. Several people may be writing the same function at the same time.
Which phase of the software life-cycle is usually the most expensive?
A. Analysis and specification of the task
B. Design
C. Implementation
D. Maintenance and evolution
What will happen if a function is executed and the precondition for the
function is not met?
A. An error message will be printed.
B. The program will loop indefinitely.
C. The system will crash.
D. Any of the above results could happen.
Answer true or false for this statement: When programming in teams, the
specification of a function should be written by the person writing the code
for the function.
TRUE.
FALSE.
Answer true or false for this statement: When programming individually (not in
a team), there is no need to write a specification for a function
TRUE.
FALSE.
If the precondition fails, it is a good idea to write a useful error message
and then halt the program. Why is the program halted?
A. Most operating systems forbid continuation.
B. The function is no longer guaranteed to make the postcondition true.
C. The function's memory requires have become exponential (or worse).
D. The function's running time has become exponential (or worse).
What ANSI C function is used to stop the program execution when a precondition
is not met.
A. assert();
B. exit();
C. return();
D. void();
Which of these function calls will always cause a program to halt? (x is an
int variable).
A. assert(10 > 0);
B. assert(10 < 0);
C. assert(x < 0);
D. None of the above will always cause a program to halt.
Multiple Choice
Section 1.2
Running Time Analysis
What does a run-time analysis usually count?
A. The number of arithmetic and other operations required for the program to
run
B. The number of megabytes required for the program to run
C. The number of seconds required for the program to run
D. The number of seconds plus the number of megabytes
E. The number of seconds times the number of megabytes
Which of these is the correct big-O expression for 1+2+3+...+n?
A. O(log n)
B. O(n)
C. O(n log n)
D. O(n�)
Which of the following formulas in big-O notation best represent the
expression n�+35n+6?
A. O(n�)
B. O(n�)
C. O(n)
D. O(42)
Answer true or false for this statement: For all possible inputs, a linear
algorithm to solve a problem must perform faster than a quadratic algorithm to
solve the same problem.
TRUE.
FALSE.
Answer true or false for this statement: True or false: An algorithm with
worst case time behavior of 3n takes at least 30 operations for every input of
size n=10.
TRUE.
FALSE.
What term is used to describe an O(n) algorithm.
A. Constant
B. Linear
C. Logarithmic
D. Quadratic
Here is some code for an integer variable n:
while (n > 0)
{
n = n/10; // Use integer division
}