Programming and Data Structures questions — page 4 of 9

Q16

GATE CS 2021 CS-2Tree Data StructuresNAT1 mark
Consider a complete binary tree with 7 nodes. Let AA denote the set of first 3 elements obtained by performing Breadth-First Search (BFS) starting from the root. Let BB denote the set of first 3 elements obtained by performing Depth-First Search (DFS) starting from the root.
The value of ∣A−B∣|A - B| is ____________.
Show answer without attempting

Answer: 1

Q23

GATE CS 2021 CS-2RecursionNAT1 mark
Consider the following ANSI C function:
int SomeFunction(int x, int y)
{
    if ((x == 1) || (y == 1)) return 1;
    if (x == y) return x;
    if (x > y) return SomeFunction(x - y, y);
    if (y > x) return SomeFunction(x, y - x);
}
The value returned by SomeFunction(15, 255) is ____________.
Show answer without attempting

Answer: 15

Q35

GATE CS 2021 CS-2C ProgrammingMCQ2 marks−2/3 if wrong
Consider the following ANSI C program:
#include <stdio.h>
#include <stdlib.h>
struct Node{
    int value;
    struct Node *next;
};
int main(){
    struct Node *boxE, *head, *boxN; int index = 0;
    boxE = head = (struct Node *) malloc(sizeof(struct Node));
    head->value = index;
    for (index = 1; index <= 3; index++){
        boxN = (struct Node *) malloc(sizeof(struct Node));
        boxE->next = boxN;
        boxN->value = index;
        boxE = boxN; }
    for (index = 0; index <= 3; index++) {
        printf("Value at index %d is %d\n", index, head->value);
        head = head->next;
        printf("Value at index %d is %d\n", index+1, head->value); }
}
Which one of the statements below is correct about the program?
Show answer without attempting

Answer: D

Q49

GATE CS 2021 CS-2RecursionNAT2 marks
Consider the following ANSI C program:
#include <stdio.h>
int foo(int x, int y, int q)
{
    if ((x <= 0) && (y <= 0))
        return q;
    if (x <= 0)
        return foo(x, y-q, q);
    if (y <= 0)
        return foo(x-q, y, q);
    return foo(x, y-q, q) + foo(x-q, y, q);
}
int main()
{
    int r = foo(15, 15, 10);
    printf("%d", r);
    return 0;
}
The output of the program upon execution is ____________.
Show answer without attempting

Answer: 60

Q5

GATE CS 2020 CSTree Data StructuresMCQ1 mark−1/3 if wrong
The preorder traversal of a binary search tree is 15, 10, 12, 11, 20, 18, 16, 19.

Which one of the following is the postorder traversal of the tree?
Show answer without attempting

Answer: B

Q16

GATE CS 2020 CSLinear Data StructuresMCQ1 mark−1/3 if wrong
What is the worst case time complexity of inserting nn elements into an empty linked list, if the linked list needs to be maintained in sorted order?
Show answer without attempting

Answer: C

Q22

GATE CS 2020 CSC ProgrammingNAT1 mark
Consider the following C program.
#include <stdio.h>
int main() {
    int a[4][5]={{1, 2, 3, 4, 5},
                 {6, 7, 8, 9, 10},
                 {11, 12, 13, 14, 15},
                 {16, 17, 18 , 19, 20}};
    printf("%d\n", *(*(a+**a+2)+3));
    return(0);
}
The output of the program is __________.
Show answer without attempting

Answer: 19

Q41

GATE CS 2020 CSTree Data StructuresMCQ2 marks−2/3 if wrong
In a balanced binary search tree with nn elements, what is the worst case time complexity of reporting all elements in range [a,b][a, b]? Assume that the number of reported elements is kk.
Show answer without attempting

Answer: B

Q46

GATE CS 2020 CSRecursionNAT2 marks
Consider the following C functions.
int fun1(int n) {
    static int i = 0;
    if (n > 0) {
        ++i;
        fun1(n-1);
    }
    return(i);
}

int fun2(int n) {
    static int i = 0;
    if (n > 0) {
        i = i + fun1(n);
        fun2(n-1);
    }
    return(i);
}
The return value of fun2(5) is __________.
Show answer without attempting

Answer: 55

Q47

GATE CS 2020 CSTree Data StructuresNAT2 marks
Consider the array representation of a binary min-heap containing 1023 elements. The minimum number of comparisons required to find the maximum in the heap is __________.
Show answer without attempting

Answer: 511

Q48

GATE CS 2020 CSC ProgrammingNAT2 marks
Consider the following C functions.
int tob(int b, int* arr) {
    int i;
    for (i=0; b>0; i++) {
        if (b%2) arr[i]=1;
        else     arr[i]=0;
        b = b/2;
    }
    return(i);
}

int pp(int a, int b) {
    int arr[20];
    int i, tot = 1, ex, len;
    ex = a;
    len = tob(b,arr);
    for (i=0; i<len; i++) {
        if (arr[i]==1)
            tot = tot * ex;
        ex = ex * ex;
    }
    return(tot);
}
The value returned by pp(3,4) is __________.
Show answer without attempting

Answer: 81

Q18

GATE CS 2019 CSC ProgrammingNAT1 mark
Consider the following C program:
#include <stdio.h>
int jumble(int x, int y){
    x = 2*x + y;
    return x;
}
int main(){
    int x=2, y=5;
    y = jumble(y,x);
    x = jumble(y,x);
    printf("%d \n", x);
    return 0;
}
The value printed by the program is __________.
Show answer without attempting

Answer: 26

Q24

GATE CS 2019 CSC ProgrammingNAT1 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 __________.
Show answer without attempting

Answer: 6

Q26

GATE CS 2019 CSRecursionMCQ2 marks−2/3 if wrong
Consider the following C function:
void convert(int n){
    if(n<0)
        printf("%d",n);
    else {
        convert(n/2);
        printf("%d",n%2);
    }
}
Which one of the following will happen when the function convert is called with any positive integer n as argument?
Show answer without attempting

Answer: D

Q27

GATE CS 2019 CSC ProgrammingMCQ2 marks−2/3 if wrong
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?
Show answer without attempting

Answer: B

Q40

GATE CS 2019 CSTree Data StructuresMCQ2 marks−2/3 if wrong
Consider the following statements:
I. The smallest element in a max-heap is always at a leaf node
II. The second largest element in a max-heap is always a child of the root node
III. A max-heap can be constructed from a binary search tree in Θ(n)\Theta(n) time
IV. A binary search tree can be constructed from a max-heap in Θ(n)\Theta(n) time

Which of the above statements are TRUE?
Show answer without attempting

Answer: A

Q52

GATE CS 2019 CSC ProgrammingNAT2 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 __________.
Show answer without attempting

Answer: 5

Q53

GATE CS 2019 CSC ProgrammingNAT2 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 __________.
Show answer without attempting

Answer: 10

Q2

GATE CS 2018 CSC ProgrammingMCQ1 mark−1/3 if wrong
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:
Show answer without attempting

Answer: A

Q3

GATE CS 2018 CSLinear Data StructuresMCQ1 mark−1/3 if wrong
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 complexity of the most time-efficient implementation of enqueue and dequeue, respectively, for this data structure?
Question figure
Show answer without attempting

Answer: B

0/200 correct0 marks

Related topics in GATE CS