Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Consider the following Java code (assuming Java allows methods to be

ID: 3699995 • Letter: P

Question

Problem 1: Consider the following Java code (assuming Java allows methods to be declared inside other methods: public class AClass [ private static int a -10; private static int b- 20; public static int bmethod(int y) l int b 30; public static int dmethod(int z) int a z; return a + cmethod (z); return a b + dmethod(y); public static int cmethod (int x) l int a40; if xo) f return a + b; else t return bmethod (x-1) + a+ b; public static void main (String[] args)t int b 2; System.out.println(cmethod(b a b); What is the value printed by the System.out.println statement when the main method is run if a) Java uses static scoping b) Java uses dynamic scoping Be sure to trace your reasoning and justify your answer!

Explanation / Answer

ANSWER

To comprehend the difference between standing and dynamic scoping, we be supposed to opening realize what is scope.

range of a variable : signifies the region in which a changeable is easy to get to.

Question.->>Now if a changeable is accessible then come again? Should be its rate?

This problem is answer by Scoping.

When a changeable is referred in program we be supposed to follow the following main anxiety to connect its value:-

scoping static dynamic

priority1 local local

prority2 ancestor most recent active sub programm

local:-

If referred variable is local after that in any scoping it is departing to be right of entry.

Ancestor:-

In static scoping when we are not in acceptance of referred variable in general scope then search it in lower to advanced ancestor hunk.

Most topical active sub program:-

In dynamic scoping whilst we are not getting referred up-and-down in local scope then explore it in the region of the occupation which makes calls to up to date function (keep on responsibility this until you get referred changeable).

For the above question:

a) Static Scoping :

i

b is assigned the rate of 2 in main() and the equivalent b is passed to scheme()(because the local characterization of x is referred to first)

ii.

inside method() x refers to b(which has a value of 2). Since x != 0, organize goes to the else block.

Inside the else wedge we return system(x-1)+a+b => bmethod(2-1) + (a=40) + (b=20)

iii.

The manage now goes to b method where y=1. From at this point we return : (a=10) + (b=30) + way(y=1)

iv. The control go inside dmethod somewhere we return : (a=1) + cmethod(1)

v. The organize goes to cmethod, where it go to the else block as x!=0, from here we depart:

method(1-1) + (a=40) + (b=20)

vi.

inside bmethod() we go another time : (a=10) + (b=30) + dmethod(0)

vii.

as of dmethod() we return: (a=0) + cmethod(0) => from where the manage goes to cmethod

viii. surrounded by cmethod() as x==0 , we return : (a=40) + (b=20) = 60

-> Trace back to step vii) (a=0) + 60 = 60

-> Traceback to step vi) (a=10) + (b=30) + 60 = 100

-> Traceback to step v) (a=40) + (b=20) + 100 = 160

-> Traceback to step iv) (a=1) + 160 = 161

-> Traceback to step iii) (a=10) + (b=30) + 161 = 201

-> Traceback to step ii) (a=40) + (b=20) + 201 = 261

-> Traceback to step i) (a=10) + (b=2) + 261 = 273 (Ans)

b) Dynamic Scoping :

i.

b is assign the value of 2 in main() and the equal b is passed to cmethod()(because the local description of x is referred to first) => We print : cmethod(2) + (a=10) + (b=2)

ii.

within cmethod() x refers to b(which has a worth of 2). Since x != 0, control goes to the else block.

in the else chunk we return bmethod(x-1)+a+b => bmethod(2-1) + (a=40) + (b=2)

PS. Here (b=2) since the latest value assign to b is 2.

iii.

The organize now goes to bmethod where y=1. From here we return : (a=40) + (b=30) + dmethod(y=1)

PS: Here (a=40) since the latest value assign to a is 40.

iv.

The control go inside dmethod where we revisit : (a=1) + cmethod(1)

v.

The control goes to cmethod, someplace a gets assigned the value 40 again, and the have power over goes to the else lump as x!=0, from here we return:

bmethod(1-1) + (a=40) + (b=30)

PS: Here b=30, since the last rate assigned to b was 30

vi.

Within bmethod() we come back : (a=40) + (b=30) + dmethod(0)

PS: Here a=40, since the last value assign to a was 40

vii.

From dmethod() we go back: (a=0) + cmethod(0) => from where the control goes to cmethod

viii.

Within cmethod() since x==0 , we return : (a=40) + (b=30) = 70

PS: Here b=30, since the last value assigned to b was 30

-> Traceback to step vii) (a=0) + 70 = 70

-> Traceback to step vi) (a=40) + (b=30) + 70 = 140

-> Traceback to step v) (a=40) + (b=30) + 140 = 210

-> Traceback to step iv) (a=1) + 210 = 211

-> Traceback to step iii) (a=40) + (b=30) + 211 = 281

-> Traceback to step ii) (a=40) + (b=2) + 201 = 243

-> Traceback to step i) (a=10) + (b=2) + 243 = 255