Test 1 - Programming and Data Structure | Computer Science

GATE Previous year Topic Wise Questions and Answers | Programming & Data Structure

28 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Consider the following C function.
float f,(float x, int y) {
float p, s; int i;
for (s=1,p=1,i=1; i

  1. Xy
  2. ex
  3. In (1+x)
  4. Xx
Question 2 Multiple Choice (Single Answer)

Assume the following C variable declaration

int * A[10], B[10][10]; of the following expressions

I. A[2]
II. A[2][3]
III. B[1] IV. B[2][3]

Which will not give compile-time errors if used as left hand sides of assignment statements in a C program?

  1. I,II, and IV only
  2. II, III, and IV only
  3. II and IV only
  4. IV only
Question 3 Multiple Choice (Single Answer)

Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree?

  1. 7 5 1 0 3 2 4 6 8 9
  2. 0 2 4 3 1 6 5 9 8 7
  3. 0 1 2 3 4 5 6 7 8 9
  4. 9 8 6 4 2 3 0 1 5 7
Question 4 Multiple Choice (Single Answer)

Let T(n) be the number of different binary search trees on n distinct elements.
Then T(n) = $\sum_{k -1}^n T(k -1)(x) $, where x is

  1. n - k + 1
  2. n - k
  3. n - k - 1
  4. n - k - 2
Question 5 Multiple Choice (Single Answer)

Consider the following 2-3-4 tree (i.e., B-tree with a minimum degree of two) in which each data item is a letter. The usual alphabetical ordering of letters is used in constructing the tree.

What is the result of inserting G in the above tree?

  1. None of the above
Question 6 Multiple Choice (Single Answer)

A data structure is required for storing a set of integers such that each of the following operations can be done in O (log n) time, where n is the number of elements in the set.
I. Deletion of the smallest element
II. Insertion of an element if it is not already present in the set
Which of the following data structures can be used for this purpose?

  1. A heap can be used but not a balanced binary search tree
  2. A balanced binary search tree can be used but not a heap
  3. Both balanced binary search tree and heap can be used
  4. Neither balanced binary search tree nor heap can be used
Question 7 Multiple Choice (Single Answer)

Let S be a stack of size n $\ge$1. Starting with the empty stack, suppose we push the first n natural numbers in sequence, and then perform n pop operations Assume that Push and Pop operations take X seconds each, and Y seconds elapse between the end of one such stack operation and the start of the next operation. For m $\ge$1, define the stack-life of m as the time elapsed from the end of Push(m) to the start of the pop operation that removes m from S. The average stack-life of an element of this stack is

  1. n(X + Y)
  2. 3Y + 2X
  3. n(X + Y) - X
  4. Y + 2X
Question 8 Multiple Choice (Single Answer)

Consider the C program shown below.

#include < stdio.h > 
#define print(x) print f(” % d“, x)
int x;
void Q(int z) {
  z + = x;
  print(z);
}
void P(int * y) {
  int x = * y + 2;
  Q(x); * y = x - 1;
  Print(x);
}
Main(void) {
  x = 5;
  P( & x)
  Print(x);
}

The output of this program is

  1. 12 7 6
  2. 22 12 11
  3. 14 6 6
  4. 7 6 6
Question 9 Multiple Choice (Single Answer)

Consider the function f defined below.

struct item {
  int data;
  struct item * next;
};
int f(struct item * p) {
  return ((p == NULL) || (p - > next == NULL) ||
    ((p - > data <= p - > next - > data) & amp; & amp; f(p - > next)));
}

For a given linked list p, the function f returns 1 if and only if

  1. the list is empty or has exactly one element
  2. the elements in the list are sorted in non-decreasing order of data value
  3. the elements in the list are sorted in non-increasing order of data value
  4. not all elements in the list have the same data value
Question 10 Multiple Choice (Single Answer)

Consider these two functions and two statements S1 and S2 about them.

S2: All the transformations applied to work1 to get work2 will always improve the performance (i.e reduce CPU time) of work2 compared to work1

  1. S1 is false and S2 is false
  2. S1 is false and S2 is true
  3. S1 is true and S2 is false
  4. S1 is true and S2 is true
Question 11 Multiple Choice (Single Answer)

Consider the following C-function in which a [n] and b [m] are two sorted integer
arrays and c [n + m] be another integer array.

void xyz(int a[], int b[], int c[]) {
  int i, j, k;
  i = j = k = 0;
  while ((i < n) && (j < m))
    if (a[i] < b[j]) c[k++] = a[i++];
    else c[k++] = b[j++];
}

Which of the following condition(s) hold(s) after the termination of the while loop?

  1. only (i)
  2. only (ii)
  3. either (i) or (ii) but not both
  4. neither (i) nor (ii)
Question 12 Multiple Choice (Single Answer)

A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4]location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.

Which one of the following is a valid sequence of elements in an array representing 3-ary max heap?

  1. 1, 3, 5, 6, 8, 9
  2. 9, 6, 3, 1, 8, 5
  3. 9, 3, 6, 8, 5, 1
  4. 9, 5, 6, 8, 3, 1
Question 13 Multiple Choice (Single Answer)

Consider this C code to swap two integers and these five statements: the code.

S1: will generate a compilation error
S2: may generate a segmentation fault at runtime depending on the arguments passed
S3: correctly implements the swap procedure for all input pointers referring to integers stored in
memory locations accessible to the process
S4: implements the swap procedure correctly for some but not all valid input pointers
S5: may add or subtract integers and pointers.

  1. S1
  2. S2 and S3
  3. S2 and S4
  4. S2 and S5
Question 14 Multiple Choice (Single Answer)

A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4]location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.

Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3- ary max heap found in the above question, Q.76. Which one of the following is the sequence of items in the array representing the resultant heap?

  1. 10, 7, 9, 8, 3, 1, 5, 2, 6, 4
  2. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  3. 10, 9, 4, 5, 7, 6, 8, 2, 1, 3
  4. 10, 8, 6, 9, 7, 2, 3, 4, 1, 5
