1992 AP Computer Science Exam A: adapted for C++
Solutions to 1992 Part I
1. If addition had higher precedence than multiplication, then the value of the expression
1 + 2 * 3 + 4 * 5
would be which of the following? A. 27 B. 47 C. 65 D. 69 E. 105
2. Consider programming this decision:
<statement A> is to be carried out only when both X and Y are positive; <statement B> is to be
carried out only when <statement A> is not carried out.
If the probability that X is positive is .7, the probability that Y is positive is .4, and the decision is to be executed
1000 times, which of the following code segments will implement the decision correctly and most efficiently?
A. if (X > 0 && Y > 0) <statement A>; D. if (X <= 0 || Y <= 0) <statement B>;
else <statement B>; else <statement A>;
B. if (Y > 0 && X > 0) <statement A>; E. if (Y < 0 || X < 0) <statement B>;
else <statement B>; else <statement A>;
C. if (X < 0 || Y < 0) <statement B>;
else <statement A>;
3. A programmer is to code a C++ function whose sole purpose is to write an error
message to the terminal
screen. Which of the following statements best characterizes the
decision as to whether the function's return
type should or should not be void?
A. There is no reason to choose void over another return type or
vice versa.
B. The return type should not be void because C++ functions cannot be
recursive.
C. The return type should be void because C++ functions cannot call
functions which have a void return type.
D. The function should have a void return type because it does not
return a value.
E. The function should return a value because C++ functions which don't
have a void
return type are more efficient than those that
have a void return type.
4. The purpose of a subprogram's precondition is to
A. initialize the local variables of the subprogram
B. describe the conditions under which the compiler is to abort
compilation
C. describe the conditions under which the subprogram may be called so
that it satisfies its postcondition
D. describe the algorithm used by the subprogram
E. describe the effect(s) of the subprogram on its postcondition
5. A program to print a calendar includes the following code.
for (int month = 1; month <= 12; month++) {
printHeading(month, year);
printDays(month, year);
}
The printDays function includes the following code.
printSpaces(month, year);
for (int day = 1; day <= numDaysIn(month, year); day++) {
cout << setw(3) << day;
if ( endOfWeek(day, month, year) ) cout << endl;
}
If, when the program is run, every week on the calendar printed has eight days,
which of the
subprograms is most likely to contain the bug?
A. printHeading B. PrintDays C. printSpaces D. numDaysIn E. endOfWeek
6. A program is to be written that will manipulate 100 names of 30 characters each. The
program
will perform several tasks, including reading names, sorting names, and
printing names. Of the
following defined objects, which will be the most appropriate for the
program?
A. apmatrix<char> table(100, 30); D.
apvector<apstring> list(30);
B. apmatrix<char> table(30, 100); E.
apmatrix<apstring> table(30, 100);
C. apvector<char> list(3000);
7. A standard C++ compiler runs on several different computers, ranging from
microcomputers to
mainframes. For this compiler, which of the following might be
different on the different machines?
I. The value of INT_MAX II. The number of
reserved words III. The collating sequence
A. I only B. III only C. I and II D. I and III E. II and III
8. Assume that A is a one-dimensional array of N positive integers and that the following assertion is true.
A[0] > A[k] for all k such that 0 < k < N.
Which of the following is a valid conclusion?
A. The array is sorted in ascending order.
D. A[0] holds the smallest value in the array.
B. The array is sorted in descending order. E.
A[0] holds the largest value in the array.
C. All the values in the array are identical.
Questions 9-10 refer to the following function.
int answer(int n) {
if ( n == 1 )
return 2;
else
return 2 * answer(n - 1);
}
9. What value does answer(5) return?
A. 2 B. 8 C. 10 D. 32 E. 120
10. If n is a positive integer, how many times will answer be called to evaluate
answer(n)
(including the initial call)?
A. 2 B. n C. 2n D. n2 E. 2n
11. Consider the following program.
#include <iostream.h>
int main() {
int x, y;
cin >> x;
while ( x != 0 ) {
cin >> y;
if ( y == 0 )
cout << "yes" << endl;
else
cout << "no" << endl;
cin >> x;
}
return 0;
}
Which of the following input sequences ensures that every statement of the program is executed
at least once?
A. 1 1 1 2 0 B. 1 0 1 1 0 C. 1 0 1 0 1 0 0 D. 1 0 0 E. 1 1 0
Questions 12-13 refer to the following information.
The code if ( n == 1 )
k = 1;
else
if ( n == 2 )
k = 2;
is rewritten in the form
if ( <condition> )
<assignment statement>;
where <condition> and <assignment statement> are chosen so that the rewritten code performs the
same task as the original code. Assume that both n and k are variables of the int type.
12. Which of the following could be used as <condition>?
I. (n == 1) || (n == 2) II. (n == 1) && (n == 2) III. (n >= 1) && (n <= 2)
A. I only B. II only C. III only D. I and III E. II and III
13. Assume that evaluating <condition> changes neither n nor k. Which of the
following could be used as <assignment statement>?
A. k = n B. k -= 1 C. k =2 D. k += n E. k = n k
14. What happens when the program shown below is compiled and executed?
#include <iostream>
int recurse(int x) {
x = 3;
if ( x == 0 )
return 0;
else {
x = 1;
return x + recurse(x);
}
}
void main() {
cout << recurse(0) << endl;
}
A. The program writes the value 5.
B. The program writes the value 6.
C. The program fails to compile because of illegal use of recurse on the right-hand side of an assignment operator.
D. The program fails to terminate because of infinite recursion.
E. The program fails to compile because the value returned by a function cannot be the actual parameter of a cout statement.
Questions 15-16 are based on the following code.
void transfer(ifstream &sourceFile, ofstream &largeNumbers, ofstream &smallNumbers) {
// precondition: sourceFile contains one integer per line and is open for reading at the beginning of the file;
// the other two files are open for writing and are initially empty.
//postcondition: All the elements of sourceFile larger than the global constant largeInteger form the contents
// of largeNumbers and the other elements of sourceFile form the contents of smallNumbers.
int item;
sourceFile >> item;
while ( ! sourceFile.eof() ) {
if ( item > largeInteger )
<write statement 1>;
else
<write statement 2>;
}
}
15. Which of the following changes must be made to the code for the postcondition to be accurate?
I. <write statement 1> and <write statement 2> are replaced by largeNumbers << item and by
smallNumbers << item, respectively.
II. <write statement 1> and <write statement 2> are replaced by largeNumbers >> item and by
smallNumbers >> item, respectively.
III. The statement sourceFile >> item is inserted in the while loop, after else <write statement 2>
A. I only B. II C. I and III D. II and III E. either I or II only
16. If the code implements the postcondition, which of the following could be added to the postcondition
without destroying its accuracy?
I. largeNumbers and smallNumbers are ordered numerically.
II. The elements of largeNumbers are in the same order in which they appear in sourceFile.
III. The elements of smallNumbers are in the same order in which they appear in sourceFile.
A. I only B. II only C. III only D. II and III only E. I, II, and III
17. Consider the following procedure.
void mystery(int n) {
if ( n > 2 )
mystery(n % 3);
cout << n / 3 << " ";
}
The function call mystery(38) will yield as output which of the following sequences of numbers?
A. 0 12 B. 12 0 C. 1 1 0 2 D. 1 1 1 1 E. 2 0 1 1
18. Consider the following declarations.
const int maxItems = <some large integer constant>;
struct listType {
int nums[maxItems];
int size; //** number of items in the list
};
double averageOf(const listType &items) {
// precondition: 0 < items.size <= maxItems (items.size != 0)
int sum = 0;
for (int count = 1; count <= items.size; count++)
<statement 1>;
<statement 2>;
}
Which of the following substitutions for <statement 1> and <statement 2> will cause averageOf to return
the average of the values in items?
<statement 1> <statement 2>
A. sum = sum + items.nums[count 1] return double(sum / (count 1))
B. sum = sum + items.nums[count 1] return sum / (count 1)
C. sum = sum + items.nums[count 1] return double(sum) / items.size
D. sum = sum + items.nums[count] return sum / items.size
E. sum = sum + items.nums[count] return double(sum) / items.size
19. Consider the following subprogram.
void getVal(char &valToReturn) {
cin.get(valToReturn);
if ( valToReturn != '\n' )
getVal(ValToReturn);
}
Assume that an input sequence of more than one character, terminating with a newline character, will be
entered. The final value of parameter valToReturn is which of the following?
A. The rightmost character in the input sequence
B. The leftmost character in the input sequence
C. The middle character in the input sequence
D. The character preceding the rightmost character in the input sequence
E. Undefined because procedure getVal never returns
20. The following code is designed to set Index to the location of the first occurrence of Goal in the array A,
and to set Index to 1 if Goal does not occur in A.
Index = 0;
while ( A[Index] != Goal )
Index += 1;
if ( A[Index] != Goal )
Index = 1;
Which of the following describes the condition under which this program segment will fail to perform the task
described?
A. Whenever Goal is the first element of the array D. Whenever Goal is 0
B. Whenever Goal is the last element of the array E. Whenever Goal equals A[Goal]
C. Whenever Goal is not present in the array
Solutions for Part I of Adapted 1992 Exam: |
|||||||||
| 1. E | 2. B | 3. D | 4. C | 5. E | 6. A | 7. A | 8. E | 9. D | 10. B |
| 11. B | 12. D | 13. A | 14. D | 15. C | 16. D | 17. A | 18. C | 19. D | 20. C |
Solutions for Part I of 1999 APCS AB Exam: Questions 16-20 refer to Large Integer Case Study |
|||||||||
| 1. C | 2. E | 3. D | 4. E | 5. B | 6.E | 7. B | 8. A | 9. B | 10. D |
| 11. D | 12. E | 13. A | 14. B | 15. B | 16. D | 17. D | 18. C | 19. A | 20. E |
| 21. B | 22. A | 23. B | 24. C | 25. C | 26. E | 27. C | 28. E | 29. D | 30. B |
| 31. E | 32. B | 33. E | 34. E | 35. C | 36. A | 37. B | 38. D | 39. C | 40. E |