Function and Recursion · Programming Languages · GATE CSE
Marks 1
Consider the following C program. Assume parameters to a function are evaluated from right to left.
#include <studio.h>
int g(int p) { printf("%d", p); return p; }
int h(int q) { printf("%d", q); return q; }
void f(int x, int y) {
g(x);
h(y);
}
int main() {
f(g(10),h(20));
}
Which one of the following options is the CORRECT output of the above C program?
Consider the following C program:
#include <stdio.h>
void fX();
int main() {
fX();
return 0;}
void fX() {
char a;
if ((a = getchar()) != '\n')
fX();
if (a != '\n')
putchar(a);}
Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the following statements is CORRECT?
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;
}
Consider the following ANSI C function:
int SomeFunction (int x, int y)
{
if ( (x == 1) I I (y == 1)) return 1;
if (x == y) return x;
if (x > y) return SomeFunction(x - y, y);
if (y > x) return SomeFunction(x, y - x);
}
The value returned by SomeFunction(15, 255) is _______.
#include < stdio.h >
int jumble (int x, int y) {
x = 2 * x + y ;
return x ;
}
int main ( ) {
int x=2, y=5 ;
y = jumble (y, x) ;
x = jumble (y, x) ;
printf ("%d \n", x) ;
return 0 ;
}
The value printed by the program is ______.
#include < stdio.h >
int counter = 0;
int calc (int a, int b) {
int c;
counter++;
if (b==3) return (a*a*a);
else {
c = calc(a, b/3);
return (c*c*c);
}
}
int main (){
calc(4, 81);
printf ("%d", counter);
}
The output of this program is _____.#include < stdio.h >
int main()
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = ‘0’;
printf("%s", s1);
}
What will be printed by the program?void foo(char *a){
if ( *a && *a != ' '){
foo(a+1);
putchar(*a);
}
}
The output of the above function on input “ABCD EFGH” isChar inchar = 'A';
Switch ( inchar ) {
case 'A' : printf ("Choice A\ n") ;
case 'B' :
case 'C' : printf (“Choice B”) ;
case 'D' :
case 'E' :
default : printf ( " No Choice" ) ; }
i) Abstraction and encapsulation
ii) Strictly-typedness
iii) Type-safe property coupled with sub-type rule
iv) Polymorphism in the presence of inheritance
float f,(float x, int y) {
float p, s; int i;
for (s=1,p=1,i=1; i < y; i++) {
p *= x/i;
s+=p;
}
return s;
}
For large values of y, the return value of the function f best approximatesMarks 2
Consider the following C function definition.
int f(int x, int y) {
for (int i=0; i<y; i++) {
x=x+x+y;
}
return x;
}
Which of the following statements is/are TRUE about the above function?
What is printed by the following ANSI C program?
#include <stdio.h>
int main(int argc, char *argv[ ]) {
int a[3][3][3] = {{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for(i = 0; i < 3; i++) {
for(k = 0; k < 3; k++)
printf("%d"",a[i][j][k]);
printf("\n");
}
return 0;
}
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 ________
Consider the following ANSI C program.
#include <stdio.h>
int main( ) {
int i, j, count;
count = 0;
i = 0;
for (j = -3; j <= 3; j++) {
if ((j >= 0) && (i++))
count = count + j;
}
count = count + i;
printf("%d", count);
return 0;
}
Which one of the following options is correct?
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 ≤ i ≤ 9. The value returned by SimpleFunction (Z, 10, 2) is ______
int fun1 (int n) {
static int i = 0;
if (n > 0) {
++i;
fun1(n-1);
}
return (i);
}
___________________
int fun2 (int n) {
static int i = 0;
if (n > 0) {
i = i + fun1 (n);
fun2(n-1);
}
return (i);
}
The return value of fun2 (5) is _______.
int tob(int b, int* arr) {
int i;
for (i=0; b>0; i++) {
if (b%2) arr[i] = 1;
else arr[i]=0;
b = b/2;
}
return (i);
}
____________________
int pp(int a, int b) {
int arr[20];
int i, tot = 1, ex, len;
ex = a;
len = tob (b, arr);
for (i=0; i < len; i++) {
if (arr[i] ==1)
tot = tot * ex;
ex = ex * ex;
}
return (tot);
}
The value returned by pp (3, 4) is ________.
Consider the following C function.
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?
Consider the following C program :
#include<stdio.h>
int r()
{
static int num=7;
return num--;
}
int main()
{
for(r();r();r())
printf("%od ",r());
return 0;
}
Which one of the following values will be displayed on execution of the programs?
unsigned long int fun(unsigned long int n){
unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return(sum);
}
The value returned when we call fun with the input $${2^{40}}$$ is#include < stdio.h >
int *A, stkTop;
int stkFunc(int opcode, int val)
{
static int size=0, stkTop=0;
switch (opcode) {
case -1: size = val; break;
case 0: if (stkTop < size) A[stkTop++] = val; break;
default: if (stkTop) return A[--stkTop];
}
return -1;
}
int main()
{
int B[20]; A = B; stkTop = -1;
stkFunc (-1, 10);
stkFunc ( 0, 5);
stkFunc ( 0, 10);
printf ("%d\n", stkFunc(1, 0) + stkFunc(1, 0));
}
The value printed by the above program is __________.Program main;
Var . . .
Procedure A1;
Var . . .
Call A2;
End A1
Procedure A2;
Var . . .
Procedure A21;
Var . . .
Call A1;
End A21
Call A21;
End A2
Call A1;
End main.
Consider the calling chain: Main $$ \to $$ A1 $$ \to $$ A2 $$ \to $$ A21 $$ \to $$ A1
The correct set of activation records along with their access links is given by
unsigned int foo (unsigned int n, unsigned int r) {
if (n > 0) return((n % r) + foo(n/r, r));
else return 0;
}
What is the return value of the function foo when it is called as foo (513, 2)? unsigned int foo (unsigned int n, unsigned int r) {
if (n > 0) return((n % r) + foo(n/r, r));
else return 0;
}
What is the return value of the function foo when it is called as foo (345, 10)?void recerse (void) {
int c;
if (?1) reverse() ;
?2
}
main {
printf ("Enter Text" );
printf("\n");
reverse() ;
printf("\n") ;
}
int f(int n)
{
static int r = 0;
if(n <= 0) return 1;
if(n>3)
{
r = n;
return f(n-2)+2;
}
return f(n-1)+r;
}
What is the value of f(5)?
void foo (int n, int sum) {
int k = 0, j = 0;
if(n==0) return;
k=n%10; j = n /10;
sum = sum + k;
foo(j, sum);
printf("%d",k);
}
int main () {
int a = 2048, sum = 0;
foo(a, sum);
printf("%d\n", sum);
}
What does the above program print?double foo(double); /* Line 1 */
int main () {
double da, db;
//input da
db = foo(da);
}
double foo(double a){
return a;
}
The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:int f(int n)
{
static int i = 1;
if(n>=5) return n;
n = n+1;
i++;
return f(n);
}
The value returned by f(1) ismain ( )
{
int x, y, m, n;
scanf("%d %d", &x, &y);
/* Assume x > 0 and y > 0 */
m=x; n=y;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
printf("%d", n);
}
The program computesClass P {
void f(int i) {
print(i);
}
}
Class Q subclass of P {
void f(int i) {
print(2*i);
}
}
Now consider the following program fragment:
Px = new Q();
Qy = new Q();
Pz = new Q();
x.f(1); ((P)y).f(1); z.f(1);
Here ((P)y) denotes a typecast of y to P. The output produced by executing the above program fragment will be
char a[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address 0, the address of a[40][50] is
int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main () {
int i,j;
for (i = 0; i <= 4; i++)
j = incr(i);
}
isint Trial (int a, int b, int c)
{
if ((a > = b) && (c < b)) return b;
else if (a > = b) return Trial (a,c,b);
else return Trial (b,a,c);
}
The function Trial:program side-effect (input, output);
var x, result: integer:
fucntion f (var x:integer):integer;
begin
x:x+1;f:=x;
end
begin
x:=5
result:=f(x)*f(x)
writeln(result)
end
Function fun (x:integer):integer;
Begin
If x > 100 then fun : x – 10
Else fun : fun(fun (x + 11))
End;
program COMPUTE (input, output);
var
X:integer;
procedure FIND (X:real);
begin
X:=sqrt(X);
end;
begin
X:=2
Find(X)
Writeln(X)
end
var a, b : integer;
begin
a:=a+b;
b:=a-b;
a:=a-b;
end;