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

5. What It the output of function scoping_test when static scoping or dynamic sc

ID: 3711765 • Letter: 5

Question

5. What It the output of function scoping_test when static scoping or dynamic scoping is used? (Otherwise assume C++ syntax and semantics, e.g. call by value).
int n = 1; // global
plus_n (int x) { cout << x + n; }
increment_n ( ) { n = n + 2; plus_n (n); }
scoping_test ( ) { int n;    n = 200; plus_n (7);    n = 50; increment_n ( ) ; cout << n; }

With Static Scoping: With Dynamic Scoping:    5. What It the output of function scoping_test when static scoping or dynamic scoping is used? (Otherwise assume C++ syntax and semantics, e.g. call by value).
int n = 1; // global
plus_n (int x) { cout << x + n; }
increment_n ( ) { n = n + 2; plus_n (n); }
scoping_test ( ) { int n;    n = 200; plus_n (7);    n = 50; increment_n ( ) ; cout << n; }

With Static Scoping: With Dynamic Scoping:   
int n = 1; // global
plus_n (int x) { cout << x + n; }
increment_n ( ) { n = n + 2; plus_n (n); }
scoping_test ( ) { int n;    n = 200; plus_n (7);    n = 50; increment_n ( ) ; cout << n; }

With Static Scoping: With Dynamic Scoping:   

Explanation / Answer

Ans- The output of the function scoping_test() is 8650

Expl- in scoping_test() we declare n=200(local scop) and call function plus_n(7) so the value of x in plus_n() =7 so x=7 and we print x+n where n=1(global scope) so it will print 8. after that we modifiy n=50(local scop) and call increment_n() function in increment we call statement n=n+2(n is global scope with value n=2) so value of n=1+2=3

n=3 and pass this value in plus_n() so here we x=3 and n=3 (global modified n) so it will print 6. now we print value of n(local scope with n=50) so print 50 final output is 8650.

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