IAE Academy

APTITUDE PRACTICE · PROGRAMMING LOGIC

Arrays and strings: 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

    Indexing starts at 0. What does the code print?

    A = [3, 7, 2, 9, 5, 6]
    s = 0
    for i = 0; i < 6; i = i + 2:
        s = s + A[i]
    print s
    1. (A)

      10

    2. (B)

      22

    3. (C)

      12

    4. (D)

      15

    Answer and solution

    Answer: (A)

    1. The loop goes through i = 0, 2 and 4.

    2. Those elements are A[0] = 3, A[2] = 2 and A[4] = 5.

    3. Their sum is 3 + 2 + 5 = 10.

    4. The program prints 10.

  2. QUESTION 2

    A program counts the vowels (a, e, i, o, u) in the string "education". What count does it print?

    1. (A)

      4

    2. (B)

      5

    3. (C)

      6

    4. (D)

      9

    Answer and solution

    Answer: (B)

    1. The letters of education are e, d, u, c, a, t, i, o, n.

    2. The vowels are e, u, a, i and o.

    3. The consonants d, c, t and n are not counted.

    4. The count is 5.

  3. QUESTION 3Type the answer

    The array is [12, 5, 18, 10, 7, 25, 10, 11]. A loop counts the elements that are strictly greater than 10. What is the final count?

    Answer and solution

    Answer:

    1. Check each element against 10.

    2. 12, 18, 25 and 11 are greater than 10.

    3. 5, 10, 7 and 10 are not greater than 10.

    4. The count is 4.

  4. QUESTION 4

    Indexing starts at 0. What does the code print?

    a = [1, 2, 3, 4, 5, 6]
    for i = 0; i < 2; i = i + 1:
        swap a[i] and a[5 - i]
    print a
    1. (A)

      [6, 5, 4, 3, 2, 1]

    2. (B)

      [6, 5, 3, 4, 2, 1]

    3. (C)

      [2, 1, 3, 4, 6, 5]

    4. (D)

      [1, 2, 5, 6, 3, 4]

    Answer and solution

    Answer: (B)

    1. The loop runs only for i = 0 and i = 1.

    2. i = 0 swaps a[0] and a[5], giving [6, 2, 3, 4, 5, 1].

    3. i = 1 swaps a[1] and a[4], giving [6, 5, 3, 4, 2, 1].

    4. The middle two elements are never swapped, so the output is [6, 5, 3, 4, 2, 1].

  5. QUESTION 5

    Indexing starts at 0. What does the code print?

    s = "banana"
    count = 0
    for i = 0; i < 4; i = i + 1:
        if s[i] == s[i + 2]:
            count = count + 1
    print count
    1. (A)

      2

    2. (B)

      3

    3. (C)

      4

    4. (D)

      6

    Answer and solution

    Answer: (B)

    1. The characters are b(0), a(1), n(2), a(3), n(4), a(5).

    2. i = 0: b and n differ. i = 1: a and a match.

    3. i = 2: n and n match. i = 3: a and a match.

    4. The count is 3.

  6. QUESTION 6

    Indexing starts at 0. What does the code print?

    A = [2, 5, 9, 9, 14, 12, 20]
    i = 1
    while i < 7 and A[i] >= A[i - 1]:
        i = i + 1
    print i + A[i]
    1. (A)

      5

    2. (B)

      12

    3. (C)

      17

    4. (D)

      14

    Answer and solution

    Answer: (C)

    1. The loop stops at the first i where A[i] is less than A[i - 1].

    2. For i = 1 to 4 each element is at least the one before it, since 9 >= 9 also holds.

    3. At i = 5, A[5] = 12 is less than A[4] = 14, so the loop stops.

    4. The program prints 5 + 12 = 17.

  7. QUESTION 7

    The code reads "hello world" from left to right. It skips every space and every letter l. Each other character is added to the front of t. What is t at the end?

    s = "hello world"
    t = ""
    for each character c in s:
        if c != ' ' and c != 'l':
            t = c + t
    print t
    1. (A)

      heoword

    2. (B)

      drowoeh

    3. (C)

      dlrowolleh

    4. (D)

      hweorod

    Answer and solution

    Answer: (B)

    1. The characters kept, in order, are h, e, o, w, o, r, d.

    2. Adding each one to the front reverses their order.

    3. The reverse of heoword is drowoeh.

    4. The program prints drowoeh.

  8. QUESTION 8

    Indexing starts at 0. Each step uses the value of a[i] already updated by the previous step. What does the code print?

    a = [2, 5, 3, 8, 4]
    for i = 0; i < 4; i = i + 1:
        a[i + 1] = a[i + 1] - a[i]
    print a[0] + a[1] + a[2] + a[3] + a[4]
    1. (A)

      22

    2. (B)

      0

    3. (C)

      9

    4. (D)

      13

    Answer and solution

    Answer: (C)

    1. i = 0: a[1] = 5 - 2 = 3.

    2. i = 1: a[2] = 3 - 3 = 0.

    3. i = 2: a[3] = 8 - 0 = 8, then i = 3: a[4] = 4 - 8 = -4.

    4. The array is [2, 3, 0, 8, -4] and its sum is 9.

  9. QUESTION 9Type the answer

    The array A is [3, 1, 4, 1, 5, 9, 2, 6]. Indexing starts at 0. The code counts pairs of positions (i, j) with i < j whose elements add up to 10. What does it print?

    count = 0
    for i = 0; i < 8; i = i + 1:
        for j = i + 1; j < 8; j = j + 1:
            if A[i] + A[j] == 10:
                count = count + 1
    print count
    Answer and solution

    Answer:

    1. Pairs are counted by position, so equal values at different positions count separately.

    2. 4 at index 2 and 6 at index 7 add up to 10.

    3. The 9 at index 5 pairs with the 1 at index 1 and the 1 at index 3.

    4. No other pair works, so the count is 3.

  10. QUESTION 10

    What does the code print?

    s = "mmnooopppp"
    n = 10
    out = ""
    i = 0
    while i < n:
        j = i
        while j < n and s[j] == s[i]:
            j = j + 1
        out = out + s[i] + str(j - i)
        i = j
    print out
    1. (A)

      m2n1o3p4

    2. (B)

      m2n1o3p3

    3. (C)

      m2no3p4

    4. (D)

      m2n1o2p4

    Answer and solution

    Answer: (A)

    1. The code writes each run of equal characters as the letter and its length.

    2. The runs are mm, n, ooo and pppp.

    3. Their lengths are 2, 1, 3 and 4, and a run of length 1 still prints its count.

    4. The output is m2n1o3p4.

More practice

More programming logic: Loops and conditions · Functions and recursion · Operators and number logic · Stacks, queues and basic data structures · 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