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

1) Consider the following program. What output is produced if static scoping is

ID: 3664007 • Letter: 1

Question

1) Consider the following program. What output is produced if static scoping is used? What output is produced if dynamic scoping is used? (4pts)

int a, b;
void p() {
     int a, p;
     a = 0;
     b = 1;
     p = 2;
     return p;
}
void print() {
     printf("%d %d ", a, b);
}
void q() {
     int b;
     a = 3;
     b = 4;
     print();
}
int main() {
     a = p();
     q();
     return 0;
}

Answer:

2) Consider the following program. What output is produced if static scoping is used? What output is produced if dynamic scoping is used? (4pts)

int a, b, c;
void print() {
     printf("%d %d %d ", a, b, c);
}
void q() {
     int b;
     a = 5;
     b = 4;
     print();
}
void p() {
    int a;
     a = 0;
     b = 1;
     c = a + b;
     q();
}
int main() {
     p();
     return 0;
}

Answer:

3) Consider the following program. What output is produced if static scoping is used? (4pts)

procedure P0
     var x: integer;

     procedure P1(x: integer)
          begin
               x := x + 1;
              P2
          end
     procedure P2
          begin
               x := x * 2;
          end
     begin
          x := 1;
         P1(x);
         print x
     end

Answer:

Explanation / Answer

1. If dynamic scoping is used then output is: 3 1

If static scoping is used then output is: 3 1

2.

If dynamic scoping is used then output is: 5 1 1

If static scoping is used then output is: 5 1 1

3.

If static scoping is used then output is: 2