Starting from:

$29.99

CMPSC 461: Programming Languages Concepts Solution

Prof. G. Tan
Submission: Please submit your homework via Gradescope. During submission, you need to match pages and homework questions. You can watch a video about how to do that via Gradescope below:
https://www.youtube.com/watch?time_continue=1&v=KMPoby5g_nE&feature= emb_logo
If you submit a scanned version of your on-paper answers, please make sure your scanned version is legible.
1. (6 points) Consider the following function written in C:
int x=3, y=2, z=1; void foo(int a, int b) { x = x + b; a = a - b;
}
In each of the cases below, write down the values of x, y and z after the following calls to foo(). If necessary, assume that output arguments are copied back to parameters in the left-to-right order.
(a) foo(y,z) where all parameters are passed by value
(b) foo(y,z) where all parameters are passed by reference
(c) foo(y,z) where all parameters are passed by value-result
(d) foo(x,y) where all parameters are passed by reference
(e) foo(x,x) where all parameters are passed by reference
(f) foo(x,x) where all parameters are passed by value-result
2. (2 points) Consider the following C++ program, where &i means i is passed by reference:
int bar (int &i) { i = i - 2;
return 2 * i;
}
void foo1 () { int x = 3, y = 6, sum; sum = x; sum = sum + bar(x);
}
void foo2 () { int x = 2, y = 7, sum; sum = bar(x); sum = sum + x;
}
(a) What is the value of sum at the end of the function foo1? Briefly explain why.
(b) What is the value of sum at the end of the function foo2? Briefly explain why.
3. (4 points) Consider the Ada program given below.
1 procedure Main is
2 A, B, C : Integer ;
3 procedure Sub1 (C: Integer ) is
4 D, E : Integer ;
5 begin −− of Sub1
6 . . .
7 end; −− of Sub1
8 procedure Sub2 (E: Integer ) is
9 procedure Sub3 (F: Integer ) is
10 C, D: Integer ;
11 begin −− of Sub3 12 Sub1 (100);
13 end; −− of Sub3
14 begin −− of Sub2
15 Sub3(E);
16 end; −− of Sub2
17 begin −− of Main
18 Sub2 (10);
19 end; −− of Main
Ada is a statically scoped language. In the above program, the Main function invokes Sub2; Sub2 invokes Sub3; and Sub3 invokes Sub1. Draw the stack of activation records when the program’s execution reaches before line 12 and line 6. For each activation record, include local variables, parameters, the dynamic link, the static link, and the return address.

More products