Programming and Data Structures - GATE Practice

Practice questions on C programming (pointers, functions, recursion, parameters), data structures (arrays, stacks, trees, heaps, linked lists), and algorithms for GATE Computer Science exam preparation.

28 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What is printed by the print statements in the program P1 assuming call by reference parameter passing?

Program P1(){
  x = 10;
  y = 3;
  func1(y, x, x);
  print x;
  print y;
}

func1(x, y, z) {
  y = y + 4;
  z = x + y + z;
}
  1. 10, 3
  2. 31, 3
  3. 27, 7
  4. None of the above
Question 2 Multiple Choice (Single Answer)

Consider the following declaration of a two-dimensional array in C:
Char a[100][100]
Assuming that the main memory is byte-addressable and that array is stored starting form memory address 0, the address of a [40] [50] is

  1. 4040
  2. 4050
  3. 5040
  4. 5050
Question 3 Multiple Choice (Single Answer)

The results returned by function under value-result and reference parameter passing conventions

  1. do not differ
  2. differ in the presence of loops
  3. differ in all cases
  4. may differ in the presence of exception
Question 4 Multiple Choice (Single Answer)

Consider the following three functions:

Which of the above three functions are likely to cause problems with pointers?

  1. Only P3
  2. Only P1 and P3
  3. Only P1 and P2
  4. P1, P2 and P3
Question 5 Multiple Choice (Single Answer)

The goal of structured programming is to

  1. have well indented programs
  2. be able to infer the flow of control from the compiled code
  3. be able to infer the flow of control form the program text
  4. avoid the use of GOTO statements
Question 6 Multiple Choice (Single Answer)

The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (tree height is the maximum distance of a leaf node from the root)?

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

Consider the following program
Program P2

If the language has dynamic scooping and parameters are passed by reference, what will be printed by the program?

  1. 10
  2. 11
  3. 3
  4. None of the above
Question 8 Multiple Choice (Single Answer)

The best data structure to check whether an arithmetic expression has balanced parenthesis is a

  1. queue
  2. stack
  3. tree
  4. list
Question 9 Multiple Choice (Single Answer)

Consider the following C function

int f(int n) {
  static int i = 1;
  if (n >= 5) return n;
  n = n + i;
  i++;
  return f(n);
}

The value returned by f(1) is

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

A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To which node should p point such that both the operations enQueue and deQueue can be performed in constant time?

  1. Rear node
  2. Front node
  3. Not possible with a single pointer
  4. Node next to front
Question 11 Multiple Choice (Single Answer)

The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a max Heap. The resultant max Heap is

Question 12 Multiple Choice (Single Answer)

Consider the following C program segment:char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s",p);The output of the program is

  1. gnirts
  2. string
  3. gnirt
  4. no output is printed
Question 13 Multiple Choice (Single Answer)

Consider the following C function
void swap (int a, int b)
{ int temp;
temp =a;
a =b;
b =temp;
}
In the order to exchange the values of two variables x and y .

  1. call swap (x,y)
  2. call swap (&x,&y)
  3. swap (x,y) cannot be used as it does not return any value
  4. swap (x,y) cannot be used as the parameters are passed by value
Question 14 Multiple Choice (Single Answer)

Consider the following C program

The program computers

  1. x$\div$y, using repeated subtraction
  2. x mod y using repeated subtraction
  3. the greatest common divisor of x and y
  4. the least common multiple of x only
Question 15 Multiple Choice (Single Answer)

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer.

Let n = d1 d2 ………… dm

int n, rev;
rev = 0;
while (n < 0) {
  rev = rev * 10 + n % 10;
  n = n / 10;
}

The loop invariant condition at the end of the ith iteration is

  1. n = d1d2......dm−i and rev = dm dm−1......dm−i+1
  2. n = dm−i+1.....dm−1 dm or rev = dm−i .....d2d1
  3. n $\ne$ rev
  4. n = d1d2....dm or rev = dm......d2d1
Question 16 Multiple Choice (Single Answer)

