0% found this document useful (0 votes)
2 views5 pages

Homework

The document is a homework exercise for a programming course involving code analysis and memory management. It includes questions about the output of the code, memory content at a specific line, and verification through execution in Visual Studio. Students are required to complete a template and bring it to the next lecture.

Uploaded by

Salmah Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Homework

The document is a homework exercise for a programming course involving code analysis and memory management. It includes questions about the output of the code, memory content at a specific line, and verification through execution in Visual Studio. Students are required to complete a template and bring it to the next lecture.

Uploaded by

Salmah Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

WRAV201 Homework Exercise

Lecture 1

Consider the code on the following two pages. Answer the following questions:

1. What will be displayed by the code? Write down your answer in the table below before
continuing to the next question.

2. Use the template provided on the last page to draw the contents of the stack and the heap
memory at the point in time when the program reaches the line of code marked //Line A.
Did you find that you would like to change the answer you gave on the first question after
drawing the memory content? Make the changes now.
3. Execute the code in Visual Studio to determine if you correctly predicted what would be
displayed.
Were you correct? If not, would you like to change your drawing of the memory content?

Bring a printout of the completed template to your next lecture.


4.
public class Main {
private int num = 99;

public static void main(String[] args) {


new Main();
}

public Main() {
Dummy d1 = new Dummy("A");
Dummy d2 = new Dummy("B");
Dummy d3 = d1;

int q = num;

d1.update("AAA");
d2.update("BBB");
d3.update("CCC");

System.out.println(d1);
System.out.println(d2);

doInt(d1.i);
doString(d2.s);

System.out.println(d1);
System.out.println(d2);

doPInt(d1);
doPString(d2);
//Line A
System.out.println(d1 == d2);
System.out.println(d2 == d3);
System.out.println(d3 == d1);

System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
}

private void doInt(int i) {


i = num;
}

private void doString(String s) {


s = "ABC";
}

private void doPInt(Dummy d) {


d.i = 77;
}

private void doPString(Dummy d) {


d.s = "XYZ";
}
}
public class Dummy {
public String s = "";
public int i = 0;

public Dummy(String s) {
this.s = s;
}

public void update(String t) {


s = s + " " + t;
i++;
}

@Override
public String toString() {
return s + " (" + i + ")";
}
}
Stack Memory

Stack: Stack:
Type Name Value Type Name Value

Stack:
Type Name Value
Stack:
Type Name Value

Heap Memory

Address #1 #2 #3 #4 #5 #6 #7 #8 #9
:
Value:
Ref Count:

You might also like