Program methology Review
CSC 340.03+04+05 MIDTERM EXAM REVIEW FALL 2018 TA
PAGE 1 OF 6
1. Section, Date and Time: FULL NAME IN CAPITAL LETTERS | SFSU ID - TIC, FRI 10/16/2018, 8:00 – 8:50 AM - TAC, FRI 10/16/2018, 9:00 – 9:50 AM
- TOE, THU 10/15/2018, 9:30 – 10:45 AM __________________________________|__________________ 2. Midterm Exam (2 exams, 1 dropped): 100 points 3. Please review all related material including WEEK 01-11 in-class code handouts, slides, mock-up exam, reading assignments, in-
class practice, sample programs posted in the File Manager, and assignments. 4. Show our work, using pens, not pencils. Our writing must be clear and readable to the grader(s). 5. Please turn off our devices before the test. Leave all of our belongings except our pen(s) under the white board. HONOR CODE: - Please follow CS Department’s policies: https://cs.sfsu.edu/cheating-and-plagiarism-policy
PART A – 50 Points A.1 - 10 pts Please explain in detail Scott Meyers’s point: Use weak_ptr for shared_ptr like pointers that can dangle.
WEEK-08 Smart Pointer: How To AND ASMT 03
A.2 - 10 pts Please explain in detail 3 different methods of passing smart pointer stu below to a function. Please explain the differences. unique_ptr<Student> stu{ make_unique<Student>(“Mickey”) };
WEEK-09 Smart Pointer: How To
CSC 340.03+04+05 MIDTERM EXAM REVIEW FALL 2018 TA
PAGE 2 OF 6
A.3 - 10 pts Please explain in detail Scott Meyers’s point: shared_ptr(s) offer convenience approaching that of garbage collection for shared lifetime management of arbitrary resources.
WEEK-09 Smart Pointer: How To A.4 - 20 Points … class StuName { public: StuName() {} StuName(string name) { this->name = new string(name); } ~StuName() { cout << *name << ": Destructor called." << endl; } const string* getName() const { return this->name; } private: string * name{ nullptr }; };
void passByRef(const unique_ptr<StuName>& uPtr_R) { cout << " name_uPtr_R: " << addressof(*uPtr_R) << endl; cout << " getName(): " << *(uPtr_R->getName()) << endl; } void passByMove(const unique_ptr<StuName> uPtr_M) { cout << " name_uPtr_M: " << addressof(*uPtr_M) << endl; cout << " getName(): " << *(uPtr_M->getName()) << endl; } void passByShare(const shared_ptr<StuName> sPtr_S) { cout << " name_sPtr_S: " << addressof(*sPtr_S) << endl; cout << " getName(): " << *(sPtr_S->getName()) << endl; } int main() { unique_ptr<StuName> name_uPtr{ make_unique<StuName>("Mickey") }; passByRef(name_uPtr); cout << " name_uPtr: " << name_uPtr << endl; passByMove(move(name_uPtr)); cout << " name_uPtr: " << name_uPtr << endl; name_uPtr = make_unique<StuName>("Minnie"); shared_ptr<StuName> name_sPtr{ name_uPtr.release() }; cout << "use_count(): " << name_sPtr.use_count() << endl; passByShare(name_sPtr); cout << "use_count(): " << name_sPtr.use_count() << endl; cout << "END of Program" << endl; return 0; } What is the output of the program above? Please use @A, @B, @C, @D, and nullptr to represent memory addresses.
WEEK-09 Smart Pointer: How To
CSC 340.03+04+05 MIDTERM EXAM REVIEW FALL 2018 TA
PAGE 3 OF 6
PART B – 50 Points
B.1 - 20 Points #include <iostream> #include <string> using namespace std; class Student { public: Student() { cout << "Student(), constructor called." << endl; } explicit Student(string stuName) : stuName(stuName) { cout << "Student(stuName), constructor called." << endl; } ~Student() { cout << "~Student(), destructor called." << endl; } string getName() { return this->stuName; } virtual void sayHi() { cout << "Student: Hi!" << endl; } void sayHello() { cout << "Student: Hello!" << endl; } private: string stuName{ "Student" }; }; class CSStudent : public Student { public: CSStudent() {} explicit CSStudent(string stuName) : Student() {} void sayHello() { cout << "CSStudent: Hello!" << endl; } }; class CSStudent340 : public CSStudent { public: CSStudent340() { cout << "CSStudent340(), constructor called." << endl; } explicit CSStudent340(string stuName) : CSStudent(stuName) { cout << "CSStudent340(stuName), constructor called." << endl; } ~CSStudent340() { cout << "~CSStudent340(), destructor called." << endl; } void sayHi() { cout << "CSStudent340: Hi!" << endl; } }; int main() { CSStudent340 stuM{ "Mickey" }; cout << "Stu: " << stuM.getName() << endl; unique_ptr<Student> stu{ make_unique<CSStudent340>("Minnie") }; stu->sayHi(); stu->sayHello(); return 0; } This program outputs 10 lines. What are they? Why?
1.
2
.
3.
4.
5.
6.
7.
8.
9.
10.
CSC 340.03+04+05 MIDTERM EXAM REVIEW FALL 2018 TA
PAGE 4 OF 6
B.2 - 10 Points
… int funcB(int); int funcA(int n) { if (n <= 1) return 1; else return n + funcB(n - 2); } int funcB(int n) { if (n <= 3) return 1; else return n * funcA(n - 3); } int main() { int num = 11; cout << funcA(num); return 0; }
What is the output of this program? Please show your work.
B.3 - 10 Points ... class Name { public: Name() {} private: string name{ "CS" }; }; weak_ptr<Name> wPtr; share_ptr<Name> sPtr{nullptr}; void func() { unique_ptr<Name> obj { make_unique<Name>() }; // #1 Insert Code } int main() {
// #2 Insert Code return 0; } [ #1 Insert Code]: Write code to keep the object which obj owns alive outside of the scope of func and wPtr reference obj.
[ #2 Insert Code]: Write code to test if wPtr is dangling. Is it dangling or not?
CSC 340.03+04+05 MIDTERM EXAM REVIEW FALL 2018 TA
PAGE 5 OF 6
B.4 - 5 Points Please code in 2 different ways to convert a unique_ptr owning a Name object to a shared_ptr which owns the same object. Then explain which way the best way is.
B.5 - 5 Points LinkedBag bagA contains: ‘M’, ‘i’, ‘c’, ‘k’, ‘e’, ‘y’ vector vec contains: ‘M’, ‘o’, ‘u’, ‘s’, ‘e’
Use a copy constructor to create LinkedBag bagX using bagA. Then add entries from vector vec to bagX. Then remove all ‘e’ from bagX.
Please explain in detail how each step works and what bagX contains after the steps are executed. Please use linked Nodes diagrams in your answers.
CSC 340.03+04+05 MIDTERM EXAM REVIEW FALL 2018 TA
PAGE 6 OF 6
PART C – 5 Extra Credit Points
The extra credit questions are to be determined.
PLEASE DO NOT DETACH THIS PAGE FROM YOUR
EXAM.
You may get partial credit for what you write on this page.