SJSU CMPE 126 S20 –Midterm 1 Exam
First:________________ Last:________________ Last 4 id:_____
Each problem earns 2 pt for correct answer, 0 pt for no answer, -1 pt for guessing.
Assume there is a stock class which has symbol (string), cost (int) and shares (int) as private members.
(1pt) Write the minimum declaration that supports statement
stock s2(“APPL”, 209), s3(“FB”, 50, 2); // 1 share of APPL at $209, 2 shares of FB at $50
(1pt) Write additional declaration that supports statement
std::cout << s2 << s3; // print out stock details, 2 x FB @50
Write additional member declaration and its implementation that support statement
std::cout << s2 + s3; // print out total dollar value of both stocks
(1pt) In addition to information hiding, give one good reason to put data members as private.
Suppose we want to create a stockDB class that stores a variable number of stocks in a dynamic array. Complete the minimum stockDB.h class definition such that
stockDB frank(5), wife(700); // frank can have at most 5 stocks, wife 700 max
class stockDB {
Write the constructor code for stockDB above.
Further assume that stock has operator< overloaded and there is a global swap(stock&, stock&) function. Complete the following implementation code for stockDB selection sort.
void stockDB::selectionSort() {
(1pt) Given the following two array declarations:
int a[5] = {10, 20, 30, 40, 50};
int *b = new int[5];
Write ONE LINE code that will copy all contents of a into b.
(3pt for no error, 2pt for 1 error, 1pt for 2 errors) Continue with the previous problem, assume array a starts at address 2000 and b starts at 7000.
If the following code is valid, what is the output. If it is invalid, explain why.
cout << a+2; ___________________________
cout << *a+2; _________________________________
cout << *(a++); __________________________________
cout << *b+2; ________________________________
cout << *(b++); ________________________________
Write a few lines of code that will cause memory leak. Explain it.
Complete the code fragment below.. suggestion: stringstream
string s=”(3, 45)”; // representing complex number 3+45i
char c;
int real, imag;
std::cout << real << “ + ” << imag << “i” << std::endl; // print out 3 + 45i