InfyTQ Python questions

The InfyTQ syllabus topics: basics, algorithms, control structures, collections, functions and OOP, with output questions checked by running the code.

1. What is the output?

print(7 // 2, 7 % 2, 2 ** 3)
  1. 3.5 1 8
  2. 4 1 8
  3. 3 1 8
  4. 3 1 6
Answer

C. 3 1 8

// is floor division, % gives the remainder and ** is power.

2. What is the output?

x = "5"
y = 2
print(x * y)
  1. 55
  2. 7
  3. 25
  4. 10
Answer

A. 55

Multiplying a string by an integer repeats the string.

3. What is the output?

a, b = 3, 4
a, b = b, a + b
print(a, b)
  1. 4 8
  2. 3 7
  3. 7 4
  4. 4 7
Answer

D. 4 7

The right side is worked out first, (4, 3 + 4), then assigned.

4. Which of these is a valid variable name in Python?

  1. _total2
  2. class
  3. 2total
  4. total-2
Answer

A. _total2

Names start with a letter or underscore; hyphens are not allowed and keywords such as class cannot be names.

5. What is the output?

print(type(10 / 2).__name__)
  1. float
  2. double
  3. number
  4. int
Answer

A. float

The / operator always gives a float in Python 3, so 10 / 2 is 5.0.

6. What is the output?

print(int("12") + float("1.5"))
  1. 121.5
  2. 13.5
  3. 13
  4. 12.15
Answer

B. 13.5

int("12") is 12 and float("1.5") is 1.5, so the sum is 13.5.

7. What does input() return in Python 3?

  1. None until Enter is pressed
  2. An int if only digits are typed
  3. Always a string
  4. A list of the words typed
Answer

C. Always a string

input() returns text; convert it with int() or float() when you need a number.

8. What is the output?

print(10 > 5 and 3 > 7 or not False)
  1. 1
  2. True
  3. False
  4. None
Answer

B. True

and is checked before or: (True and False) is False, and False or True is True.

9. Binary search halves the search range at every step. At most how many halvings leave one element out of 1024?

  1. 1024
  2. 10
  3. 32
  4. 512
Answer

B. 10

1024 = 2^10, so ten halvings bring it down to one element.

10. What is the worst-case time complexity of linear search on n items?

  1. O(1)
  2. O(log n)
  3. O(n log n)
  4. O(n)
Answer

D. O(n)

In the worst case every item is checked once.

11. What is the output?

def f(n):
    if n == 0:
        return 0
    return n + f(n - 1)
print(f(4))
  1. 10
  2. 24
  3. 4
  4. 0
Answer

A. 10

f(4) = 4 + 3 + 2 + 1 + 0 = 10.

12. After the first pass of bubble sort (ascending) on [5, 1, 4, 2], the list is:

  1. [1, 4, 2, 5]
  2. [1, 5, 4, 2]
  3. [5, 4, 2, 1]
  4. [1, 2, 4, 5]
Answer

A. [1, 4, 2, 5]

One pass compares neighbours and swaps them when out of order, pushing the largest value, 5, to the end.

13. Which algorithm needs the list to be sorted before it can be used?

  1. Counting how often a value appears
  2. Linear search
  3. Finding the maximum
  4. Binary search
Answer

D. Binary search

Binary search decides which half to keep by comparing with the middle value, which only works on sorted data.

14. What is the output?

nums = [3, 8, 2, 9, 4]
big = nums[0]
for x in nums:
    if x > big:
        big = x
print(big)
  1. 8
  2. 9
  3. 3
  4. 4
Answer

B. 9

The loop keeps the largest value seen so far.

15. Two nested loops that each run n times have a time complexity of:

  1. O(2n)
  2. O(n²)
  3. O(n)
  4. O(log n)
Answer

B. O(n²)

The inner loop runs n times for each of the n outer passes: n × n steps.

16. What is the output?

def g(n):
    if n <= 1:
        return 1
    return n * g(n - 2)
print(g(5))
  1. 5
  2. 15
  3. 120
  4. 60
Answer

B. 15

g(5) = 5 × g(3) = 5 × 3 × g(1) = 15.

17. What is the output?

total = 0
for i in range(1, 10, 3):
    total += i
print(total)
  1. 45
  2. 22
  3. 12
  4. 15
Answer

C. 12

range(1, 10, 3) gives 1, 4 and 7.

18. What is the output?

i = 0
while i < 10:
    i += 3
print(i)
  1. 10
  2. 9
  3. 3
  4. 12
Answer

D. 12

i becomes 3, 6, 9 and then 12, which ends the loop.

19. What is the output?

for i in range(5):
    if i == 3:
        break
    print(i, end=" ")
  1. 0 1 2 4
  2. 0 1 2
  3. 3
  4. 0 1 2 3
Answer

B. 0 1 2

break stops the loop as soon as i is 3, before it is printed.

20. What is the output?

for i in range(5):
    if i % 2 == 0:
        continue
    print(i, end="")
  1. 01234
  2. 13
  3. 1 3
  4. 024
Answer

B. 13

continue skips the even numbers; end="" prints the odd ones next to each other.

21. What is the output?

x = 15
if x > 10:
    print("A")
elif x > 5:
    print("B")
else:
    print("C")
  1. AB
  2. B
  3. ABC
  4. A
Answer

D. A

Only the first true branch of an if/elif chain runs.

22. What is the output?

for i in range(3):
    pass
else:
    print("done", i)
  1. Nothing is printed
  2. An error
  3. done 3
  4. done 2
Answer

D. done 2

A for loop's else runs when the loop ends without break; i keeps its last value, 2.

23. What is the output?

count = 0
for i in range(3):
    for j in range(i):
        count += 1
print(count)
  1. 9
  2. 2
  3. 6
  4. 3
Answer

D. 3

The inner loop runs 0, 1 and 2 times: 3 in all.

24. What is the output?

n = 1234
s = 0
while n > 0:
    s += n % 10
    n //= 10
print(s)
  1. 10
  2. 4
  3. 1234
  4. 24
Answer

A. 10

The loop adds the digits 4 + 3 + 2 + 1.

25. What is the output?

a = [1, 2, 3]
b = a
b.append(4)
print(len(a))
  1. 0
  2. 1
  3. 3
  4. 4
Answer

D. 4

b = a does not copy the list; both names refer to the same list.

26. What is the output?

d = {"a": 1, "b": 2}
d["a"] = 5
print(sum(d.values()))
  1. 8
  2. 3
  3. 7
  4. 6
Answer

C. 7

The value for "a" is replaced by 5, so the values are 5 and 2.

27. What is the output?

s = {3, 1, 3, 2, 1}
print(len(s))
  1. 3
  2. 5
  3. 2
  4. 4
Answer

A. 3

A set keeps only unique values: 1, 2 and 3.

28. What is the output?

nums = [10, 20, 30, 40, 50]
print(nums[1:4])
  1. [20, 30, 40, 50]
  2. [10, 20, 30, 40]
  3. [20, 30, 40]
  4. [20, 40]
Answer

C. [20, 30, 40]

A slice includes the start index and stops before the end index.

29. What is the output?

t = (1, 2, 3)
try:
    t[0] = 9
except TypeError:
    print("immutable")
  1. immutable
  2. (9, 2, 3)
  3. Nothing is printed
  4. 9
Answer

A. immutable

Tuples cannot be changed, so the assignment raises TypeError.

30. What is the output?

print([x * x for x in range(5) if x % 2 == 1])
  1. [1, 9]
  2. [0, 4, 16]
  3. [1, 4, 9, 16]
  4. [1, 3]
Answer

A. [1, 9]

Only the odd x (1 and 3) are kept, and each is squared.

31. Which collection suits storing each student's marks against their roll number, for fast lookup by roll number?

  1. A dictionary
  2. A tuple
  3. A set
  4. A list
Answer

A. A dictionary

A dictionary maps keys (roll numbers) to values (marks) with fast lookup by key.

32. What is the output?

words = ["pen", "ink", "pen", "cap"]
count = {}
for w in words:
    count[w] = count.get(w, 0) + 1
print(count["pen"])
  1. 4
  2. 3
  3. 1
  4. 2
Answer

D. 2

get(w, 0) returns 0 the first time a word is seen; "pen" appears twice.

33. What is the output?

def add(a, b=10):
    return a + b
print(add(5), add(5, 1))
  1. 6 6
  2. 15 6
  3. 5 6
  4. 15 15
Answer

B. 15 6

b defaults to 10 when it is not passed.

34. What is the output?

x = 5
def change():
    x = 10
change()
print(x)
  1. None
  2. 0
  3. 5
  4. 10
Answer

C. 5

Assigning x inside the function creates a new local x; the global x is unchanged.

35. What is the output?

def f():
    print("hi", end=" ")
r = f()
print(r)
  1. hi hi
  2. None
  3. hi None
  4. hi
Answer

C. hi None

f prints "hi" and returns nothing, so r is None.

36. What is the output?

sq = lambda n: n * n
print(sq(sq(2)))
  1. 4
  2. 8
  3. 16
  4. 64
Answer

C. 16

sq(2) is 4 and sq(4) is 16.

37. What is the output?

def total(*nums):
    return sum(nums)
print(total(1, 2, 3, 4))
  1. [1, 2, 3, 4]
  2. 4
  3. 10
  4. 1
Answer

C. 10

*nums collects all the arguments into a tuple.

38. What is the output?

def show(name, greeting="Hello"):
    return greeting + " " + name
print(show(greeting="Hi", name="Ravi"))
  1. Hello Ravi
  2. Hello Hi
  3. Ravi Hi
  4. Hi Ravi
Answer

D. Hi Ravi

Keyword arguments can be passed in any order.

39. What is the output?

def put(item, box=[]):
    box.append(item)
    return len(box)
put(1)
print(put(2))
  1. 3
  2. 2
  3. 1
  4. 0
Answer

B. 2

A default list is created once and shared between calls, so the second call sees two items.

40. Which statement about return in Python is true?

  1. return prints the value on the screen
  2. Every function must contain a return statement
  3. return may appear only once in a function
  4. A function can return several values, packed as a tuple
Answer

D. A function can return several values, packed as a tuple

return a, b gives back the tuple (a, b); a function without return gives None.

41. What is the output?

class Dog:
    def __init__(self, name):
        self.name = name
d = Dog("Tom")
print(d.name)
  1. name
  2. self
  3. Dog
  4. Tom
Answer

D. Tom

__init__ stores the name on the new object.

42. What is the output?

class A:
    def hi(self):
        return "A"
class B(A):
    def hi(self):
        return "B"
print(B().hi())
  1. B
  2. AB
  3. BA
  4. A
Answer

A. B

B overrides the hi method it inherits from A.

43. What is the output?

class Counter:
    count = 0
    def __init__(self):
        Counter.count += 1
Counter()
Counter()
print(Counter.count)
  1. 2
  2. 0
  3. 3
  4. 1
Answer

A. 2

count is a class variable shared by all objects; each new object adds 1.

44. What is the output?

class P:
    def __init__(self):
        self.__secret = 1
p = P()
print(hasattr(p, "__secret"))
  1. None
  2. False
  3. True
  4. 1
Answer

B. False

Names that start with two underscores are renamed (to _P__secret), so "__secret" is not found.

45. What is the output?

class Base:
    def __init__(self):
        self.x = 1
class Child(Base):
    def __init__(self):
        super().__init__()
        self.y = 2
c = Child()
print(c.x + c.y)
  1. 1
  2. 12
  3. 3
  4. 2
Answer

C. 3

super().__init__() runs Base's constructor, which sets x.

46. Inside a method, what does self refer to?

  1. The object the method was called on
  2. The parent class
  3. A global variable
  4. The class itself
Answer

A. The object the method was called on

Python passes the object as the first argument, named self by convention.

47. Which OOP idea lets the same method name behave differently in different classes?

  1. Encapsulation
  2. Polymorphism
  3. Composition
  4. Abstraction
Answer

B. Polymorphism

Polymorphism: each class gives its own version of the method.

48. What is the output?

class Shape:
    def area(self):
        return 0
class Sq(Shape):
    def __init__(self, s):
        self.s = s
    def area(self):
        return self.s * self.s
shapes = [Shape(), Sq(3)]
print(sum(x.area() for x in shapes))
  1. 6
  2. 9
  3. 0
  4. 3
Answer

B. 9

Each object uses its own area: 0 + 9.

Mock test

More Technical MCQs topics