Question 1
Consider the following ANSI C program
#include
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 _________ .
60
10
15
50
Question 2
Consider the following code:
int a, b, c = 0;
void prtFun(void);
void main()
{
static int a = 1;
prtFun();
a += 1;
prtFun() printf("\n %d %d ", a, b);
}
void prtFun(void)
{
static int a = 2;
int b = 1;
a += ++b;
printf("\n %d %d ", a, b);
}
What output will be generated by the given code d\\segment if:
Line 1 is replaced by “auto int a = 1;”
Line 2 is replaced by “register int a = 2;” (GATE CS 2012)
3 1
4 1
4 2
4 2
6 1
6 1
4 2
6 2
2 0
4 2
4 2
2 0
Question 3
Consider the above question. What output will be generated by the given code d\\segment if:
Line 1 is replaced by “auto int a = 1;”
Line 2 is replaced by “register int a = 2;”
3 1
4 1
4 2
4 2
6 1
6 1
4 2
6 2
2 0
4 2
4 2
2 0
Question 4
Consider the following C program
int a, b, c = 0;
void prtFun (void);
int main ()
{
static int a = 1; /* line 1 */
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
void prtFun (void)
{
static int a = 2; /* line 2 */
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
What output will be generated by the given code segment?
3 1
4 1
4 2
4 2
6 1
6 1
4 2
6 2
2 0
3 1
5 2
5 2
Question 5
The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudocode below is invoked as height (root) to compute the height of a binary tree rooted at the tree pointer root.

The appropriate expression for the two boxes B1 and B2 are
B1 : (1 + height(n->right)), B2 : (1 + max(h1,h2))
B1 : (height(n->right)), B2 : (1 + max(h1,h2))
B1 : height(n->right), B2 : max(h1,h2)
B1 : (1 + height(n->right)), B2 : max(h1,h2)
Question 6
Consider the following function written in the C programming language. The output of the above function on input “ABCD EFGH” is
void foo (char *a)
{
if (*a && *a != ` `)
{
foo(a+1);
putchar(*a);
}
}
ABCD EFGH
ABCD
HGFE DCBA
DCBA
Question 7
Consider the following program:
int f(int *p, int n)
{
if (n <= 1) return 0;
else return max(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 maximum of x and y. The value printed by this program is
2
3
4
5
Question 8
What will be the output of the following JAVA program?
#include <iostream>
using namespace std;
static int d = 1;
void count(int n) {
cout << n << " ";
cout << d << " ";
d++;
if(n > 1) count(n - 1);
cout << d << " ";
}
int main() {
count(3);
return 0;
}
#include <stdio.h>
static int d = 1;
void count(int n) {
printf("%d ", n);
printf("%d ", d);
d++;
if(n > 1) count(n - 1);
printf("%d ", d);
}
int main() {
count(3);
return 0;
}
class GFG
{
static int d=1;
static void count(int n)
{
System.out.print(n+" ");
System.out.print(d+" ");
d++;
if(n > 1) count(n-1);
System.out.print(d+" ");
}
public static void main(String args[])
{
count(3);
}
}
d = 1
def count(n):
global d
print(n, end=' ')
print(d, end=' ')
d += 1
if n > 1:
count(n - 1)
print(d, end=' ')
count(3)
let d = 1;
function count(n) {
process.stdout.write(n + ' ');
process.stdout.write(d + ' ');
d++;
if (n > 1) count(n - 1);
process.stdout.write(d + ' ');
}
count(3);
3 1 2 2 1 3 4 4 4
3 1 2 1 1 1 2 2 2
3 1 2 2 1 3 4
3 1 2 1 1 1 2
Question 9
Consider the following recursive C function. If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?
void get(int n) {
if (n < 1)
return;
get(n - 1);
get(n - 3);
printf("%d\n", n);
}
void get(int n) {
if (n < 1)
return;
get(n - 1);
get(n - 3);
printf("%d\n", n);
}
void get(int n)
{
if (n < 1)
return;
get(n - 1);
get(n - 3);
printd("%d", n);
}
def get(n):
if n < 1:
return
get(n - 1)
get(n - 3)
print(n)
function get(n) {
if (n < 1)
return;
get(n - 1);
get(n - 3);
console.log(n);
}
15
25
35
45
Question 10
Consider the following C function.
int fun(int n) {
int x = 1, k;
if (n == 1)
return x;
for (k = 1; k < n; ++k)
x = x + fun(k) * fun(n - k);
return x;
}
int fun (int n){
int x = 1, k;
if (n == 1)
return x;
for (k = 1; k < n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
int fun(int n) {
int x = 1;
if (n == 1)
return x;
for (int k = 1; k < n; ++k)
x = x + fun(k) * fun(n - k);
return x;
}
def fun(n):
x = 1
if n == 1:
return x
for k in range(1, n):
x += fun(k) * fun(n - k)
return x
function fun(n) {
let x = 1;
if (n === 1)
return x;
for (let k = 1; k < n; ++k)
x += fun(k) * fun(n - k);
return x;
}
The return value of fun(5) is __________.
0
26
51
71
There are 22 questions to complete.