ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
def merge(a, b):
    i = j = 0
    out = []
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            out.append(a[i])
            i += 1
        else:
            out.append(b[j])
            j += 1
    out += a[i:] + b[j:]
    return out
print(merge([1, 4, 4, 9], [2, 4, 10]))
  1. A. [1, 2, 4, 4, 9, 10]
  2. B. [1, 2, 4, 4, 4, 10, 9]
  3. C. [1, 2, 4, 4, 4, 9, 10]
  4. D. [1, 2, 4, 9, 4, 4, 10]

Correct answer: C. [1, 2, 4, 4, 4, 9, 10]

With <= the a-side copy of 4 goes first. Together the inputs hold three 4s, so all three appear, and the leftover 10 from b is appended last.

More Python Programming questions

Exams that ask this topic