So I am looking at a past exam paper which I am studying for my exams coming up and I am really confused regarding this question, can someone give details on the state of the memory for the following:
class Q2 {
private static int num = 0;
private String prefix;
public Q2 (String p)
{ prefix = p; }
public String Msg (String str) {
String n;
num++;
n = num.ToString();
return n + ” - ” + prefix + str;
}
}
Using an appropriate diagram, describe the state of memory after all of the following statements have been executed.
Q2 var1, var2;
var1 = new Q2(“Question 2 ”);
var2 = new Q2 (“Another view ”);
Now consider the method call.
… var1.Msg (“hello again”) …
Explain in detail the following aspects of the execution of this statement; use diagrams where appropriate.
The referencing and calling of the method.
The execution and return of the method.
Discuss the memory management issues that will arise with the following infinite loop.
while (true) {
Var1 = new Q2 (“Part c ”);
String s = Var1.Msg (“round and round”);
… … …
}
I have been trying to draw diagrams to represent the stack and heap for this example, but I am confused due to the way the objects are created, I appreciate any help on this.
Thanks.