Skip to content

GATE CS Programming and Data Structures questions

Practise Programming and Data Structures questions for GATE CS. Explore official PYQs and original subject-wise quizzes, check answers and view available solutions. Free, no sign-in.

Subject, topic, year and type filter the full library. Match any choice within a filter, and all selected filter categories. Search, source and sort apply to this page.

20 on this page
Official PYQ2019Programming and Data StructuresProgramming in CNAT · 1 mark
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 _______
Official PYQ2019Programming and Data StructuresProgramming in CMCQ · 2 marks
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?
Official PYQ2019Programming and Data StructuresBinary HeapsMCQ · 2 marks
Consider the following statements: The smallest element in a max-heap is always at a leaf node The second largest element in a max-heap is always a child of a root node A max-heap can be constructed from a binary search tree in Θ(n)\Theta(n) time A binary search tree can be constructed from a max-heap in Θ(n)\Theta(n) time Which of the above statements are …
Official PYQ2019Programming and Data StructuresTreesNAT · 2 marks
Let TT be a full binary tree with 88 leaves. (A full binary tree has every level full.) Suppose two leaves aa and bb of TT are chosen uniformly and independently at random. The expected value of the distance between aa and bb in TT (ie., the number of edges in the unique path between aa and bb) is (rounded off to 22 decimal …
Official PYQ2019Programming and Data StructuresProgramming in CNAT · 2 marks
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 _________
Official PYQ2019Programming and Data StructuresProgramming in CNAT · 2 marks
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 _______
Official PYQ2018Programming and Data StructuresBinary HeapsNAT · 2 marks
The number of possible min-heaps containing each value from {1,2,3,4,5,6,7}\{1,2,3,4,5,6,7\} exactly once is _______
Official PYQ2018Programming and Data StructuresProgramming in CMCQ · 2 marks
Consider the following C code. Assume that unsigned long int type length is 6464 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 2402^{40} is:
Official PYQ2018Programming and Data StructuresProgramming in CMCQ · 2 marks
#include<stdio.h> void fun1(char* s1, char* s2){ char* temp; temp = s1; s1 = s2; s2 = temp; } void fun2(char** s1, char** s2){ char* temp; temp = *s1; *s1 = *s2; *s2 = temp; } 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 …
Official PYQ2018Programming and Data StructuresTreesNAT · 1 mark
The postorder traversal of a binary tree is 8, 9, 6, 7, 4, 5, 2, 3, 1\text{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{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 _____
Official PYQ2018Programming and Data StructuresRecursionNAT · 1 mark
Consider the following C\text{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 ______.
Official PYQ2018Programming and Data StructuresArrays, Stacks and QueuesMCQ · 1 mark
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 nn 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 …
Official PYQ2018Programming and Data StructuresProgramming in CMCQ · 1 mark
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:
Official PYQ2017Programming and Data StructuresLinked ListsMCQ · 1 mark
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
Official PYQ2017Programming and Data StructuresProgramming in CNAT · 1 mark
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); }
Official PYQ2017Programming and Data StructuresProgramming in CNAT · 1 mark
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); } Recall that strlenstrlen is defined in string.hstring.h as …
Official PYQ2017Programming and Data StructuresRecursionMCQ · 1 mark
Consider the C functions 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)foo(3) and bar(3)bar(3) will result in:
Official PYQ2017Programming and Data StructuresRecursionMCQ · 1 mark
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)\text{fun1}(5) is called is
Official PYQ2017Programming and Data StructuresTreesNAT · 1 mark
Let TT be a tree with 1010 vertices. The sum of the degrees of all the vertices in TT is ________
Official PYQ2017Programming and Data StructuresProgramming in CMCQ · 1 mark
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 …