GATE Questions & Answers of Programming and Data Structures Computer Science and Information Technology

Programming and Data Structures 129 Question(s) | Weightage 09 (Marks)

Consider the following C program:

#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 ________.

Consider the following C program:

#include <stdio.h>

int main()

int arr[]={1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip=arr+4;

printf(“%d\n”, ip[1]);

return 0;

The number that will be displayed on execution of the program 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(“%d”,r());

return 0;

}

Which one of the following values will be displayed on execution of the programs?

Consider the following statements:

I.    The smallest element in a max-heap is always at a leaf node

II.    The second largest element in a max-heap is always a child of the root node

III.    A max-heap can be constructed from a binary search tree in $\mathrm\Theta(n)$ time

IV.    A binary search tree can be constructed from a max-heap in $\mathrm\Theta(n)$ time

Which of the above statements are TRUE?

Let T be a full binary tree with 8 leaves. (A full binary tree has every level full.) Suppose two leaves $a$ and b of T are chosen uniformly and independently at random. The expected value of the distance between $a$ and b in T (i.e., the number of edges in the unique path between $a$ and b) is (rounded off to 2 decimal places) .

Consider the following C program:

#include <stdio.h>

int main(){

float sum = 0.0, j = 1.0, i = 2.0;

while (i/j > 0.0625){

j = j + j;

sum = sum + i/j;

printf("%f\n", sum);

}

return 0;

}

The number of times the variable sum will be printed, when the above program is executed, is _________________.

Consider the following C program:

#include <stdio.h>

int main()

{

int a[ ] = {2, 4, 6, 8, 10};

int i, sum = 0, *b = a + 4;

for (i = 0; i < 5; i++)

sum = sum + (*b – i) – *(b – i);

printf (“%d\n”, sum);

return 0;

}

The output of the above C program is __________.

Consider the following C program.

#include<stdio.h>
struct Ournode{
char x,y,z;
};
int main(){
struct Ournode p = {'1', '0', 'a'+2};
struct Ournode *q = &p;
printf ("%c, %c", *((char*)q+1), *((char*)q+2));
return 0;
}
The output of this program is:

A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let enqueue be implemented by inserting a new node at the head, and dequeue be implemented by deletion of a node from the tail.
Which one of the following is the time complexity of the most time-efficient implementation of enqueue and dequeue, respectively, for this data structure?

The postorder traversal of a binary tree is 8,9,6,7,4,5,2,3,1. The inorder traversal of the same tree is 8,6,9,4,7,2,5,1,3. The height of a tree is the length of the longest path from the root to any leaf. The height of the binary tree above is ______.

Consider the following C program:

#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 _____.

Consider the following C program:

#include<stdio.h>

