A.1 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 What is a dangling/stale pointer? And please give a code example.

Does delete delete a pointer?
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

#include < iostream >
#include < string >

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 liftime, begin & end, of this element? Why?

- Memory Area 1: Environment (not tested on 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 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

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

...
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 << "|y: " << y << endl;
x += y;
}

void t4() {
int y = x + 1;
int& z = y;
z += -1;
cout << "x: " << x << "|y: " << y << endl;
}

int main() {
vector< int > vec1{1, 3, 5, 7, 9};
vector< int > vec2{2, 4, 6, 8, 10};
vec1.swap(vec2);
int* ptr = &vec1[1];
y = *(ptr + 2);

t1();
t2();
t3();
t4();
return 0;
}

The program outputs 5 lines. What are they?

A.7

int x = 1, y = -1;

void swapplus(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, 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?

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.