Question

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 produce?
a) Assuming static (lexical) scope binding of names. b) Assuming dynamic scope binding.

21. Consider the following C program:

int f(int *i) {
          *i +=2;

y++;

return i; }

     void main() {
          int x = 1;
          int y = 2;
          x = x*y + f(&x);
     }

What is the value of x after the assignment statement in main, assuming

a) operands are evaluated left to right. b) operands are evaluated right to left.

22.Consider the following program written in C syntax:

     void foo(int x, int y) {
        i = y;

} y += 3;

     void main() {
        i=2;

}

j=3;
foo(i,j);
printf(“%d %d\n”, i, j);

For each of the following parameter-passing methods, what will be the output of the program?

a) Passedbyvalue
b) Passedbyreference c) Passedbyvalue-result

0 0
Add a comment Improve this question Transcribed image text
Answer #1

20.
By using static scope.
In static scoping a variable always refers to its top level environment.
main() function calls baz().
global variables x=3 and y=5 are declared.
in baz() function set local variable y as 7 and calls bar().
in bar() local variable x=10 and y=y+3=5+3=8. and calls foo().
in foo() x=x+2=3+2=5 and y=y+4=8+4=12.
after that bar() function prints the local variable x and global variable y.
it prints 10 and 12.and then it comes to the baz().
it prints local variable y,it prints 7.

Finally the program prints 10,12,7. using static scoping.

By using Dynamic scoping

it refers the identifier with most recent environment.
First of all x=3 and y=5.
the main() calls baz().In baz() y modified to 7,and calls bar().
in bar() x value is set to 10,x=10,y=y+3=7+3=10.and then calls foo().
in foo() x=x+2=10+2=12,and y=y+4=10+4=14.then it comes to bar() and prints values of x and y.
it prints 12 and 14.and then in prints y value in baz().it prints 14.

the output of the program using dynamic scope is 12,14,14.

21.

a.operands are evaluated from left to right.
x=1,y=2
x=x*y+f(&x).
it evaluates the value of x*y=1*2=2
and then calling the function that increments the value of x by 2 = x+2=1+2=3
finally x=2+3=5.

b.operands are evaluated from right to left
x=1,y=2
x=x*y+f(&x).
it calls the function first = x=x+2=3.
then evaluates value of x*y=3*2=6.
finally x=6+3=9.

22.

call by value:

#include <stdio.h>

void foo(int x, int y) {
int i = y;
y += 3;
}

int main() {
int i=2;
int j=3;
foo(i,j);
printf("%d %d\n", i, j);
}

Output: 2 3

call by reference:the function modifies the original variable.

#include <stdio.h>

void foo(int *x, int *y) {
int *i = *y;
*y += 3;
}

int main() {
int i=2;
int j=3;
foo(&i,&j);
printf("%d %d\n", i, j);
}

Output: 2 6 .

passed by value result.

the values of arguments are copied into the formal parameters.

final values of the of parameters are copied back out to the arguments

#include <stdio.h>

void foo(int *x, int *y) {
int i = *y;
*y += 3;
}

int main() {
int i=2;
int j=3;
foo(&i,&j);
printf("%d %d\n", i, j);
}

Output: 2 6.

If you have any queries, please comment below.

Please upvote , if you like this answer.

Add a comment
Know the answer?
Add Answer to:
Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void)...
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
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