Functions


Q1.

Consider the following ANSI C program #include < stdio.h > int foo(int x, int y, int q) { if ((x < = 0) && (y < = 0)) return q; if (x < = 0) return foo(x, y-q, q); if (y < = 0) return foo(x-q, y, q); return foo(x, y-q, q) + foo(x-q, y, q); } int main( ) { int r = foo(15, 15, 10); printf("%d", r); return 0; } The output of the program upon execution is ____
GateOverflow

Q2.

Consider the following program: int main() { f1(); f2(2); f3(); return(0); } int f1() { return(1); } int f2(int X) { f3(); if (X==1) return f1(); else return (X*f2(X-1)); } int f3() { return(5); } Which one of the following options represents the activation tree corresponding to the main function?
GateOverflow

Q3.

The integer value printed by the ANSI-C program given below is ______.#include < stdio.h > int funcp(){ static int x = 1; x++; return x; } int main(){ int x,y; x = funcp(); y = funcp()+x; printf("%d\n", (x+y)); return 0; }
GateOverflow

Q4.

Consider the following ANSI C function: int SimpleFunction(int Y[], int n, int x) { int total = Y[0], loopIndex; for (loopIndex=1; loopIndex<=n-1; loopIndex++) total=x*total +Y[loopIndex]; return total; } Let Z be an array of 10 elements with Z[i]=1, for all i such that 0\leq i \leq 9. The value returned by SimpleFunction(Z,10,2) is __________
GateOverflow

Q5.

What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed? a=3; void n(x){x=x*a; print(x);} void m(y){a=1;a=y-a;n(a);print(a);} void main(){m(a);}
GateOverflow

Q6.

Consider thefollowingprogram: int f(int*p, int n) { if (n<=1)return0; else returnmax(f(p+1,n-1),p[0]-p[1]); } int main() { int a[]={3,5,2,6,4}; printf("%d", f(a,5)); } Note: max(x,y) returns the maxi mumof x and y. The value printed by this program is____________ .
GateOverflow

Q7.

Consider the following recursive C function that takes two arguments unsigned int rer(unsigned int n, unsigned int r){ if(n>0)return(n%r + rer(n/r,r)); else retturn 0; }What is the return value of the function rer when it is called as rer(513,2)?
GateOverflow

Q8.

Consider the following C program: void convert(int n) { if (n<0) printf("%d",n); else { convert(n/2); printf("%d",n%2); } } Which one of the following will happen when the function convert is called with any positive integer n as argument?
GateOverflow

Q9.

Consider the C functions foo and bar given below: int foo (int val ) { int x = 0; while (val > 0) { x = x + foo(val--); } return val ; } int bar (int val ) { int x = 0; while (val > 0) { x = x + bar(val-1);} return val ; } Invocations of foo(3) and bar(3) will result in:
GateOverflow

Q10.

What is the output of the C++ program? #include < iostream > using namespace std; void square(int *x){ *x = (*x)++ * (*x); } void square(int *x, int *y){ *x = (*x) * --(*y); } int main() { int number = 30; square(&number, &number); cout < < number; return 0; }
GateOverflow