What does the following C-statement declare?

  1. A function that takes an integer pointer as argument and returns an integer
  2. A function that takes an integer pointer as argument and returns an integer pointer
  3. A pointer to a function that takes an integer pointer as argument an returns
  4. A function that takes an integer pointer as argument returns a function pointer
Question 17 Multiple Choice (Single Answer)

What does the following algorithm approximate? (Assume m>1,

  1. log m
  2. m2
  3. m1/2
  4. m1/3
Question 18 Multiple Choice (Single Answer)

A single array A [1........MAXSIZE] is used to implement two stacks.
The two stacks grow from opposite ends of the array. Variables top 1 and top 2 (top 1<top 2) point to the location of the topmost element in each of the stacks. If the space is to be used efficiently, the condition for “stack full” is

  1. (top 1= MAXSIZE/2) and (top 2 = MAXSIZE/.2+1)
  2. top 1+ top2=MAXSIZE
  3. (top 1= MAXSIZE/2) or (top2 = MAXSIZE)
  4. top 1= top 2−1
Question 19 Multiple Choice (Single Answer)

An Abstract Data type (ADT) is

  1. same as an abstract class
  2. a data type that cannot be instantiated
  3. a data type for which only the operations defined on it can be used, but none else
  4. all of the above
Question 20 Multiple Choice (Single Answer)

Assume that the operators +, -, x are left associative and $\land$ is right associative .The order of precedence (from highest to lowest) is $\land$, x, +, -. The postfix expression corresponding to the infix expression a + b x c - d $\land$ e $\land$ f is

  1. abc x +def $\land$$\land$−
  2. abc x + de$\land$f$\land$
  3. ab +c x d−e$\land$f$\land$
  4. −+ a x bc$\land$$\land$def
Question 21 Multiple Choice (Single Answer)

Post order traversal of a given binary search tree, T produces the following sequence of keys
10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29

Which one of the following sequences of keys can be the result of an in order traversal of the tree T?

  1. 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
  2. 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
  3. 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
  4. 95, 50, 60, 40, 27, 23, 22, 25, 10, 0, 15, 29
Question 22 Multiple Choice (Single Answer)

A common property of logic programming languages and functional languages is

  1. both are procedural language
  2. both are based on$\lambda$−calculus
  3. both are declarative
  4. all of the above
Question 23 Multiple Choice (Single Answer)

Consider the following C program segment

The value returned by the function Do Something when a pointer to the proof of a non-empty tree is passed as argument is

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

A program P reads in 500 integers in the range (0, 100) representing the scores of 500 students. It then prints the frequency of each score above 50. What be the best way for P to store the frequencies?

  1. An array of 50 numbers
  2. An array of 100 numbers
  3. An array of 500 numbers
  4. A dynamically allocated array of 550 numbers
Question 25 Multiple Choice (Single Answer)

Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.

  1. P-2, Q-3, R-4, S-1
  2. P-4, Q-3, R-2, S-1
  3. P-3, Q-4, R-1, S-2
  4. P-3, Q-4, R-2, S-1
Question 26 Multiple Choice (Single Answer)

Consider the following C-program:

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("%dn", sum);

  getchar();
}

What does the above program print?

  1. 8, 4, 0, 2, 14
  2. 8, 4, 0, 2, 0
  3. 2, 0, 4, 8, 14
  4. 2, 0, 4, 8, 0
Question 27 Multiple Choice (Single Answer)

Which of the following are essential features of an object-oriented programming language?

  1. Abstraction and encapsulation
  2. Strictly-typedness
  3. Type-safe property coupled with sub-type rule
  4. Polymorphism in the presence of inheritance
  1. 1 and 2 only
  2. 1 and 4 only
  3. 1, 2 and 4 only
  4. 1, 3 and 4 only
Question 28 Multiple Choice (Single Answer)

Consider the following C-program:

double foo(double); /* Line 1 */
int main() {
  double da, db;
  // input da
  db = foo(da);
}

double foo(double a) {
  return a;
}

The above code complied without any error or warning. If Line 1 is deleted, the above code will show

  1. no compile warning or error
  2. some complier-warning not leading to unitended results
  3. Some complier-warning due to type-mismatch eventually leading to unitended results
  4. Complier errors