Programming and Data Structures questions — page 3 of 9

Q48

GATE CS 2024 CS-2Linear Data StructuresMSQ2 marks
Let S1 and S2 be two stacks. S1 has capacity of 4 elements. S2 has capacity of 2 elements. S1 already has 4 elements: 100, 200, 300, and 400, whereas S2 is empty, as shown below.

Only the following three operations are available:
PushToS2: Pop the top element from S1 and push it on S2.
PushToS1: Pop the top element from S2 and push it on S1.
GenerateOutput: Pop the top element from S1 and output it to the user.

Note that the pop operation is not allowed on an empty stack and the push operation is not allowed on a full stack.

Which of the following output sequences can be generated by using the above operations?
Question figure
More than one option can be correct.
Show answer without attempting

Answer: B, C, D

Q12

GATE CS 2023 CSTree Data StructuresMCQ1 mark−1/3 if wrong
Which one of the following sequences when stored in an array at locations A[1],…,A[10]A[1], \dots, A[10] forms a max-heap?
Show answer without attempting

Answer: B

Q13

GATE CS 2023 CSLinear Data StructuresMCQ1 mark−1/3 if wrong
Let SLLdel be a function that deletes a node in a singly-linked list given a pointer to the node and a pointer to the head of the list. Similarly, let DLLdel be another function that deletes a node in a doubly-linked list given a pointer to the node and a pointer to the head of the list.
Let nn denote the number of nodes in each of the linked lists. Which one of the following choices is TRUE about the worst-case time complexity of SLLdel and DLLdel?
Show answer without attempting

Answer: D

Q35

GATE CS 2023 CSC ProgrammingNAT1 mark
The integer value printed by the ANSI-C program given below is ____________.
#include<stdio.h>
int funcp(){
static int x = 1;
x++;
return x;
}
int main(){
int x,y;
x = funcp();
y = funcp()+x;
printf("%d\n", (x+y));
return 0;
}
Show answer without attempting

Answer: 7

Q36

GATE CS 2023 CSRecursionMCQ2 marks−2/3 if wrong
Consider the following program:
int main()
{
f1();
f2(2);
f3();
return(0);
}
int f1()
{
return(1);
}
int f2(int X)
{
f3();
if (X==1)
return f1();
else
return (X*f2(X-1));
}
int f3()
{
return(5);
}
Which one of the following options represents the activation tree corresponding to the main function?
Show answer without attempting

Answer: A

Q46

GATE CS 2023 CSTree Data StructuresMCQ2 marks−2/3 if wrong
Let AA be a priority queue for maintaining a set of elements. Suppose AA is implemented using a max-heap data structure. The operation EXTRACT-MAX(A) extracts and deletes the maximum element from AA. The operation INSERT(A, key) inserts a new element key in AA. The properties of a max-heap are preserved at the end of each of these operations.

When AA contains nn elements, which one of the following statements about the worst case running time of these two operations is TRUE?
Show answer without attempting

Answer: B

Q47

GATE CS 2023 CSTree Data StructuresMCQ2 marks−2/3 if wrong
Consider the C function foo and the binary tree shown.
typedef struct node {
int val;
struct node *left, *right;
} node;
int foo(node *p) {
int retval;
if (p == NULL)
return 0;
else {
retval = p->val + foo(p->left) + foo(p->right);
printf("%d ", retval);
return retval;
}
}
When foo is called with a pointer to the root node of the given binary tree, what will it print?
Question figure
Show answer without attempting

Answer: C

Q59

GATE CS 2023 CSLinear Data StructuresNAT2 marks
Consider a sequence aa of elements a0=1,a1=5,a2=7,a3=8,a4=9a_0 = 1, a_1 = 5, a_2 = 7, a_3 = 8, a_4 = 9, and a5=2a_5 = 2. The following operations are performed on a stack SS and a queue QQ, both of which are initially empty.

I: push the elements of aa from a0a_0 to a5a_5 in that order into SS.
II: enqueue the elements of aa from a0a_0 to a5a_5 in that order into QQ.
III: pop an element from SS.
IV: dequeue an element from QQ.
V: pop an element from SS.
VI: dequeue an element from QQ.
VII: dequeue an element from QQ and push the same element into SS.
VIII: Repeat operation VII three times.
IX: pop an element from SS.
X: pop an element from SS.

The top element of SS after executing the above operations is ____________.
Show answer without attempting

Answer: 8

Q15

GATE CS 2022 CSLinear Data StructuresMCQ1 mark−1/3 if wrong
Consider the problem of reversing a singly linked list. To take an example, given the linked list below,

head -> [a] -> [b] -> [c] -> [d] -> [e] -> NULL

the reversed linked list should look like

head -> [e] -> [d] -> [c] -> [b] -> [a] -> NULL

Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O(1)O(1) space?
Show answer without attempting