Question 15 Multiple Choice (Single Answer)

Consider the following C function:

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

  1. 5
  2. 7
  3. 9
  4. 18
Question 16 Multiple Choice (Single Answer)

In the following C program fragment, j, k, n and TwoLog_n are integer variables, and A is an array of
integers. The variable n is initialized to an integer $\ge$3, and TwoLog_n is initialized to the value of 2
*$\lceil log_2(n) \rceil$

for (k = 3; k & lt; = n; k++)
  A[k] = 0;
for (k = 2; k & lt; = TwoLog_n; k++)
  for (j = k + 1; j & lt; = n; j++)
    A[j] = A[j] || (j % k);
for (j = 3; j & lt; = n; j++)
  if (!A[j]) print f(” % d”, j);

The set of numbers printed by this program fragment is

  1. {mm $\ge$ n, ($\ge$i)[m=i!] }
  2. {mm $\ge$ n, ($\exists $i)[m=i2]}
  3. {mm $\le $ n, m is prime}
  4. { }
Question 17 Multiple Choice (Single Answer)

The following postfix expression with single digit operands is evaluated using a stack:
8 2 3 $\land$/ 2 3 * + 5 1 * -

Note that $\land$ is the exponentiation operator. The top two elements of the stack after the first * is evaluated are:

  1. 6, 1
  2. 5, 7
  3. 3, 2
  4. 1, 5
Question 18 Multiple Choice (Single Answer)

Consider the following segment of C-code:

int j, n;
    j = 1;
   while (j &lt;= n)
         j = j*2

The number of comparisons made in the execution of the loop for any n > 0 is:

  1. [log2 n] + 1
  2. n
  3. [log2 n]
  4. [log2 n] + 2
Question 19 Multiple Choice (Single Answer)

Consider the program below:

The value printed is

  1. 6
  2. 8
  3. 14
  4. 15
Question 20 Multiple Choice (Single Answer)

Consider the following C program segment where CellNode represents a node in a binary tree:

Struct Cell Node {
  Struct Cell Node * left child;
  Int element;
  Struct Cell Node * right Child;
};
Int Get Value(struct Cell Node * ptr) {
  Int Value = 0;
  if (ptr! = NULL)
    if ((ptr - > left child == NULL) &&
      (ptr - > right Child == NULL))
      Value = 1;
    else
      Value = value + GetValue(ptr - > left Child) + Get Value(ptr - > right child);
}
return (value);
}

