Questions & Answers of Programming and Data Structures

Topics of Programming and Data Structures 118 Question(s) | Weightage 09 (Marks)

Consider the following C program.

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?

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++;
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) {
            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];
      char   grade ;
      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);
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--) {

Match the following:

(P) static char var,

(i) sequence of memory location to store addresses

(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;
    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.

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

Consider the folowing C program.

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.

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);
      if(n>1) count(n-1);
      printf("%d ", d);

   void main(){ 

What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?

     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;

    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: ΘN delete, O(logN) insert, O(logN) find, and ΘN 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.

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

         r : = r – y
         q : = q + 1
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 != ‘ ‘)

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(________);
      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?

Consider a binary tree T that has 200 leaf nodes. Then, the number of nodes in T that have exactly two children are _______.

While inserting the elements 71, 65, 84, 69, 67, 83 in an empty binary search tree (BST) in the sequence shown, the element in the lowest level is

The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is

Consider the following array of elements
89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100
The minimum number of interchanges needed to convert it into a max-heap is

Consider the following recursive C function.

void get (int n)
    if (n<1) return;
    get (n–1);
    get (n–3);
    printf(“%d”, n);

If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?

Consider the following C program.

int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main ( )
    int x = 1;
   x + = f1 ( ) + f2 ( ) + f3( ) + f2( );
   pirntf (“%d”, x);
   return 0;

int f1 ( ) { int x = 25; x++; return x;}
int f2 ( ) { static int x = 50; x++; return x;}
int f3 ( ) {x *= 10; return x};

The output of the program is ______.

Consider the following C program.

int main( )
    static int a[ ] = {10, 20, 30, 40, 50};
    static int *p[ ] = {a, a+3, a+4, a+1, a+2};
    int **ptr = p;
    printf(“%d%d”, ptr-p, **ptr);

The output of the program is ______.

Consider the following C program:

# include<stdio.h>
int main ( )
    int i, j, k = 0;
    j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
    k –= – –j;
    for (i = 0; i < 5: i++)
       switch (i + k)
          case 1:
          case 2: printf (“\ n%d”, i+k);
          case 3: printf (“\n%d”, i+k);
          default: printf (“\n%d”, i+k);
    return 0;

The number of time printf statement is executed is ______.