Answer: A

Q21

GATE CS 2022 CSC ProgrammingMCQ1 mark−1/3 if wrong
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[])
{
  int x = 1, z[2] = {10, 11};
  int *p = NULL;
  p = &x;
  *p = 10;
  p = &z[1];
  *(&z[0] + 1) += 3;
  printf("%d, %d, %d\n", x, z[0], z[1]);
  return 0;
}
Show answer without attempting

Answer: D

Q28

GATE CS 2022 CSTree Data StructuresNAT1 mark
Suppose a binary search tree with 1000 distinct elements is also a complete binary tree. The tree is stored using the array representation of binary heap trees. Assuming that the array indices start with 0, the 3rd3^{\text{rd}} largest element of the tree is stored at index_____________
Show answer without attempting

Answer: 509

Q43

GATE CS 2022 CSC ProgrammingMCQ2 marks−2/3 if wrong
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[])
{
  int a[3][3][3] =
  {{1, 2, 3, 4, 5, 6, 7, 8, 9},
   {10, 11, 12, 13, 14, 15, 16, 17, 18},
   {19, 20, 21, 22, 23, 24, 25, 26, 27}};
  int i = 0, j = 0, k = 0;
  for( i = 0; i < 3; i++ ){
    for(k = 0; k < 3; k++ )
      printf("%d ", a[i][j][k]);
    printf("\n");
  }
  return 0;
}
Show answer without attempting

Answer: A

Q44

GATE CS 2022 CSC ProgrammingMCQ2 marks−2/3 if wrong
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[]){
  char a = 'P';
  char b = 'x';
  char c = (a & b) + '*';
  char d = (a | b) - '-';
  char e = (a ^ b) + '+';
  printf("%c %c %c\n", c, d, e);
  return 0;
}
ASCII encoding for relevant characters is given below
Question figure
Show answer without attempting

Answer: A

Q62

GATE CS 2022 CSLinear Data StructuresNAT2 marks
Consider the queues Q1Q_1 containing four elements and Q2Q_2 containing none (shown as the Initial State in the figure). The only operations allowed on these two queues are Enqueue(Q, element) and Dequeue(Q). The minimum number of Enqueue operations on Q1Q_1 required to place the elements of Q1Q_1 in Q2Q_2 in reverse order (shown as the Final State in the figure) without using any additional storage is___________.
Question figure
Show answer without attempting

Answer: 0

Q10

GATE CS 2021 CS-1Tree Data StructuresMCQ1 mark−1/3 if wrong
A binary search tree TT contains nn distinct elements. What is the time complexity of picking an element in TT that is smaller than the maximum element in TT?
Show answer without attempting

Answer: D

Q21

GATE CS 2021 CS-1Linear Data StructuresNAT1 mark
Consider the following sequence of operations on an empty stack.
push(54); push(52); pop(); push(55); push(62); s = pop();
Consider the following sequence of operations on an empty queue.
enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q = dequeue();
The value of s + q is ______.
Show answer without attempting

Answer: 86

Q37

GATE CS 2021 CS-1C ProgrammingMCQ2 marks−2/3 if wrong
Consider the following ANSI C program.
#include <stdio.h>
int main()
{
    int i, j, count;
    count = 0;
    i = 0;
    for (j = -3; j <= 3; j++)
    {
        if ((j >= 0) && (i++))
            count = count + j;
    }
    count = count + i;
    printf("%d", count);
    return 0;
}
Which one of the following options is correct?
Show answer without attempting

Answer: B

Q48

GATE CS 2021 CS-1C ProgrammingNAT2 marks
Consider the following ANSI C function:
int SimpleFunction(int Y[], int n, int x)
{
  int total = Y[0], loopIndex;
  for (loopIndex = 1; loopIndex <= n - 1; loopIndex++)
       total = x * total + Y[loopIndex];
  return total;
}
Let Z be an array of 10 elements with Z[ii]=1, for all ii such that 0≤i≤90 \le i \le 9. The value returned by SimpleFunction(Z, 10, 2) is ______.
Show answer without attempting

Answer: 1023

Q2

GATE CS 2021 CS-2Tree Data StructuresMCQ1 mark−1/3 if wrong
Let HH be a binary min-heap consisting of nn elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in HH?
Show answer without attempting

Answer: C

Q10

GATE CS 2021 CS-2C ProgrammingMCQ1 mark−1/3 if wrong
Consider the following ANSI C program:
#include <stdio.h>
int main(){
    int arr[4][5];
    int i, j;
    for (i=0; i<4; i++){
        for (j=0; j<5; j++){
            arr[i][j] = 10*i + j;
        }
    }
    printf("%d", *(arr[1] + 9));
    return 0;
}
What is the output of the above program?
Show answer without attempting

Answer: C

0/200 correct0 marks

Related topics in GATE CS