Question

Consider the following program written in C syntax int a , b , c ; //...

Consider the following program written in C syntax

int a , b , c ; // first declaration

void g()
{
print(a,b,c);
}
int f(int a) // parameter declaration
{

int b; // second declaration
b = a + 1;
g(); // first call
{

int a; // third declaration
int c; // fourth declaration
c = b;
a = b + c;
g(); // second call
}
g(); // third call
return a + b ;
}

int main()
{
int a = 2; // fifth declaration
a = f(a);
g(); // fourth call
}

1. If static scoping is used, the reference to a in the first call to g() resolves to which declaration?
2. If static scoping is used, the reference to a in the third call to g() resolves to which declaration?
3. If dynamic scoping is used, the reference to a in the first call to g() resolves to which declaration?
4. If dynamic scoping is used, the reference to a in the third call to g() resolves to which declaration?
5. What is the output of this program if static scoping is used?
6. What is the output of this program is dynamic scoping is used?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. Reference to first declaration. It is because only the binding is done at compile time. Hence, it always refers to global variable. Also, the g() is a global function.
  1. Reference to first declaration. It is same as above, because binding is only done at compile time. Hence, it refers to first declaration.

  1. Reference to fifth declaration. Dynamic binding always uses last declaration in a recent call.

  1. Reference to third declaration. As we know, dynamic binding always uses last declaration in a recent call. Here, third call resolves the reference to third declaration.

  1. NOTE: At the time of declaration, the global variables are initialized to 0.

OUTPUT:

000                // It refers first declaration in first call

000               // g() refers to first declaration in static.

000              // third call of g()

000               // fourth call of g()

  1. Output:

230            // In first call of g(), a=2 by last declaration before the call, b=a+1=3 and

                  // c = 0

Garbage    // in second call of g(), a and b are not initialized and, they are non-static.

230           // g(), third call

500           // g() fourth call

Add a comment
Know the answer?
Add Answer to:
Consider the following program written in C syntax int a , b , c ; //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following program written in C syntax:

    Question 4: Consider the following program written in C syntax:void swap(int a, int b) { int temp;temp = a; a = b;b = temp;}void main() {int value = 4, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]);swap(list[0], list[1]); swap(value, list[value]);}For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap?1.  Passed by value2.  Passed by reference3.  Passed by result   By valueBy referenceBy result Output 

  • 2. Consider the following programs (using C's syntax): #include <stdio.h> int a- 1, b-2; int foo...

    2. Consider the following programs (using C's syntax): #include <stdio.h> int a- 1, b-2; int foo (int x) 1 return (x+a); void ba r () { printf("%d, %d\n",a,b); void foobar) } printf("%d, %d\n", a, b) ; int a -4; bar bfoo (b); bar int main)( int b 8; foobar printf ("%d, %d\n", a, b) ; return 0; (a) What does the program print under static scoping? (b) What does the program print under dynamic scoping?

  • Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void)...

    Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void) { x = x + 2; y = y + 4; } void bar(void) { int x = 10; y = y + 3; foo( ); cout << x << endl; cout << y << endl; } void baz(void) { int y = 7; bar( ); cout << y << endl; } void main( ) { baz( ); } What output does this program...

  • Given the following program in a C-like syntax, what does F1(m) return assuming static scoping and...

    Given the following program in a C-like syntax, what does F1(m) return assuming static scoping and dynamic scoping? You can assume that expressions are evaluated from left to right. int m = 3; int F1(int i) { return (i + F2(m) + m); } int F2(int j) { j++; return (j * m); } int main() { int m = 5; F1(m); } Explain in details how you got the answers to earn full credit. Don't just write the answer....

  • Answer only The following questions refer to the skeletal C++ program shown below. void f10 void...

    Answer only The following questions refer to the skeletal C++ program shown below. void f10 void £20 Part 8: (10 points) int a,c,di int main) int c,d eit void ri int b, e t) int a, b: 12( void 12( int b, d if (..) { int a, di / Line 1 (1) Assume that static scoping is used, indicate which version of each of the following variables will be visible at Line 1 by filling in each blank with...

  • Consider the following JavaScript skeletal program: //the main program var x: function sub1 () { var...

    Consider the following JavaScript skeletal program: //the main program var x: function sub1 () { var x: function sub2 () { } } function sub3 () { } Assume the execution of this program is in the following unit order: main calls sub1 sub1 calls sub2 sub2 calls sub3 a. Assuming static scoping, which declaration of x is the correct one for a reference to x in: i. sub1 ii. sub2 iii. sub3 b. Repeat part a, but assume dynamic...

  • programming languages in #f language QUESTION 4 Consider the following skeletal C program: void funi(void); /*...

    programming languages in #f language QUESTION 4 Consider the following skeletal C program: void funi(void); /* prototype */ void fun2(void); /* prototype */ void fun3(void); /* prototype */ void main() { int a, b, c; void fun1(void) { int b, c, d; void fun2 (void) { int c, d, e; void fun3(void) { int d, e, f, Assuming that dynamic scoping is used, what variables are visible during execution of the last function called in following calling sequence: main calls...

  • Consider the following program, written in JavaScript-like syntax: // main program var x, y, z; function...

    Consider the following program, written in JavaScript-like syntax: // main program var x, y, z; function sub1() { var a, y, z; . . . } function sub2() { var a, b, z; . . . } function sub3() { var a, x, w; . . . } Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last subprogram activated? Include with each visible variable the name of the...

  • Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P...

    Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P (M:proc) int a:=2 M() proc K int b:=3 P(F) K() //main program print(a) //built in function a- what does this code print if it uses dynamic scoping and deep binding? b- what does this code print if it uses dynamic scoping and shallow binding?

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT