IAE Academy

APTITUDE PRACTICE · PROGRAMMING LOGIC

Stacks, queues and basic data structures: aptitude questions

10 questions for the aptitude round of campus placement tests, each with its answer and working. Try each one before you open the answer.

Companies do not publish their test papers; these questions are ours.

The questions

In the pseudocode, for i = a to b runs i from a up to b, with b included.

  1. QUESTION 1

    A stack is empty at the start. The following operations are done in order: push 4, push 9, pop, push 6, push 2, pop. Which element is now on top of the stack?

    1. (A)

      2

    2. (B)

      4

    3. (C)

      6

    4. (D)

      9

    Answer and solution

    Answer: (C)

    1. After push 4 and push 9, the stack is 4, 9 with 9 on top.

    2. Pop removes 9, so only 4 is left.

    3. Push 6 and push 2 give 4, 6, 2 with 2 on top.

    4. The last pop removes 2, so the top is 6.

  2. QUESTION 2

    A queue is empty at the start. The operations are: enqueue 5, enqueue 8, enqueue 3, dequeue, enqueue 7, dequeue. Which element is now at the front of the queue?

    1. (A)

      8

    2. (B)

      3

    3. (C)

      7

    4. (D)

      5

    Answer and solution

    Answer: (B)

    1. After the three enqueues the queue is 5, 8, 3 from front to rear.

    2. The first dequeue removes 5, leaving 8, 3.

    3. Enqueue 7 gives 8, 3, 7.

    4. The second dequeue removes 8, so the front is 3.

  3. QUESTION 3Type the answer

    A stack is empty at the start. The operations are: push 10, push 20, push 30, push 40, pop, pop, push 50. What is the sum of all elements left in the stack?

    Answer and solution

    Answer:

    1. After four pushes the stack is 10, 20, 30, 40.

    2. Two pops remove 40 and then 30, leaving 10, 20.

    3. Push 50 gives 10, 20, 50.

    4. The sum is 10 + 20 + 50 = 80.

  4. QUESTION 4

    How many times does the print statement run in this loop when n = 100?

    for (i = 1; i <= n; i = i * 2)
        print(i);
    1. (A)

      7

    2. (B)

      6

    3. (C)

      5

    4. (D)

      8

    Answer and solution

    Answer: (A)

    1. The value of i doubles each time: 1, 2, 4, 8, 16, 32, 64.

    2. The next value is 128, which is more than 100, so the loop stops.

    3. Each of the seven values gives one print.

    4. The statement runs 7 times.

  5. QUESTION 5

    A singly linked list holds 3, 8, 1, 6, 9 from head to tail. First, the node that comes right after the node holding 8 is deleted. Then a new node holding 4 is inserted right after the head node. What is the list from head to tail now?

    1. (A)

      4, 3, 8, 6, 9

    2. (B)

      3, 4, 8, 6, 9

    3. (C)

      3, 4, 8, 1, 9

    4. (D)

      3, 8, 4, 6, 9

    Answer and solution

    Answer: (B)

    1. The node after 8 is the node holding 1, so deleting it gives 3, 8, 6, 9.

    2. The head node holds 3.

    3. Inserting 4 right after the head gives 3, 4, 8, 6, 9.

  6. QUESTION 6

    The numbers 1, 2, 3, 4 are pushed onto a stack in that order. Then every element is popped from the stack, and each popped element is immediately enqueued into an empty queue. Then two dequeues are done. What does the second dequeue return?

    1. (A)

      1

    2. (B)

      2

    3. (C)

      4

    4. (D)

      3

    Answer and solution

    Answer: (D)

    1. The stack has 4 on top, so the pops return 4, 3, 2, 1 in that order.

    2. The queue therefore holds 4, 3, 2, 1 from front to rear.

    3. The first dequeue returns 4.

    4. The second dequeue returns 3.

  7. QUESTION 7Type the answer

    What is the final value of count after this code runs? Both loop bounds are inclusive.

    count = 0
    for i = 1 to 6
        for j = i to 6
            count = count + 1
    Answer and solution

    Answer:

    1. For i = 1, j runs from 1 to 6 (both included), so 6 times.

    2. For i = 2 to 6 the inner loop runs 5, 4, 3, 2 and 1 times.

    3. The total is 6 + 5 + 4 + 3 + 2 + 1.

    4. So count = 21.

  8. QUESTION 8

    A singly linked list holds 2, 5, 7, 9 from head to tail. A new list starts empty. The first node of the old list is taken out and placed at the front of the new list. This is done three times. What does the new list hold from head to tail?

    1. (A)

      2, 5, 7

    2. (B)

      9, 7, 5, 2

    3. (C)

      7, 5, 2

    4. (D)

      5, 2, 7

    Answer and solution

    Answer: (C)

    1. Move 2 to the front of the empty new list: 2.

    2. Move 5 to the front: 5, 2.

    3. Move 7 to the front: 7, 5, 2.

    4. After three moves the new list is 7, 5, 2, and 9 is still in the old list.

  9. QUESTION 9Type the answer

    How many times does work() run in this code when n = 32? Division is integer division.

    for (i = n; i >= 1; i = i / 2)
        for (j = 1; j <= i; j++)
            work();
    Answer and solution

    Answer:

    1. The outer loop takes i = 32, 16, 8, 4, 2, 1.

    2. For each i, the inner loop runs i times.

    3. The total is 32 + 16 + 8 + 4 + 2 + 1.

    4. So work() runs 63 times.

  10. QUESTION 10

    A postfix expression is evaluated with a stack. Each number is pushed. For an operator, the top two values are popped, the operator is applied with the lower value on the left, and the result is pushed. What is the final result for this expression?

    6 2 3 + * 4 -
    1. (A)

      -26

    2. (B)

      22

    3. (C)

      34

    4. (D)

      26

    Answer and solution

    Answer: (D)

    1. Push 6, 2 and 3. The operator + pops 3 and 2 and pushes 5, so the stack is 6, 5.

    2. The operator * pops 5 and 6 and pushes 30.

    3. Push 4. The operator - pops 4 and then 30, and computes 30 - 4.

    4. The final result is 26.

More practice

More programming logic: Loops and conditions · Functions and recursion · Arrays and strings · Operators and number logic · C language basics

Past GATE aptitude questions, solved: General Aptitude

Every topic with its answers, to print or keep: one PDF (1.9 MB)

All aptitude topics