void fun1(char *s1, char *s2){
char *tmp;
tmp = s1;
s1 = s2;
s2 = tmp;
}
void fun2(char **s1, char **s2){
char *tmp;
tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
int main(){
char *str1 = "Hi", *str2 = "Bye";
fun1(str1, str2); printf("%s %s ", str1, str2);
fun2(&str1, &str2); printf("%s %s", str1, str2);
return 0;
}
The output of the program above is

Consider the following C code. Assume that unsigned long int type length is 64 bits.

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 240 is

Consider the following program written in pseudo-code. Assume that x and y are integers.

Count(x,y) {
if (y != 1){
if (x != 1) {
print("*");
Count(x/2, y);
}
else {
y = y-1;
Count(1024, y);
}
}
}

The number of times that the print statement is executed by the call Count(1024,1024) is _____.

The number of possible min-heaps containing each value from {1, 2, 3, 4, 5, 6, 7}exactly once is _____.

Let T be a binary search tree with 15 nodes. The minimum and maximum possible heights of T are:
Note: The height of a tree with a single node is 0

Consider the C code fragment  given below.

typedef  struct  node  {
int   data;
node*  next ;
} node ;
void   join (node* m,   node*  n)  {
node*   p = n ;
while (p->next   != NULL)  {
p = p->next;
}
p->next   =  m;
}

Assuming  that m and n  point to valid  NULL- terminated linked lists, invocation of join will

Consider C struct defined below:

struct   data    {

int    marks    [100];
int     cnumber;
};
struct   data  student ;

The base address of  student is available in register R1. The filed  student. grade  can be accessed efficiently using

Consider the following C code:

#include  <stdio . h>
int  *assignval  (int  *x, int  val)  {
*x  =  val;
return  x;
}
void main  ()  {
int  *x  =  malloc (sizeof (int));
if  (NULL  ==  x)  return;
x  =  assignval  (x ,0);
if  (x)   {
x  =  (int  *)   malloc (sizeof  (int));
if  (NULL ==  x)  return;
x  =  assignval  (x,10) ;
}
printf ("%d\n" ,  *x) ;
free  (x) ;
}

The code suffers from which one of the following problems:

Let T be a tree with 10 vertices. The sum of the degrees of all the vertices in T is ____________ .

Consider the following two functions.

 void fun1 (int n) {         if (n == 0) return;         printf ("%d", n);         fun2 (n - 2);         printf ("%d", n); } void fun2 (int n) {         if (n == 0) return;         printf ("%d", n);         fun1 (++n);         printf ("%d", n); }

The output printed when fun1 (5) is called  is

Consider the C function 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) wil result in :

Consider the following C program

#include <stdio.h>
#include <string.h>

void printlength (char *s, char *t) {
unsigned int c=0;
int len = ((strlen(s) - strlen(t)) > c) ? strlen(s): strlen(t);
printf("%d\n",len);
}

void main () {
char *x = "abc",
char *y = "defgh";
printlength (x,y);
}
Rcall that strlen is defined in string. h as returning a value of type size_t, which is an unsigned int. The output of the program is ______.

The output of executing the following C program is ____________ .

#include <stdio.h>

int total (int v) {
static int count = 0;
while(v) {
count += v&1;
v >>= 1;
}
return count ;
}

void main () {
static int x = 0;
int i =5;
for (; i>0;i--) {
x=x+total(i);
}
printf("%d\n",x);
}

Match the following:

 (P) static char var, (i) sequence of memory location to store addresses (Q)m=malloc(10);    m=NULL; (ii) A variable located in data section of memory (R) char *ptr[10]; (iii) Request to allocate a CPU register to store data (s)register int varl; (iv) A lost memory which cannot be free

A circular queue has been implemented using a singly linked list where each node consist of a value and a single pointer pointing to the next node. We maintain exactly two external pointers FRONT and REAR pointing to the front node and rear node of the queue, respectively. Which of the following statement is/ are CORRECT for such a circular queue, so that insertion and deletion operations can be performed in (1) time?

I. Next pointer of front node point to the rear node.

II. Next pointer of rear node points to the front node.

Consider the following function implemented in C:
void printxy(int x, int y) {
int *ptr;
x=0;
ptr=&x;
y=*ptr;
*ptr=1;
printf(“%d, %d”, x, y);
}
The output of invoking printxy(1,1) is

The pre-order traversal of a binary search tree is given by 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20. Then the post-order traversal of this tree is:

Consider the C program fragment below which is meant to divide x by y using repeated subtraction. The variables x, y, q and r are all unsigned int.

while (r >=y) {
r = r - y;
q = q + 1;
}
Which of the  following condition on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r)?

Consider the following snippet of a C program. Assume that swap(&x, &y) exchanges the contents of x and y.

