chapter

    Programming and Data Structures Practice Questions for Gate Cs

    GATE CS Programming and Data Structures: 7 chapters, 66 previous year questions (100% of Programming and Data Structures), 1831 practice questions and one sol

    A question from this chapter

    Question 1
    Level 3: Exam Standard

    Consider the following two statements regarding ANSI C evaluation:

    \textbf{Assertion (A):} The decimal value of the expression (char)( ('~' | 'a') + 50 ) is .

    \textbf{Reason (R):} The bitwise OR of '~' (126) and 'a' (97) yields 127. Adding 50 gives 177, which exceeds the maximum signed char value of 127. The explicit (char) cast truncates the integer result to 8 bits, wrapping around to due to two's complement representation.

    Which one of the following options is correct?

    Question 2
    Level 3: Exam Standard

    Consider the character array char s[30] = "GATE2024";. A programmer wishes to transform this string into "GATEGATE20242024" by performing in-place character assignments. The transformation is done in two steps:

    1. Shift the necessary existing characters to the right to create a gap.
    2. Copy the required characters into the gap.

    Assuming the programmer uses the most efficient sequence of single-character assignments (and avoids undefined behavior from overlapping reads/writes), what is the MINIMUM number of character assignments required to complete this transformation, including the final null terminator placement?

    Question 3
    Level 3: Exam Standard

    Consider the following ANSI C function:

    ```c

    int f(int x, int y, int acc) {

    if (y == 0) return acc;

    if (y % 2 == 0) return f(x * 2, y / 2, acc);

    return f(x, y - 1, acc + x);

    }

    ```

    What is the minimum value of such that the function call f(2, y, 0) results in exactly 6 recursive calls (excluding the initial invocation)?

    Question 4
    Level 3: Exam Standard

    Consider a queue of size containing elements (with $1$ at the front). We wish to reverse the queue into an initially empty queue using only and operations, without any additional storage. During the complete execution of the standard optimal two-queue reversal algorithm, how many more operations are performed on than on ?

    Question 5
    Level 3: Exam Standard

    Consider a binary tree with $100$ nodes. Find the maximum possible number of leaf nodes in such a tree.

    Question 6
    Level 3: Exam Standard

    A student analyzes the worst-case number of comparisons for three different heap operations on elements:

    • : Inserting elements one by one into an initially empty min-heap.
    • : Performing Extract-Min operations on a min-heap of size .
    • : Building a min-heap of size from an arbitrary array using the optimal Build-Heap algorithm.

    The student translates the algorithmic steps into mathematical sums and concludes that because "extraction only moves down, while insertion moves up, and build-heap checks all nodes". This conclusion contains a sign error in the inequality direction. Which of the following is the correct ranking of these quantities for large ?

    Question 7
    Level 3: Exam Standard

    Consider a hash table of size $11$ using double hashing. The auxiliary hash functions are $h_1(k) = k \bmod 11$ and $h_2(k) = 1 + (k \bmod 7)$. The probe sequence for the $i$-th probe is given by $h(k, i) = (h_1(k) + i \cdot h_2(k)) \bmod 11$ for $i = 0, 1, 2, \dots$. The keys $13$, $24$, and $101$ are inserted into the initially empty table in this order. The slot index at which the key $101$ is finally stored is ___________.

    Free preview ends here

    Login to view the complete practice questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Programming and Data Structures Practice Questions for GATE CS

    GATE CS Programming and Data Structures: 7 chapters, 66 previous year questions (100% of Programming and Data Structures), 1831 practice questions and one solved question from each chapter.

    About Programming and Data Structures Practice Questions

    1831 practice questions for Programming and Data Structures in GATE CS, sorted chapter by chapter and graded from basic to exam level, each with a full solution.

    Programming and Data Structures Weightage in GATE CS

    Programming and Data Structures accounts for 66 of 66 Programming and Data Structures previous year questions in our bank (100%), about 6.6 per paper across 10 papers.

    Programming and Data Structures Chapter Matrix

    ChapterTopicsPYQsShare of unit PYQsPractice questions
    C Programming, Control Flow and Array AlgorithmsLoop Tracing and Iterative Computation, Function Calls, Evaluation Order and Static State, Bitwise and Character Expressions, Array Processing and Iterative Algorithms, Variable Scope, Shadowing and Compilation1320%353
    Pointers, Arrays, Strings and Memory ManagementPointer Assignment, Dereferencing and Arithmetic, Strings and Character Pointers, Multidimensional Arrays and Memory Layout, Runtime Memory and Dynamic Allocation1015%278
    Recursion and Recursive Program AnalysisRecursive Array and String Traversal, Recursive Arithmetic Algorithms, Nested Recursion and Return-value Analysis69%165
    Stacks, Queues and Linked ListsStack Operations and Augmented Stacks, Queue Operations, Reversal and Stack-queue Interaction, Linked List Manipulation, Recursion and Complexity1117%283
    Binary Trees, Binary Search Trees and TraversalsBinary Tree Structure, Height and Node Counts, Binary Search Tree Construction and Ordering Properties, Tree Traversals and Recursive Tree Processing, Complete Binary Search Trees in Array Representation1320%363
    Heaps, Priority Queues and Data Structure OperationsHeap Structure, Height and Leaf Positions, Heap Construction and Array Validation, Priority Queue Operations and Heap Extrema, Meld Operations and Data Structure Complexity812%228
    Hashing and Collision ResolutionOpen Addressing and Probe Sequences, Uniform Hashing and Expected Load, Dynamic Hashing and Collision Buckets, Separate Chaining and Chain Length58%161

    More from Programming and Data Structures

    One Solved Question from Each Programming and Data Structures Chapter

    Question 1 · C Programming, Control Flow and Array Algorithms MCQ

    Consider the following two statements regarding ANSI C evaluation:

    \textbf{Assertion (A):} The decimal value of the expression (char)( ('~' | 'a') + 50 ) is .

    \textbf{Reason (R):} The bitwise OR of '~' (126) and 'a' (97) yields 127. Adding 50 gives 177, which exceeds the maximum signed char value of 127. The explicit (char) cast truncates the integer result to 8 bits, wrapping around to due to two's complement representation.

    Which one of the following options is correct?

    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is NOT the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. D.

      A is false, but R is true.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Integer promotion occurs during arithmetic, but an explicit cast to char forces truncation to 8 bits, invoking signed overflow behavior.

    Step 1: Evaluate the bitwise OR. The ASCII value of '~' is 126 (0111 1110). The ASCII value of 'a' is 97 (0110 0001).

    .

    Step 2: Evaluate the addition. Due to integer promotion, the addition is performed as an int, yielding $177$.

    Step 3: Apply the explicit cast. The expression casts the int 177 to char. In 8-bit two's complement, 177 is 1011 0001.

    Step 4: Interpret as signed char. The most significant bit is 1, indicating a negative number. The value is .

    Step 5: Verify the statements. Assertion (A) correctly states the final value is . Reason (R) correctly identifies the intermediate sum (177), the boundary limit (127), and the mechanism of two's complement wrap-around via casting. R perfectly explains A.

    Answer: Both A and R are true, and R is the correct explanation of A.

    Question 2 · Pointers, Arrays, Strings and Memory Management MCQ

    Consider the character array char s[30] = "GATE2024";. A programmer wishes to transform this string into "GATEGATE20242024" by performing in-place character assignments. The transformation is done in two steps:

    1. Shift the necessary existing characters to the right to create a gap.
    2. Copy the required characters into the gap.

    Assuming the programmer uses the most efficient sequence of single-character assignments (and avoids undefined behavior from overlapping reads/writes), what is the MINIMUM number of character assignments required to complete this transformation, including the final null terminator placement?

    1. A.

      8

    2. B.

      9

    3. C.

      13

    4. D.

      16

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: When shifting a string to a higher address (rightward) in-place, you must copy backwards to avoid overwriting unread characters. Only the characters that actually need to move should be shifted.

    Step 1: Analyze the initial and target states.

    Initial: (Indices 0 to 8).

    Target: (Indices 0 to 16).

    Step 2: Identify what needs to move. The prefix (indices 0-3) is already in the correct position. The suffix (indices 4-8, exactly 5 bytes) must be shifted right by 4 positions to indices 8-12.

    Step 3: Execute the shift safely. Since the destination (8) is greater than the source (4), a forward copy would overwrite the null terminator before it is read. We must copy backwards:

    ()

    ('4')

    ('2')

    ('0')

    ('2')

    This requires exactly 5 assignments.

    Step 4: Fill the gap. The gap at indices 4-7 must be filled with .

    .

    This requires exactly 4 assignments.

    Step 5: Sum the operations. total assignments.

    Answer: B

    Question 3 · Recursion and Recursive Program Analysis MCQ

    Consider the following ANSI C function:

    ```c

    int f(int x, int y, int acc) {

    if (y == 0) return acc;

    if (y % 2 == 0) return f(x * 2, y / 2, acc);

    return f(x, y - 1, acc + x);

    }

    ```

    What is the minimum value of such that the function call f(2, y, 0) results in exactly 6 recursive calls (excluding the initial invocation)?

    1. A.

      10

    2. B.

      11

    3. C.

      12

    4. D.

      13

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This function implements a variant of Russian Peasant Multiplication. The number of recursive calls depends on the binary representation of .

    Step 1: Analyze the state transitions.

    • If is even, is halved (1 call).
    • If is odd, is decremented by 1, and is added to acc (1 call). The next step will immediately halve the new even .

    Step 2: Relate to binary representation.

    Each bit of (except the leading 1) requires one "halving" step. Each '1' bit (except the leading 1) requires an additional "decrement" step.

    Total calls = (number of bits in - 1) + (number of set bits in - 1) + 1 (for the final decrement to 0).

    Simplified: Total calls = .

    Step 3: We want exactly 6 calls. Let's test the options.

    • For (binary 1010): . Popcount = 2. Calls = 3 + 2 = 5.
    • For (binary 1011): . Popcount = 3. Calls = 3 + 3 = 6.
    • For (binary 1100): . Popcount = 2. Calls = 3 + 2 = 5.
    • For (binary 1101): . Popcount = 3. Calls = 3 + 3 = 6.

    Step 4: Both 11 and 13 yield 6 calls. The question asks for the minimum value, which is 11.

    Verification for :

    1. f(2, 11, 0) f(2, 10, 2)
    2. f(2, 10, 2) f(4, 5, 2)
    3. f(4, 5, 2) f(4, 4, 6)
    4. f(4, 4, 6) f(8, 2, 6)
    5. f(8, 2, 6) f(16, 1, 6)
    6. f(16, 1, 6) f(16, 0, 22)
    7. f(16, 0, 22) returns 22.

    Total recursive calls = 6.

    Question 4 · Stacks, Queues and Linked Lists MCQ

    Consider a queue of size containing elements (with $1$ at the front). We wish to reverse the queue into an initially empty queue using only and operations, without any additional storage. During the complete execution of the standard optimal two-queue reversal algorithm, how many more operations are performed on than on ?

    1. A.

      35

    2. B.

      45

    3. C.

      55

    4. D.

      -35

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is an operation counting problem. We must separately count the Dequeue operations on and and find their difference.

    Step 1: Every element is removed from exactly once. Thus, total Dequeue operations on = .

    Step 2: For the -th element (from $1$ to ), after it is moved to , the elements already in must be rotated. Each rotation requires 1 Dequeue from .

    Step 3: Total Dequeue operations on = .

    Step 4: For , Dequeue on = .

    Step 5: The difference (Dequeue on minus Dequeue on ) = .

    Answer: 35

    Question 5 · Binary Trees, Binary Search Trees and Traversals NAT

    Consider a binary tree with $100$ nodes. Find the maximum possible number of leaf nodes in such a tree.

    Correct Answer:

    50.00

    Step-by-Step Solution

    Key idea: To maximize leaf nodes , we must minimize internal nodes. Use the constraint and optimize.

    Step 1: We want to maximize subject to:

    Step 2: Substitute :

    Step 3: Since , maximizing means maximizing .

    Step 4: From with :

    • Maximum occurs when is minimized
    • If : (not integer!)
    • If : ✓

    Step 5: With :

    Verification: , , . Total = ✓

    Answer: 50

    Question 6 · Heaps, Priority Queues and Data Structure Operations MCQ

    A student analyzes the worst-case number of comparisons for three different heap operations on elements:

    • : Inserting elements one by one into an initially empty min-heap.
    • : Performing Extract-Min operations on a min-heap of size .
    • : Building a min-heap of size from an arbitrary array using the optimal Build-Heap algorithm.

    The student translates the algorithmic steps into mathematical sums and concludes that because "extraction only moves down, while insertion moves up, and build-heap checks all nodes". This conclusion contains a sign error in the inequality direction. Which of the following is the correct ranking of these quantities for large ?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: The time complexity of heap operations depends on the number of comparisons per level and the number of levels traversed. Build-Heap is , while individual inserts and extracts are each, leading to for operations.

    Step 1: Evaluate . Build-Heap processes nodes from down to 1. The total number of comparisons is bounded by .

    Step 2: Evaluate . Each of the insertions takes at most comparisons (1 comparison per level with the parent). Total comparisons .

    Step 3: Evaluate . Each of the extractions takes at most comparisons (2 comparisons per level with the two children). Total comparisons .

    Step 4: Compare the constants. Since is and the others are , is strictly the smallest for large . Between and , extraction requires 2 comparisons per level while insertion requires 1. Thus, .

    The correct ranking is .

    Answer: Option A.

    Question 7 · Hashing and Collision Resolution MCQ

    Consider a hash table of size $11$ using double hashing. The auxiliary hash functions are $h_1(k) = k \bmod 11$ and $h_2(k) = 1 + (k \bmod 7)$. The probe sequence for the $i$-th probe is given by $h(k, i) = (h_1(k) + i \cdot h_2(k)) \bmod 11$ for $i = 0, 1, 2, \dots$. The keys $13$, $24$, and $101$ are inserted into the initially empty table in this order. The slot index at which the key $101$ is finally stored is ___________.

    1. A.

      5

    2. B.

      7

    3. C.

      8

    4. D.

      10

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is a double hashing trace question. The step size is determined by and remains constant for a given key across all its probes.

    Step 1: Calculate and for each key.

    • For : . .
    • For : . .
    • For : . .

    Step 2: Trace the insertions.

    • Insert 13: Probe 0 is . Slot 2 is empty, so 13 is stored at index 2.
    • Insert 24: Probe 0 is (occupied). Probe 1 is . Slot 6 is empty, so 24 is stored at index 6.
    • Insert 101: Probe 0 is (occupied). Probe 1 is (occupied). Probe 2 is . Slot 10 is empty, so 101 is stored at index 10.

    Answer: 10