GATE CS 2025 Set 1

65 questions · Previous year paper

The GATE 2025 Computer Science and Information Technology paper, Set 1 (forenoon session): 65 questions for 100 marks in 180 minutes, with General Aptitude (15 marks) and the technical section (85 marks). Answers follow the official final answer key; every question has a short explanation.

Take it as a timed test: ₹99

Computer Science and Information Technology

Question 61

NAT · 2 marks
#include <stdio.h>
int foo(int S[],int size){
    if(size == 0) return 0;
    if(size == 1) return 1;
    if(S[0] != S[1]) return 1+foo(S+1,size-1);
    return foo(S+1,size-1);
}
int main(){
    int A[]={0,1,2,2,2,0,0,1,1};
    printf("%d",foo(A,9));
    return 0;
}

The value printed by the given C program is _______ . (Answer in integer)

Explanation

Answer: 5

Each call compares the first two elements and then moves one place to the right. It adds 1 whenever the two neighbours differ, and the last single element adds 1 more (the size 1 case). So foo returns the number of unequal neighbour pairs plus 1, which is the number of runs of equal values.

In 0, 1, 2, 2, 2, 0, 0, 1, 1 there are 4 unequal pairs: (0, 1), (1, 2), (2, 0) and (0, 1). They split the array into the runs 0 | 1 | 2 2 2 | 0 0 | 1 1.

Value printed \(= 4 + 1 = 5\).

Answer: 5

Question 62

NAT · 2 marks

Let LIST be a datatype for an implementation of linked list defined as follows:

typedef struct list {
    int data;
    struct list *next;
} LIST;

Suppose a program has created two linked lists, L1 and L2, whose contents are given in the figure below (code for creating L1 and L2 is not provided here). L1 contains 9 nodes, and L2 contains 7 nodes.

Consider the following C program segment that modifies the list L1. The number of nodes that will be there in L1 after the execution of the code segment is ________ . (Answer in integer)

L1: 1, 7, 12, 3, 9, 5, 11, 15, 8 and L2: 1, 11, 6, 9, 15, 12, 4, each ending in NULL
int find (int query, LIST *list) {
    while (list != NULL){
        if(list->data == query) return 1;
        list = list->next;
    }
    return 0;
}
int main () {
      … … …
    ptr1=L1; ptr2=L2;
    while (ptr1->next != NULL){
        query = ptr1->next->data;
        if (find (query, L2))
            ptr1->next = ptr1->next->next;
        else ptr1 = ptr1->next;
    }
      … … …
    return 0;
}

Explanation

Answer: 5

The loop always tests the node after ptr1, so the head of L1 is never examined. If that next value is in L2, the node is unlinked and ptr1 stays where it is; otherwise ptr1 moves forward.

L2 holds {1, 11, 6, 9, 15, 12, 4}. Going through L1 after the head: 7 kept, 12 removed, 3 kept, 9 removed, 5 kept, 11 removed, 15 removed, 8 kept.

L1 becomes 1 → 7 → 3 → 5 → 8. The head node 1 stays even though 1 is also in L2.

Answer: 5

Question 63

NAT · 2 marks

Consider the following C program:

#include <stdio.h>
int gate (int n) {
    int d, t, newnum, turn;
    newnum = turn = 0; t=1;
    while (n>=t) t *= 10;
    t /=10;
    while (t>0) {
        d = n/t;
        n = n%t;
        t /= 10;
        if (turn) newnum = 10*newnum + d;
        turn = (turn + 1) % 2;
    }
    return newnum;
}
int main () {
    printf ("%d", gate(14362));
    return 0;
}

The value printed by the given C program is _______ . (Answer in integer)

Explanation

Answer: 46

The first loop multiplies t by 10 until it exceeds n, ending at \(t = 100000\); then t /= 10 gives \(t = 10000\), the place value of the leading digit. The second loop reads the digits of 14362 from left to right: 1, 4, 3, 6, 2.

turn starts at 0 and flips after every digit, so only the 2nd and 4th digits are appended: newnum becomes 4 and then \(10 \times 4 + 6 = 46\). The digits 1, 3 and 2 are skipped.

Answer: 46

Question 64

NAT · 2 marks

The maximum value of \(x\) such that the edge between the nodes B and C is included in every minimum spanning tree of the given graph is _________ . (answer in integer)

Weighted graph on A, B, C, D: AB = 7, AD = 6, CD = 8, BC = x, and crossing diagonals BD and AC with weights 1 and 3

Explanation

Answer: 5

The diagonals BD and AC (weights 1 and 3) are in every MST: BD is the unique lightest edge at D (DA = 6, DC = 8) and AC is the unique lightest edge at A (AD = 6, AB = 7). Together they form two pieces, {A, C} and {B, D}.

A spanning tree on 4 vertices has 3 edges, so exactly one more edge must join the two pieces. The candidates are BC = \(x\), AD = 6, AB = 7 and CD = 8. BC is in every MST only if it is strictly the lightest of these, i.e. \(x \lt 6\).

At \(x = 6\), BC ties with AD and {BD, AC, AD} is an MST without BC. So the largest integer value is 5.

Answer: 5

Question 65

NAT · 2 marks

In a double hashing scheme, \(h_1(k) = k \bmod 11\) and \(h_2(k) = 1 + (k \bmod 7)\) are the auxiliary hash functions. The size \(m\) of the hash table is 11. The hash function for the \(i\)-th probe in the open address table is \([h_1(k) + i\,h_2(k)] \bmod m\). The following keys are inserted in the given order: 63, 50, 25, 79, 67, 24.

The slot at which key 24 gets stored is ___________. (Answer in integer)

Explanation

Answer: 10 to 11

The first five keys go to their home slots \(h_1(k)\) with no collision: 63 → 8, 50 → 6, 25 → 3, 79 → 2, 67 → 1.

For 24: \(h_1(24) = 2\), which is taken by 79, and \(h_2(24) = 1 + (24 \bmod 7) = 4\).

  • \(i = 1\): \((2 + 4) \bmod 11 = 6\), taken by 50.
  • \(i = 2\): \((2 + 8) \bmod 11 = 10\), free.

So 24 is stored in slot 10, with slots numbered 0 to 10. The key also accepts 11, the same slot counted from 1.

Answer: 10

Questions 61 to 65 of 65