ECE 264: Object-Oriented Software Development
Fall 2012
Exam 1 Solutions (9:00-9:50 am, 10/10/2012)
Name: __________________________________________
ID #: _________________
For this exam, you are allowed to use textbooks, lecture notes (printed), one 8.5” x 11” doublesided page of notes and other paper-based materials. All electronic devices (e.g., cellular phones,
PDAs) and Computer are prohibited. If you have a cellular phone, please turn it off prior to the
start of the exam to avoid distracting other students.
The exam contains 3 questions for a total of 100 points. You may get extra bonus points from
Question 2(b). Please answer the questions in the spaces provided. If you need additional space,
use the back of the page on which the question is written and clearly indicate that you have done
so.
Please read each question carefully before you answer.
You will have 50 minutes to complete this exam.
Q1: C++ input/output
Q2: Classes
Q3: Writing code
TOTAL SCORE
/ 40
/ 40+ n*4 bonus points
/ 20
/ 100+ n*4 bonus points
1
1. (40 points; 10 points per part (a-d)) C++ input/output
For each code snippet shown below, list the output exactly as it will appear on the screen. Be
sure to clearly indicate spaces between characters when necessary.
a.
int A, B, Y;
char M, N;
double X;
cin >> A >> M >> X;
cin >> B >> N >> Y;
cout << A << endl << B;
cout << endl << Y << endl;
Assume the input is:
2 6 4
3 12 13.7
Solutions:
2
3
13
b.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n = 17;
cout << n << " in hexadecimal is: "
<< hex << n << endl
<< setbase( 10 ) << n << " in decimal is: "
<< n << endl;
} // end main
2
Solutions:
17 in hexadecimal is: 11
17 in decimal is: 17
c.
int A[4] = {0, 16, 32};
for (int i = 0; i < 3; i++) {
if ((A[i] % 5) == 1)
cout << dec;
else if ((A[i] % 5) == 2)
cout << hex;
cout << A[i] << endl;
}
Solutions:
0
16
20
3
// A[i] % 5 == remainder
//
of A[i] / 5
d.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double n = 83;
cout << fixed << showpos
<< setw( 10 ) << setprecision( 3 ) << setfill( '-' )
<< n << endl;
}
Solutions:
---+83.000
4
2. (40 points + n*4 bonus points) Classes
All parts of this question deal with a class, Account, that is implemented by “Account.h” and
“Account.cpp” files as shown in the following table 1 and tables. The class contains set/get
functions that change/return each data member.
a. (20 points) What is the output by the following main functions?
1
2
3
4
5
6
int main()
{
Account account1( -2017 );
cout << "account1 balance: $" << account1.getBalance() << endl;
} // end mai
Table1: Account.h
1 // Definition of Account class.
2 class Account
3 {
4 public:
5 Account( int ); // constructor initializes balance
6 void credit( int ); // add an amount to the account balance
7 int getBalance(); // return the account balance
8 private:
9 int balance; // data member that stores the balance
10 }; // end class Account
Table 2: Account.cpp
1 // Member-function definitions for class Account.
2 #include <iostream>
5
3 using namespace std;
45
#include "Account.h" // include definition of class Account
67
// Account constructor initializes data member balance
8 Account::Account( int initialBalance )
9 {
10 balance = 0; // assume that the balance begins at 0
11
12 // if initialBalance is greater than 0, set this value as the
13 // balance of the Account; otherwise, balance remains 0
14 if ( initialBalance > 0 )
15 balance = initialBalance;
16
17 // if initialBalance is negative, print error message
18 if ( initialBalance < 0 )
19 cout << "Error: Initial balance cannot be negative.\n" << endl;
20 } // end Account constructor
21
22 // credit (add) an amount to the account balance
23 void Account::credit( int amount )
24 {
25 balance = balance + amount; // add amount to balance
26 } // end function credit
27
28 // return the account balance
29 int Account::getBalance()
30 {
31 return balance; // gives the value of balance to the calling
function
32 } // end function getBalance
b. (20 points + n*4 bonus points) Some of the lines in the program below, which uses the
Account class, contain potential compiler errors. Determine which lines are incorrect, and
write correct versions in the table below. You may not need all lines in the table, as the file may
contain less than 8 errors. If you can find 4 line errors and correct them, you will get 20 full
points. If you can find more errors in other n lines (except the 4 lines you have identified)
and correct them, you will get bonus points (n*4).
Assume that you have a corrected version of Account.h and Account.cpp as described in Table 1
and Table 2 above. Do not worry about program correctness; just ensure that each line will
compile without an error.
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Line
#
4
7
9
10
#include <iostream>
using std::cout;
using std::endl;
using <Account.h>
int main() {
Account r2(15,7);
int bal;
Account::credit(“500”);
bal = r2.balance;
cout << "The balance in r2 is " << r2.bal << " or "
<< getBalance() << endl;
return r2;
}
Corrected code
#include “Account.h”
Must use #include with .h files
Account r2(15);
Account::credit(500);
bal = r2.getBalance();
balance is a private data member of r2—you can't access it directly
cout << "The balance in r2 is " << r2.getBalance();<< " or
"
11
<< r2.
12
13 Return 0;
getBalance()
7
3. (20 points) Writing code: input/output
Write a program that prints the value 100.453627 rounded to the nearest digit, tenth, hundredth,
thousandth and ten thousandth. (Notes: see expected output; adding your code after line#12;
You may use loop structure and setprecision(int) function; see the notes in line#12)
1 // Coding Exercise 2: rounding.cpp
2 #include <iostream>
3 #include <iomanip>
4 using namespace std;
5
6 int main()
7 {
8 double x = 100.453627; // value to test precision outputs
9
10 cout << fixed; // display output using fixed-point notation
11
12 // display output using loop counter as precision
.
} // end main
Expected output
Solutions:
13 for ( int loop = 0; loop <= 5; loop++ )
14 cout << setprecision( loop ) << "Rounded to " << loop
15 << " digit(s) is " << x << endl;
8
© Copyright 2026 Paperzz