Sale!

CSC 340.03+.04 MIDTERM EXAM 01 solved

Original price was: $35.00.Current price is: $28.00.

Category:

Description

5/5 - (1 vote)

PART A – 100 Points

A.1 – 10 pts –

How is a pointer different from a reference variable? And please give a code example. Would these statements cause an error? Why or why not? int year = 2019; int yearNext = 2020; int & ref = year; ref = yearNext;

A.2 – 10 pts

– What is a dangling/stale pointer? And please give a code example. Does delete delete a pointer? [ Yes ] [ No ] Please explain why. Then please give instructions how to properly deallocate an object allocated on Free-Store. Please use a code example to demonstrate.

A.3 – 20 Points

#include #include using namespace std; string type = “Credit”; class credit_card { public: credit_card() = default; explicit credit_card(const double& balance, string com = “Disney”) : com_(move(com)), balance_(balance) {} void display_info() const { cout << credit_card::type_ << ” [” << this->com_ << “]: ” << this->balance_ << endl; } void set_com(const string& com) { this->com_ = com; } private: static

string type_; string com_{ “N/A” }; double balance_{ 0 }; }; string credit_card::type_ = type; credit_card& update_credit_card(const double& balance) { credit_card cc1{ balance }; static credit_card* cc2 = new credit_card{ 100 }; cc1.set_com(“Tesla”); *cc2 = cc1; return *cc2; } int main() { credit_card cc3 = update_credit_card(300); cc3.display_info(); credit_card cc4 = credit_card{ cc3 }; cc4.set_com(“Zoom”); cc4.display_info(); credit_card* cc5 = new credit_card{ update_credit_card(500) };

cc5->display_info(); cc3.set_com(“Google”); cc4.display_info(); return 0; } For each element listed below, please answer: a. In which memory area is this element stored? Why? b. The lifetime, begin & end, of this element? Why? – Memory Area 1: Environment (not tested in this exam) – Memory Area 2: Runtime Stack – Memory Area 3: Free-store – Memory Area 4A: Uninitialized Data (global, static…)

– Memory Area 4B: Initialized Data (global, static…) – Memory Area 5: Binary Program (not tested in this exam)  type Which area: [1] [2] [3] [4a] [4b] [5] Why is that area? What is its lifetime and why? cc2 Which area: [1] [2] [3] [4a] [4b] [5] Why is that area? What is its lifetime and why? cc3, the object Which area: [1] [2] [3] [4a] [4b] [5] Why is that area? What is its lifetime and why? CC4, the object Which area: [1] [2] [3] [4a] [4b] [5] Why is that area? What is its lifetime and why? c. What is the output of the program?

A.4 – 0 pts

– Not covered in class yet. Please have this question as a future practice problem. Please explain each parameter passing method, when to use it, and code a function prototype example. Pass-by-lvalue-reference Pass-by-const-lvalue-reference

A.5 – 0 pts –

Not covered in class yet. Please have this question as a future practice problem. int cs = 340; int *pointer = &cs;

Please code a constant pointer which points to pointer Please code an lvalue reference to reference *pointer Please code an rvalue reference to reference *pointer Please use what you created, if legal, to change the value of cs to 413. Provide your code or reasoning:

A.6 – 30 Points

… static int x = 1; int y = x * 2; void t1() { y++; cout << “x: ” << x << ” | y: ” << y << endl; y += 1; x -= -1; } void t2() { int* x = &y; cout << “x: ” << x << ” | y: ” << y << endl; } void t3() { int y = x; static int x = 2; cout << “x: ” << x + 1 << ” | y: ” << y + x << endl; x += y; } void t4() { int y = x + 1; int& z = y; z += -1; cout << “x: ” << x + z << ” | y: ” << y << endl; } int main() { vector vec1{ 1, 3, 5, 7, 9 };

vector vec2{ 2, 4, 6, 8, 10 }; vec1.swap(vec2); int * ptr = &vec1[1]; y = *(ptr + 2); t1(); t2(); t3(); t3(); t4(); return 0; } This program outputs 5 lines. What are they? 1. 2. 3. 4. 5.

A.7 – 30 Points

int x = 1, y = -1; void swapplus1(int n1, int n2) { int temp = n1 + 1; n1 = n2 – 1; n2 = temp; x = x + n1; } void swapplus2(int& n1, int& n2) { int temp = n1 + 1; n1 = n2 – 1; n2 = temp; } void swapplus3(const int& n1, const int& n2) { int n1val, n2val, temp = n1 + 1; n1val = n2 – 1;

n2val = temp; y -= n2; } void swapplus4(int* p1, int* p2) { int temp = *p1 + 1; *p1 = *p2 + 1; *p2 = temp; x = *p1 + y; } void swapplus5(int* &p1, int* &p2) { int* temp = p1 + 1; p1 = p2 – 1; p2 = temp; } void print(const int& x, const int& y) { cout << “\n x: ” << x << ” |y: ” << y; } int main() { int arr[]{ 2, 4, 6, 8, 10, 12, 14 }; y = arr[3] / size(arr) ; swapplus1(x, y); print(x, y); swapplus2(x, y); print(x, y); swapplus3(x, y); print(x, y); swapplus4(&x, &y); print(x, y); int *px = &x, *py = &y; (*px)–; (*py) -= -7; swapplus5(px, py); print(x, y); return 0; }

This program outputs 5 lines. What are they? 1. 2. 3. 4. 5.

PART B – 0 Points

B.1 – 0 pts –

Not covered in class yet. Please have this question as a future practice problem. What did the IDEO team do during the Problem Formulation phase? Did you use the problem-solving steps in ASMT 2 and ASMT 3? How or why not?

B.2 – 0 pts –

Not covered in class yet. Please have this question as a future practice problem. IDEO: In the future, if you will be a CEO of a software company, will you fire people who are better than you are? How will you feel sitting in a meeting with these people? What are the risks if any?