GATE 2025 (Session 1)
65 questions
MCQ 30 · MSQ 13 · NAT 221-mark 30 · 2-mark 35
Questions 61–65 of 65
#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).Answer: 5
Trace the recursion on . Each element is compared with the one next to it. Add each time they are different:
Now go back up the calls: , , , , , , , .
Answer: 5
Now go back up the calls: , , , , , , , .
Answer: 5
Let LIST be a datatype for an implementation of linked list defined as follows:
L1: 1 → 7 → 12 → 3 → 9 → 5 → 11 → 15 → 8 → NULL
L2: 1 → 11 → 6 → 9 → 15 → 12 → 4 → NULL
Consider the following C program segment that modifies the list L1. The number of nodes in L1 after the execution of the code segment is _________. (Answer in integer)
typedef struct list {
int data;
struct list *next;
} LIST;
Suppose the program has created two linked lists, L1 and L2, whose contents are given below (code for creating L1 and L2 is not provided here). L1 contains 9 nodes, and L2 contains 7 nodes.L1: 1 → 7 → 12 → 3 → 9 → 5 → 11 → 15 → 8 → NULL
L2: 1 → 11 → 6 → 9 → 15 → 12 → 4 → NULL
Consider the following C program segment that modifies the list L1. The number of nodes in L1 after the execution of the code segment is _________. (Answer in integer)
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;
}
Answer: 5
L2 works as a lookup set: . L1 is .
starts at the head node of L1 (value 1). The code only tests the value of the next node, so the head is never tested and stays in the list, even though 1 is in L2. If the next value is in L2, that node is unlinked and stays where it is. Otherwise moves forward.
Trace:
Next is 7: not in L2, advance.
Next is 12: in L2, delete 12.
Next is 3: not in L2, advance.
Next is 9: in L2, delete 9.
Next is 5: not in L2, advance.
Next is 11: in L2, delete 11.
Next is 15: in L2, delete 15.
Next is 8: not in L2, advance. Now is at the last node and the loop ends.
Deleted nodes: 12, 9, 11, 15, which is 4 nodes. L1 is now , so nodes.
Answer: 5
starts at the head node of L1 (value 1). The code only tests the value of the next node, so the head is never tested and stays in the list, even though 1 is in L2. If the next value is in L2, that node is unlinked and stays where it is. Otherwise moves forward.
Trace:
Next is 7: not in L2, advance.
Next is 12: in L2, delete 12.
Next is 3: not in L2, advance.
Next is 9: in L2, delete 9.
Next is 5: not in L2, advance.
Next is 11: in L2, delete 11.
Next is 15: in L2, delete 15.
Next is 8: not in L2, advance. Now is at the last node and the loop ends.
Deleted nodes: 12, 9, 11, 15, which is 4 nodes. L1 is now , so nodes.
Answer: 5
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)Answer: 46
The function first finds how many digits has. It gets digits, using the loop that scales by 10. Then it takes the digits from the most significant to the least significant. It keeps every other digit, beginning with the 2nd digit, because begins at (skip, keep, skip, keep, ...).
Digits of in order: .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
Final value: .
Answer: 46
Digits of in order: .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
: digit is kept, so .
: digit is skipped.
Final value: .
Answer: 46
The maximum value of 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)
Answer: 5
From the figure: .
Edge BC is in every MST only if every other path from B to C has an edge heavier than . If some other path has all edges , BC can be dropped from some MST.
Other paths from B to C and their heaviest edge:
B-D-A-C: weights 1, 6, 3, heaviest 6
B-A-C: weights 7, 3, heaviest 7
B-D-C: weights 1, 8, heaviest 8
B-A-D-C: weights 7, 6, 8, heaviest 8
The best alternative has heaviest edge 6, so BC is in every MST only when .
At , BC ties with AD, and the tree BD, AC, AD (weight 10) is also an MST without BC.
For , Kruskal takes BD (1), AC (3), then BC (5), and AD (6) would form a cycle. So BC is in the only MST.
The largest integer value is 5.
Answer: 5
Edge BC is in every MST only if every other path from B to C has an edge heavier than . If some other path has all edges , BC can be dropped from some MST.
Other paths from B to C and their heaviest edge:
B-D-A-C: weights 1, 6, 3, heaviest 6
B-A-C: weights 7, 3, heaviest 7
B-D-C: weights 1, 8, heaviest 8
B-A-D-C: weights 7, 6, 8, heaviest 8
The best alternative has heaviest edge 6, so BC is in every MST only when .
At , BC ties with AD, and the tree BD, AC, AD (weight 10) is also an MST without BC.
For , Kruskal takes BD (1), AC (3), then BC (5), and AD (6) would form a cycle. So BC is in the only MST.
The largest integer value is 5.
Answer: 5
In a double hashing scheme, and are the auxiliary hash functions. The size of the hash table is 11. The hash function for the -th probe in the open address table is 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)
The slot at which key 24 gets stored is _________. (Answer in integer)
Answer: 10 to 11
Double hashing uses and . For probe the slot is , and the table size is .
Insert the keys in order. On a collision, probe again.
: , so slot (empty).
: , so slot (empty).
: , so slot (empty).
: , so slot (empty).
: , so slot (empty).
: is taken by , and . Probe : is taken by . Probe : is empty, so it goes in slot .
The official key accepts 10 to 11 (11 would be the same slot counted from 1 instead of 0).
Answer: 10
Insert the keys in order. On a collision, probe again.
: , so slot (empty).
: , so slot (empty).
: , so slot (empty).
: , so slot (empty).
: , so slot (empty).
: is taken by , and . Probe : is taken by . Probe : is empty, so it goes in slot .
The official key accepts 10 to 11 (11 would be the same slot counted from 1 instead of 0).
Answer: 10