int main () {
int array[] = {3,5,1,4,6,2};
int done = 0;
int i;

while  (done == 0) {
done = 1;
for (i = 0, i<=4; i++) {
if (array[i]< array[i+1]) {
swap (&array[i], &array[i+1]);
done = 0;
}
}
for (i=5; i>=1; i--) {
if (array[i] > array [i-1]) {
swap(&array[i], &array[i-1]);
done = 0;
}
}
printf("%d", array[3]);
}
The output of the program is ________.

Consider the following C program.

#include<stdio.h>
int main () {
int m = 10;
int n, nl;
n = ++m;
n1 = m++;
n--;
--n1;
n-= n1;
printf ("%d",n),
return 0;
}
The output of the program is ___________.

Consider the folowing C program.

#include<stidio.h>
#include<string.h>
int main () {
char* c = "GATECSIT2017";
char* p = c;
printf ("%d", (int)strlen (c+2[p]-6[p]-1));
return 0;
}
The output of the program is ____________.

A queue is implemented using an array such that ENQUEUE and DEQUEUE operations are performed efficiently. Which one of the following statements is CORRECT (n refers to the number of items in the queue)?

Consider the following C program.

void f(int, short);
void main()
{
int i = 100;
short s = 12;
short *p = &s;
__________ ; // call to f()
}

Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?

Consider the following C program.

#include<stdio.h>
void mystery(int *ptra, int *ptrb)
{
int *temp;
temp = ptrb;
ptrb = ptra;
ptra = temp;
}
int main()
{
int a=2016, b=0, c=4, d=42;
mystery(&a, &b);
if (a < c)
mystery(&c, &a);
mystery(&a, &d);
printf("%d\n", a);
}

The output of the program is _________ .

The following function computes the maximum value contained in an integer array p[] of size

n (n >= 1).

int max(int *p, int n) {
int a=0, b=n-1;

while (__________) {
if (p[a] <= p[b]) { a = a+1; }
else              { b = b-1; }
}
return p[a];
}

The missing loop condition is

What will be the output of the following C program?

void count(int n){
static int d=1;
printf("%d ", n);
printf("%d ", d);
d++;
if(n>1) count(n-1);
printf("%d ", d);
}

void main(){
count(3);
}

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);}

An operator delete (i) for a binary heap data structure is to be designed to delete the item in the $i$-th node. Assume that the heap is implemented in an array and i refers to the i-th index of the array. If the heap tree has depth d (number of edges on the path from the root to the farthest leaf), tthen what is the time complexity to re-fix the heap efficiently after the removal of the element?

Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns the element at the head of the queue Q without removing it from Q. Similarly Top(S) returns the element at the top of S without removing it from S. Consider the algorithm given below.

The maximum possible number of iterations of the while loop in the algorithm is____________ .

The attributes of three arithmetic operators in some programming language are given below.

Operator Precedence Associativity Arity
+ High Left Binary
- Medium Right Binary
* Low Left Binary

The value of the expression 2−5+1−7∗3 in this language is .

The value printed by the following program is __________.

void f(int* p, int m){
m = m + 5;
*p = *p + m;
return;
}

void main(){
int i=5, j=10;

f(&i, j);
printf("%d", i+j);
}

N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed.

An algorithm performs the following operations on the list in this order: $\mathrm{\Theta }\left(N\right)$ delete, O(logN) insert, O(logN) find, and $\mathrm{\Theta }\left(N\right)$ decrease-key. What is the time complexity of all these operations put together?

A complete binary min-heap is made by including each integer in [1;1023] exactly once. The depth of a node in the heap is the length of the path from the root of the heap to that node. Thus, the root is at depth 0. The maximum depth at which integer 9 can appear is _________.

The following function computes $X^Y$ for positive integers X and Y.
int exp(int X, int Y) {
int res = 1, a = X, b = Y;

while ( b != 0 ){
if ( b%2 == 0) { a = a*a; b = b/2; }
else           { res = res*a; b = b-1; }
}
return res;
}
Which one of the following conditions is TRUE before every iteration of the loop?

Consider the following New-order strategy for traversing a binary tree:
$\bullet$Visit the root;
$\bullet$Visit the right subtree using New-order;
$\bullet$Visit the left subtree using New-order;
The New-order traversal of the expression tree corresponding to the reverse polish expression
3 4 * 5 - 2 ˆ 6 7 * 1 + - is given by:

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 _________.

