Stuyvesant High School
MCX1 FINAL -- January 2000
Stan Teitel, Principal
Mr. D. Jaye, APS / M. Zamansky CSC
Instructions: Answer all questions on the provided answer sheet. Part
I must be done in pencil; Part II
may be done in pen or pencil.
Part I: Multiple choice. Answer all questions. Each question is worth 4 points. (Answers below)
1. The expression !(-4.2 != 3.0) || !(10 != 20) is:
a) true b) false c)
invalid d) undefined
2. Which of the following statements assign a value of zero to the variable answer
?
a) answer = 4 % 3 - 2;
b) answer = 7/3 - 1;
c) answer = 2 - 5 % 3;
d) answer = 2 + 6 % 3;
3. Suppose that a program contains only the following declarations:
const int middle = 12;
int trial;
char x;
double zonae;
and the function test has been declared as
void test(int &high, double low);
Which of the following function calls is correct?
a) test(trial, middle/2.0)
b) test(middle, middle/2.0)
c) test(middle, zonae)
d) test(zonae, trial)
e) test(middle, 53.6, zonae)
4. Which of the following best represents the output of the program
int main( )
{
int y= 0, z = 0;
for (int x = 0 ; x <= 5 ; x++)
{
y += 1;
z += x;
}
cout << y << "
" << z << endl;
return 0;
}
a) 6 10 b) 6 15 c) 6 21
d) 5 10
5. Under which of the following conditions can a function be overloaded; that is, when
can two functions with
the same name be included in one program?
a) The functions are defined in different library files.
b) The functions have different numbers or types of parameters.
c) The functions have different parameter names.
d) Two functions with the same name can never be included in one
program.
6. In the following program fragment the function sortum needs to be declared and defined:
// Declare and define sortum here
int main( )
{ apvector <int> intarr(100) ;
....
sortum(intarr);
....
return 0;
}
Assume the purpose of sortum is to sort the elements in its
parameter list in ascending order. So before
sortum is called as it is below, we don't assume anything
about the order of the elements of intarr, and after
it is called we assume that the elements of intarr are
arranged in ascending order. The declaration for the function
sortum should be
a) void sortum(apvector <int> & intarr);
b) void sortum(int apvector(100) intarr);
c) int sortum(apvector <int> &intarr);
d) void sortum(const apvector <int> &intarr);
e) void sortum(apvector <int> intarr(100));
7. If we have the declaration bool ace[10]; then which of the following is a
correct statement?
a) ace has exactly two elements or members
b) ace can hold ten elements or members
c) Each element of ace is an integer in the range of 1 through 10
d) The ASCII code for each element of ace is either 0 or 1.
e) None of the statements above are correct.
For questions 8 - 9 consider the following code
segment:
apstring s = "razzle";
int k = s.find('z');
8. What is the value of k after the segment is executed?
a) 0 b) 1 c) 2
d) 3 e) npos
9. Which of the following strings would return a value of npos?
a) sassy b) dazzle c)
zzz d) zebra e) zinggy
For questions 10 through 12 use the following declarations:
const int male = 0, female = 1;
struct student
{
int sex;
apstring name;
double gpa;
};
apvector <student> some_class(100) ;
10. The identifier that represents the name of the fifth record in the vector some_class
is
a) some_class[4].student.name b)
some_class.student.name[4]
c) some_class[4].name
d) some_class.name[4]
e)
some_class[4].student
11. In order to determine whether the gpa of the tenth and the gpa of the twentieth
records in the vector
some_class are equal one would use the expression
a) gpa[9] == gpa[19]
b) gpa[9].some_class == gpa[19].some_class
c) some_class.gpa[9] == some_class.gpa[19]
d) some_class[9].gpa == some_class[19].gpa
e) none, the two cannot be compared
12. The statements
for (int i = 0; i < 100); i++) {
if (some_class[i].sex
== male) nummales += 1;
else numfemales += 1;
}
are best described by:
a) nummales and numfemales will hold the
number of males and females represented by the records in the array class.
b) nummales and numfemales will hold the
number of males and females represented by the records in the array class,
provided that nummales
and numfemales are declared and initialized properly.
c) May generate a run-time error if there is one record in the
array some_class for which the field sex is not properly initialized.
d) Will generate a compile-time error because the proper reference to
the field gpa is class.gpa[i].
e) Will generate both a run-time and compile-time error.
13. Which are the following are true about arrays in C++?
I) elements of an array are stored
contiguously in memory
II) the elements of an array can be accessed by
means of an integral index
III) the index of the first element in an array
is one
IV) the name of an array represents the address
of the first element of the array in memory
a) I, II and IV only
b) I, II and III only c) II, III and IV
only d) I, II, III and IV
For questions 14-15 consider the following code
segment:
int mystery (istream & infile)
{
int k, sum = 0;
while (true)
{
infile >> k;
if (k < 0) return sum;
sum += k;
}
}
14. What is true about the function mystery?
a) It will read numbers from a given file until a negative number is
read and returns the sum of the
non-negative numbers.
b) It will read numbers from a given file and return the sum of these
numbers
c) It will read numbers from a given file until a negative number is
read and returns the sum of these numbers
including that negative number.
d) It will read numbers from a given file until a negative number is
read and returns that negative number.
e) None of these statements are true.
15. If the file opened by the file stream infile contains the following:
2 3 4 2 -1 2 -3 0 -7,
what is the value of sum after this program segment is
executed?
a) 11 b) 10 c) 12
d) 9 e) none of these
Part II: Answer all parts of this question (40 points)
1. Assume that the class BankAccount, specified below, has been implemented.
Each member of the class
represents one person's bank account. The class's public member
functions allow a client program to find out:
° the account number and
° the current balance (how much money is
currently in the account).
They also allow money to be added or to be subtracted from the current balance via a deposit or a withdrawal.
Class BankAccount {
public:
BankAccount (int accountNum);
// constructor
int AccountNum( ) const;
// returns this bank account's account //number
double Balance( ) const;
// returns this bank account's current //balance
void Deposit(double amount);
// adds amount to current balance
void Withdraw(double amount);
//subtracts amount from current balance
....
private:
// private data members
};
Also assume that the following declaration has been made to represent information about
one transaction
on a bank account (one deposit or one withdrawal):
struct Transaction {
int accountNum;
char flag; // either 'd' for deposit or 'w' for
withdrawal
double amount;
};
a) Write function FindAccount, as stated below. FindAccount should
return the index of the BankAccount
in array A with the given account number num
or return -1 if there is no such BankAccount
in the array.
Complete function FindAccount below. Assume that it is called only with values that satisfy its precondition.
int FindAccount( const apvector <BankAccount> &A,
int num)
//precondition: no two BankAccounts in array A have
the same account number
b) Write function OneTransaction, as started below. Function OneTransaction
has two parameters: an array of
BankAccounts and a Transaction. Function OneTransaction
should find the BankAccount record with the given
account number and should deposit or withdraw the given amount as
appropriate.
For example, assume that accounts.length( ) is 4 and that
the elements in the array have the following account
numbers and balances:
[0]
[1]
[2]
[3]
100
107
102
105
$100.27
$57.30
$150.00
$5.25
Here are some examples to illustrate what the call OneTransaction(accounts,
trans) should do:
Value of trans
Modified element of accounts array
accountNum: 107
[1]
flag: 'd'
107
amount: 10.50
67.80
accountNum: 100
[0]
flag: 'w'
100
amount: 100.27
0.0
accountNum: 105
[3]
flag: 'w'
105
amount: 6.00
- 0.75
In writing function OneTransaction, you may include calls to
function FindAccount, specified above in part (a).
Assume that FindAccount works as specified, regardless of what
you wrote for part (a).
Complete function OneTransaction below. Assume that OneTransaction
is called only with values that satisfy
its precondition.
void OneTransaction( apvector <BankAccount>
&accounts, Transaction trans)
// precondition: no two BankAccount records in the accounts
array have the same account number,
//
one of the BankAccount record in the accounts array has account number trans.accountNum,
//
trans.flag is either 'd' or 'w'
c) Write function DaysTransactions, as started below. Function DaysTransactions
has four parameters: an
array of BankAccount records named accounts, an
array of Transactions named transactions, an array
of integers named overdrawn, and an integer N. DaysTransactions
should carry out each of the transactions
in the transactions array. It should then fill in the overdrawn
array with the numbers of all of the accounts in the
accounts array that are overdrawn (have a negative balance) at
the end of the day. Finally, it should set N to
the number of overdrawn accounts.
For example, if the accounts array is initially as shown
above in part (b) and the transactions array contains all
three of the example transaction shown in part (b), then N
should be set to 1, and the overdrawn array should
contain the account number 105, because that is the only account with a
negative balance.
In writing function, DaysTransactions, you may include
calls to function OneTransaction, specified above
in part (b). Assume that OneTransaction works correctly,
regardless of what you wrote for part (b).
Complete function DaysTransctions below. Assume that it is called only with values that satisfy its precondition.
void DaysTransactions ( apvector <BankAccount>
&accounts,
const apvector <Transaction> &transactions,
apvector <int> &overdrawn,
int & N)
// precondition: No two BankAccounts records in the accounts
array have the same account number.
// For every transaction in the transactions array there is a BankAccount
record in the accounts array with
// an account number that is in the acountNum field.
// For every transaction in the transactions array, the flag
field is either 'd' or 'w'.
// overdrawn.length( ) >= accounts.length( )
PART I SOLUTIONS: 1. b 2.
c 3. a 4. b 5. b 6.
a 7. b 8. c 9. a
10. c 11. d
12. b 13. a 14. a 15. a