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
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.
Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void)...
XCORE Question 1 Consider the following program void Elint x, int y Y = y + 1 cout<<x<<"*«y << endl; void nain) 1 int i, a13): all) = 15; a 2) - 203 a13) = 25; cout <i«"" <all) <<"" << a12) << ""« a[3] << endl; cout <i<** <all) << "" << a12) <<""« a[3] << endl; What values of the variable and array A are printed with the following rules. a parameters are passed by value bi parameters...
Consider the following C program: int sub(int *sum) { *sum =*sum +*sum; return 10; } void main() { int num= 3; num = sub(&num)+ num; } What is the value of num after the assignment statement in main, assuming operands are evaluated left to right. operands are evaluated right to left.
Give the output for the following code assume int globalvar 10: above the function prototypes void foo(int&); void bar(int); int main() { int globalVar = 5; foo(globalVar); cout << globalVar << endl; bar(globalVar); cout << globalVar << endl; return 0; } void foo(int& x) { x = globalVar + x; cout << globalVar << endl; return; void bar(int x) { globalVar + x; cout << globalVar << endl; X = return;
Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k; Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl; Magic(2,4); cout<<"i, j, k and...
4. Consider the following C program: (2 points) int fun( int *j){ + 11; return 9; } void main(){ int k 2; k + fun( &k); k What is the value of k after the assignment statement in main, assuming operands are evaluated left to right. operands are evaluated right to left. Please, also explain how you got the values in detail.
16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int main) int x-1,y 0,z-2; x-fun2(y,z); cout sx fun 1 (z); cout (#xtytz(endl; y-fun2(x,x); cout <exty+zscendl; system("pause"); void fun 1 (int a) int fun2(int a, int b) int static c2; return atx; 16 Points) Question 3 Write down the outputs of the following program into the provided table...
C++ output 6) What is the exact output of the following program? #include <iostream> using namespace stdi void zolo(int &a, int sb) int main int x = 5, y =8; zolo(x,y)i cout << "x " << x << endl ; cout << "y = "" << y << endl ; return o: void zolo(int &a, int &b) int v1 = b + a; ' int v2 = b-a; 3 a=v1;13
Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int); int main() { f1(10); return 0; } void f1(int n) { f2(n + 5); } void f2(int n) { f3(n - 2); } void f3(int n) { cout << n << endl; // LINE 1 } Just before the program statement marked with the comment "LINE 1" is executed, how many stack frames will be on the program call stack?
What is the output of the following program? int main(void) { int x, y, z; x = 5; y= test(x); z= x + y; cout<< setw(4)<<x <<” + “<<setw(4)<<y<<”=” << setw(4)<<z<<endl; return 0; } int test (int x) { int w; w = x + 10; return (w); }
Consider the following small program. What will the output be? def foo(x): print(x) def bar(): return 3 def baz(y, z): return y * z a = bar() + 2 b = baz(a, 2) foo(a + b) 10 3 2 25 none of these 15