The value returned by GetValue when a pointer to the root of a binary tree is passed as its argument is:

  1. the number of nodes in the tree
  2. the number of internal nodes in the tree
  3. the number of leaf nodes in the tree
  4. the height of the tree
Question 21 Multiple Choice (Single Answer)

What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a single node is 0.

  1. 2
  2. 3
  3. 4
  4. 5
Question 22 Multiple Choice (Single Answer)

Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression?
a = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z)

  1. x = 3, y = 4, z = 2
  2. x = 6, y = 5, z = 3
  3. x = 6, y = 3, z = 5
  4. x = 5, y = 4, z = 5
Question 23 Multiple Choice (Single Answer)

Consider a binary max-heap implemented using an array.

Which one of the following array represents a binary max-heap?

  1. {25,12,16,13,10,8,14}
  2. {25,14,13,16,10,8,12}
  3. {25,14,16,13,10,8,12}
  4. {25,14,12,13,10,8,16}
Question 24 Multiple Choice (Single Answer)

Consider a binary max-heap implemented using an array.

What is the content of the array after two delete operations on the correct answer to the previous question?

  1. {14,13,12,10,8}
  2. {14,12,13,8,10}
  3. {14,13,8,12,10}
  4. {14,13,12,8,10}
Question 25 Multiple Choice (Single Answer)

Choose the correct option to fill ?1 and ?2 so that the program below prints an input string in reverse
order. Assume that the input string is terminated by a newline character.

Void reverse(void) {
  int c;
  if ( ?1) reverse(); ?2
}
Main() {
  Print f(“Enter Text”);
  printf(“\n”);
  Reverse();
  printf(“\n”);
}
  1. ?1 is (getchar() ! = '')
    ?2 is getchar(c);
  2. ?1 is (c = getchar()) ! = '')
    ?2 is getchar (c);
  3. ?1 is (c != '')
    ?2 is putchar(c);
  4. ?1 is ((c = getchar) )) ! = '')
    ?2 is putchar (c);
Question 26 Multiple Choice (Single Answer)

An implementation of a queue Q, using two stacks S1 and S2, is given below:

void insert(Q, x) {
  push(S1, x);
}
void delete(Q) {
  if (stack - empty(S2)) then
  if (stack - empty(S1)) then {
    print(“Q is empty”);
    return;
  } else
    while (!(stack - empty(S1))) {
      x = pop(S1);
      push(S2, x);
    }
  x = pop(S2);
}

Let n insert and m ($\le$ n) delete operations be performed in an arbitrary order on an empty queue Q. Let
x and y be the number of push and pop operations performed respectively in the process. Which one of
the following is true for all m and n?

  1. n + m $\le$ x < 2n and 2m $\le$ y $\le$ n + m
  2. n + m $\le$ x < 2n and 2m $\le$ y $\le$ 2n
  3. 2m $\le$ x < 2n and 2m $\le$ y $\le$ n + m
  4. 2m $\le$ x < 2n and 2m $\le$ y $\le$ 2n
Question 27 Multiple Choice (Single Answer)

The following C function takes a single-linked list of integers as a parameter and rearranges the
elements of the list. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given
order. What will be the contents of the list after the function completes execution?

struct node {
  int value;
  struct node * next;
};
Void rearrange struct node * list {
  struct node * p, * q;
  int temp;
  if (!list || !!list - > ; next) return;
  p = list; | q = list - > ;
  next;
  while q {
    temp = p - > ;
    value;
    p - > ;
    value = q - > ;
    value;
    q - > ;
    value = temp;
    p = q - > ;
    next;
    q = p ? p - > ;
    next: 0;
  }
}
  1. 1,2,3, 4,5, 6, 7
  2. 2,1, 4,3, 6,5, 7
  3. 1,3, 2,5, 4, 7, 6
  4. 2, 3, 4,5, 6, 7,1
Question 28 Multiple Choice (Single Answer)

What is printed by the following C program?

int f(int x, int *py, int **ppz) void main() {
  {
    Int y, z;
    int c, *b, **a; **ppz + = 1;
    z = *ppz c = 4;
    b = &c;
    a = &b; 
   *py + = 2;
    y = *py;
    printf(“ % d”, f(c, b, a));
    x + = 3;
  }
  return x + y + z;
}
``
  1. 18
  2. 19
  3. 21
  4. 22