The given diagram shows the flowchart for a recursive function A(n). Assume that all statements, except for the recursive calls, have O(1) time complexity. If the worst case time complexity of this function is $\style{font-family:'Times New Roman'}{\mathit0(n^\alpha)}$, then the least possible value (accurate up to two decimal positions) of $\alpha$ is_________ .

The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is ___________.

Note: The height of a tree with a single node is 0.

In an adjacency list representation of an undirected simple graph G = (V;E), each edge (u;v) has two adjacency list entries: [v] in the adjacency list of u, and [u] in the adjacency list of v. These are called twins of each other. A twin pointer is a pointer from an adjacency list entry to its twin. If $\left|E\right|$ = m and $\left|V\right|$ = n, and the memory size is not a constraint, what is the time complexity of the most efficient algorithm to set the twin pointer in each entry in each adjacency list?

The height of a tree is the length of the longest root-to-leaf path in it. The maximum and minimum number of nodes in a binary tree of height 5 are

Which of the following is/are correct inorder traversal sequence(s) of binary search tree(s)?

 I. 3, 5, 7, 8, 15, 19, 25 II. 5, 8, 9, 12, 10, 15, 25 III. 2, 7, 10, 8, 14, 16, 20 IV. 4, 6, 7, 9, 18, 20, 25

The output of the following C program is ______.
void f1(int a, int b)
{
int c;
c=a; a=b; b=c;
}
void f2(int *a, int *b)
{
int c;
c=*a; *a=*b; *b=c;
}
int main ( )
{
int a=4, b=5, c=6;
f1(a, b);
f2(&b, &c);
printf(“%d”, c–a–b);
}

What are the worst-case complexities of insertion and deletion of a key in a binary search tree?

Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4.

 Array Index 1 2 3 4 5 6 7 8 9 Value 40 30 20 10 15 16 17 8 4

Now consider that a value 35 is inserted into this heap. After insertion, the new heap is

What is the output of the following C code? Assume that the address of $x$ is 2000 (in decimal) and an integer requires four bytes of memory?

int main( )
{
unsigened int x[4][3] =
{(1,2,3), {4,5,6}, {7, 8, 9}, {10, 11, 12}};
printf(“%u, %u, %u”, x+3, *(x+3),*(x+2)+3);
}

Consider the following pseudo code, where x and y are positive integers.

begin
q: = 0
r : = x
while r
y do

being
r : = r – y
q : = q + 1
end
end

The post condition that needs to be satisfied after the program terminates is

Consider the following function written in the C programming languages.
void foo(char *a)
{
if(*a && *a != ‘ ‘)
{
foo(a+1);
putchar(*a);
}
}

The output of the above function on input “ABCD EFGH” is

Consider a complete binary tree where the left and the right subtrees of the root are max-heaps. The lower bound for the number operations to convert the tree to a heap is

A binary tree T has 20 leaves. The number of nodes in T having two children is___________

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;
}

The return value of fun(5) is _____.

Suppose you are provided with the following function declaration in the C programming language.

int partition(int a[ ], int n);

The function treats the first element of a[ ] as s pivot, and rearranges the array so that all elements less than or equal to the pivot is in the left part of the array, and all elements greater than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last element of the left part. The return value is the number of elements in the left part.
The following partially given function in the C programming language is used to find the kth smallest element in an array a[ ] of size n using the partition function. We assume $k\leq n.$

int kth_smallest(int a[ ], int n, int k)
{
int left_end = partition(a, n);
if (left_end+1 = =k)
{
Return a[left_end];
}
if(left_end+1) > k)
{
return kth_smallest(________);
}
else
{
return kth_smallest(________);
}
}

The missing argument lists are respectively

Consider the C program below.
#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)+stkFun(1,0);
}

The value printed by the above program is ______.

Consider the following C program segment.

#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?