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.
Questions
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;
}
- 10, 3
- 31, 3
- 27, 7
- None of the above
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
- 4040
- 4050
- 5040
- 5050
The results returned by function under value-result and reference parameter passing conventions
- do not differ
- differ in the presence of loops
- differ in all cases
- may differ in the presence of exception
Consider the following three functions:

Which of the above three functions are likely to cause problems with pointers?
- Only P3
- Only P1 and P3
- Only P1 and P2
- P1, P2 and P3
The goal of structured programming is to
- have well indented programs
- be able to infer the flow of control from the compiled code
- be able to infer the flow of control form the program text
- avoid the use of GOTO statements
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)?
- 2
- 3
- 4
- 6
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?
- 10
- 11
- 3
- None of the above
The best data structure to check whether an arithmetic expression has balanced parenthesis is a
- queue
- stack
- tree
- list
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
- 5
- 6
- 7
- 8
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?

- Rear node
- Front node
- Not possible with a single pointer
- Node next to front
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
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
- gnirts
- string
- gnirt
- no output is printed
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 .
- call swap (x,y)
- call swap (&x,&y)
- swap (x,y) cannot be used as it does not return any value
- swap (x,y) cannot be used as the parameters are passed by value
Consider the following C program

The program computers
- x$\div$y, using repeated subtraction
- x mod y using repeated subtraction
- the greatest common divisor of x and y
- the least common multiple of x only
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
- n = d1d2......dm−i and rev = dm dm−1......dm−i+1
- n = dm−i+1.....dm−1 dm or rev = dm−i .....d2d1
- n $\ne$ rev
- n = d1d2....dm or rev = dm......d2d1
What does the following C-statement declare?
- A function that takes an integer pointer as argument and returns an integer
- A function that takes an integer pointer as argument and returns an integer pointer
- A pointer to a function that takes an integer pointer as argument an returns
- A function that takes an integer pointer as argument returns a function pointer
What does the following algorithm approximate? (Assume m>1,

- log m
- m2
- m1/2
- m1/3
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
- (top 1= MAXSIZE/2) and (top 2 = MAXSIZE/.2+1)
- top 1+ top2=MAXSIZE
- (top 1= MAXSIZE/2) or (top2 = MAXSIZE)
- top 1= top 2−1
An Abstract Data type (ADT) is
- same as an abstract class
- a data type that cannot be instantiated
- a data type for which only the operations defined on it can be used, but none else
- all of the above
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
- abc x +def $\land$$\land$−
- abc x + de$\land$f$\land$
- ab +c x d−e$\land$f$\land$
- −+ a x bc$\land$$\land$def
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?
- 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
- 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
- 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
- 95, 50, 60, 40, 27, 23, 22, 25, 10, 0, 15, 29
A common property of logic programming languages and functional languages is
- both are procedural language
- both are based on$\lambda$−calculus
- both are declarative
- all of the above
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
- the number of leaf nodes in the tree
- the number of nodes in the tree
- the number of internal nodes in the tree
- the height of the tree
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?
- An array of 50 numbers
- An array of 100 numbers
- An array of 500 numbers
- A dynamically allocated array of 550 numbers
Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.

- P-2, Q-3, R-4, S-1
- P-4, Q-3, R-2, S-1
- P-3, Q-4, R-1, S-2
- P-3, Q-4, R-2, S-1
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?
- 8, 4, 0, 2, 14
- 8, 4, 0, 2, 0
- 2, 0, 4, 8, 14
- 2, 0, 4, 8, 0
Which of the following are essential features of an object-oriented programming language?
- Abstraction and encapsulation
- Strictly-typedness
- Type-safe property coupled with sub-type rule
- Polymorphism in the presence of inheritance
- 1 and 2 only
- 1 and 4 only
- 1, 2 and 4 only
- 1, 3 and 4 only
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
- no compile warning or error
- some complier-warning not leading to unitended results
- Some complier-warning due to type-mismatch eventually leading to unitended results
- Complier errors














