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

Consider the following Java code (assuming Java allows methods to be declared in

ID: 3813059 • Letter: C

Question

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) {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) {int a = 40; if (x == 0) {return a + b;} else {return bmethod (x - 1) + a + b;}} public static void main (String[] args) {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 understand the difference between static and dynamic scoping, we should first understand what is scope.

Scope of a variable : siginfies the region in which a variable is accessible.

Question.->>Now if a variable is accessible then what should be its value?

This question is answered by Scoping.

When a variable is referred in program we should follow the following priority to associate its value:-

local:- If referred variable is local then in any scoping it is going to be access.

Ancestor:- In static scoping when we are not getting referred variable in local scope then search it in lower to higher ancestor block.

Most recent active sub program:- In dynamic scoping when we are not getting referred variable in local scope then search it in the region of the function which makes calls to current function (keep on doing this until you get referred variable).

For the above question:

a) Static Scoping :

i. b is assigned the value of 2 in main() and the same b is passed to cmethod()(because the local definition of x is referred to first)

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

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

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

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

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

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

vi. Within bmethod() we return : (a=10) + (b=30) + dmethod(0)

vii. From dmethod() we return: (a=0) + cmethod(0) => from where the control goes to cmethod

viii. Within cmethod() since x==0 , we return : (a=40) + (b=20) = 60

-> Traceback 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 assigned the value of 2 in main() and the same b is passed to cmethod()(because the local definition of x is referred to first) => We print : cmethod(2) + (a=10) + (b=2)

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

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

PS. Here (b=2) because the latest value assigned to b is 2.

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

PS: Here (a=40) because the latest value assigned to a is 40.

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

v. The control goes to cmethod, where a gets assigned the value 40 again, and the control goes to the else block as x!=0, from here we return:

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

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

vi. Within bmethod() we return : (a=40) + (b=30) + dmethod(0)

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

vii. From dmethod() we return: (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 (Ans)

Scoping Static Dynamic Priority 1 local local Priority 2 Ancestor most recent active sub program
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote