# Python Predict the Output Interview Questions

Get FREE domain for 1st year and build your brand new site In this article, we test our Python programming knowledge by going over 50 Predict the Output Interview Questions.

1. What is the output of the code;

``````>>> str = "linux"
>>> str[:3]
>>>
``````

(a) linux
(b) inu
(c) lin
(d) error

Ans: (c)
Explanation: The above code returns the text 'lin', the first three bytes of the string "linux".

2. What is the output of the code;

``````>>> def main(a):
a = a + '2'
a = a * 2
return a
>>> main("byte")
``````

(a) error
(b) byte2byte2
(c) byte2byte
(d) Other error

Ans: (b)
Explanation: We can add and multiply Python strings.

3. What is the output of the code;

``````>>> print("tom\ndick\nharry")
``````

(a) tom
dick
harry
(b) "tom\ndick\nharry"
(c) tom\ndick\nharry
(d) None of the above

Ans: (a)

4. What is wrong with the code;

``````i = 1
while True:
if i % 3 == 0:
break
print(i)

i + = 1
``````

(a) Nothing is wrong
(b) Wrong indentation
(c) + and = are spaced
(d) we used '' instead of '='

Ans: (c)
Explanation: '+' and '=' should be joined for the code to work.

5. What does the correct code from question (4) do;
(a) Executes infinitely
(b) Prints 1 and 2
(c) Prints all prime numbers infinitely
(d) None of the above

Ans: (b)
Explanation: The function loops while 'i' whose value is 1 until the value of 'i is divisible by 3. Therefore we loop terminates at step 3 since 3 % 3 == 0. At each step in the loop, we print the value of 'i'.

6. What is the output of the code;

``````>>> 2 << 7
``````

(a) 256
(b) 0
(c) 127
(d) 49

Ans: (a)
Explanation: '<<' is referred to as a bitwise left shift. A bit-shift shifts each digit in a number's binary left or right. Shifting can be left(most significant bit is lost and 0 inserted on the opposite end) and right(least significant bit lost and 0 inserted on the opposite end.

7. Which function is called if the following is executed;

``````str = foo()
format(str)
``````

(a) format()
(b) str()
(c) __str__
(d) __format__()

Ans: (c)
Explanation: Functions str(str) and format(str) will both call str.__str__().

8. What is the output of the following code;

``````>>> class classy:
...     def __init__(self, id):
...         self.id = str(id)
...         id="11"
...
>>> instance = classy(22)
>>> instance.id
``````

(a) 11
(b) 22
(c) "11"
(d) None of the above

Ans: (b)
Explanation: Above we initialize a class and instantiate it with the value '22'. This will be the result of the above snippet.

9. What is the output of the following code;

``````>>> def funk(lst):
...     lst = ['def']
...     lst = ['abc']
...     return id(lst)
...
>>> lst = ['abc', 'def']
>>> id(lst) == funk(lst)
``````

(a) False
(b) True
(c) Error
(d) None of the above

Ans: (b)
Explanation: The 'id' function returns a unique id based on the value passed. In this case, we evaluate the same object and therefore we expect the left and right sides to be equal.

10. What is the output of the code;

``````>>> lst = ['abc', 'def']
>>> id(lst)
``````

(a) Error
(b) A unique integer
(c) integer overflow
(d) None of the above

Ans: (b)
Explanation: The 'id' function in Python returns a unique integer.

11. What is the output of the code;

``````>>> "a" + 'bc'
``````

(a) abc
(b) 'abc'
(c) "abc"
(d) Error

Ans: (b) or (c)
Explanation: Above we use the '+' sign to concatenate two strings.

12. How many arguments can you pass in the following function;

``````def func(*args, *kwargs):
pass
``````

(a) 2
(b) 3
(c) any number
(d) 10

Ans: (c)
Explanation: args and kwargs are keywords in python that allow a function to take any number of arguments. They are commonly used in situations where we don't know the exact number of parameters a function is going to take.

13. What is the output of the code;

``````>>> min(max(False, -7.5, -7), 2, 1, 9)
``````

(a) None of the following
(b) -7.5
(c) 9
(d) False

Ans: (d)
Explanation: Above max function gets the maximum between -7.5, -7, and False. Here, False is the maximum since it is zero, then between False, 2, 1, and 9, False is the minimum(min) therefore it prints False.

14. If num = 100.100, what is the output of the following code;

``````>>> print("%.2f"% num)
``````

(a) 100.10
(b) 100.1
(c) 100.100
(d) 100.10f

Ans: (a)
Explanation: Above, we round 'num' to two decimal places and have 100.10. What will be the output if num was 100.109? In this case, we will have 100.11 since we will round '0' because 9 is greater than or equal to 5.

15. What is the output of the code;

``````>>>  (lambda x, y: x * y)(2, 3)
``````

(a) 6
(b) 5
(c) Error
(d) None of the above

Ans: (a)
Explanation: The above lambda function takes two parameters and multiplies them then returns the results.

16. What is the output of the code;

``````>>> lst = [11, 'bool', 54, {}, 33, [], '']
>>> list(filter(bool, lst))
``````

(a) [11, 54, 33]
(b) [11, 'bool', 54, 33]
(c) ['bool', {}, '']
(d) Error

Ans: (b)
Explanation: Above we filter out elements that amount to zero and are left with those that don't.

17. What is the output of the code;

``````>>> add_one = lambda x: x + 1
``````

(a) 5
(b) 4
(c) Error
(d) None of the above

Ans: (a)
Explanation: We create a lambda function add_one that adds one to an argument passed to it therefore when we pass '4' we get '4'.

18. What is the output of the code;

``````>>> (lambda x: x + 1)(2)
``````

(a) 2
(b) 3
(c) Error
(d) None of the above

Ans: (b)
Explanation: Above we create a function that takes 'x' and adds '1' to it then returns it. We then use parenthesis to pass function arguments. In this case, we pass '2' as the function argument.

19. What is the output of the code;

``````>>> str = 'abcd'
>>> for i in str:
...     print(i.upper())
...
``````

(a) A B C D
(b) A \n B \n C \n D
(c) a \n b \n c \n d
(d) a b c d

Ans: (b)
Explanation: By using upper function we convert every character in 'str' to upper case by using a 'for' loop to traverse the string.

20. What is the output of the code;

``````>>> type('100.9')
``````

(a) <class 'int'>
(b) <class 'str'>
(c) <class 'float'>
(d) None of the above

Ans: (b)
Explanation: By using single quotation marks above, we convert the number 100.9 into a string in Python. We can convert the number into a float by casting it as follows;

``````>>> a = '100.9'
>>> float(a)
100.9
``````

21. What is the output of the code;

``````>>> print([i.lower() for i in "HELLO"])
``````

(a) hello
(b) ['h' 'e' 'l' 'l' 'o']
(c) ['hello']
(d) Error

Ans: (b)
Explanation: Above we are iterating over every letter in the string 'HELLO' and converting it to lowercase.

22. What is the output of the code;

``````>>> text = 'hello world'
>>> x = [print(i) for i in text if i not in "aeiou"]
``````

(a) h l l w r l d
(b) e o o
(c) Error
(d) None of the above

Ans: (a)
Explanation: Above we remove all vowels from the text 'hello world' through a process referred to as list comprehension

23. What is the output of the code;

``````>>> d = {1 : "A", 2 : "B", 3 : "C"}
>>> d.clear()
>>> print(d)
``````

(a) {1 : "A", 2 : "B", 3 : "C"}
(b) {}
(c) [1, 2, 3]
(d) ['A', 'B', 'C']

Ans: (b)
Explanation: The clear method removes all key-value pairs from a Python dictionary.

24. What is the output of the code;

``````>>> d = {1 : "A", 2 : "B", 3 : "C"}
>>> d.get(1, 4)
``````

(a) 'A'
(b) 'B'
(c) 'C'
(d) 1

Ans: (a)
Explanation: The get method in Python dictionaries returns the value of a key if present otherwise it returns a default value if absent. The default value is the second parameter we pass to the get method.

25. What is the output of the code;

``````>>> d = {1 : "A", 2 : "B", 3 : "C"}
>>> d.setdefault((2))
``````

(a) 'A'
(b) 'B'
(c) 'C'
(d) 1

Ans: (a)
Explanation: The setdefault method sets d[key] = default is key does not exist in the dictionary.

26. What is the output of the code;

``````>>> float(18//6+3/3)
``````

(a) 4.333333333333333
(b) 4.0
(c) 4
(d) None of the above

Ans: (b)
Explanation: We evaluate the expression as follows, first we evaluate (18 // 6) which is 3, then add 1(3/3) and get 4.0.

27. What is the output of the code;

``````>>> print('hello' 'world')
``````

(a) helloworld
(b) hello world
(c) Error

Ans: (a)
Explanation: We will print out 'helloworld'. If we wanted a space between the two strings we would have to explicitly define it.

28. What is the output of the code;

``````>>> print('__funk__'.isidentifier())
``````

(a) True
(b) False

Ans: (a)
Explanation: The isidentifier() function returns true if the passed argument is an identifier, otherwise it returns false.

29. What is the output of the code;

``````>>> 8/4/2, 8/(4/2)
``````

(a) (1.0, 4.0)
(b) (1, 4)
(c) (4.0, 1.0)
(d) None of the above

Ans: (a)
Explanation: We have 2/2 which is equal to 1.0 and 8/2 which is equal to 4.0.

30. What is the output of the code;

``````>>> str = "helloworld"
>>> str[::-1]
``````

(a) helloworld
(b) 'dlrowolleh'
(c) dlrowolleh

Ans: (b)
Explanation: Above we reverse the string 'helloworld' and obtain 'dlrowolleh'.

31. What is the output of the code;

``````>>> bin(34)
``````

(a) '0b101010'
(b) '0b110010'
(c) '0b100010'
(d) '0b100011'

Ans: (c)
Explanation: The binary representation of 34 is 100010 therefore the result is (c)

32. What is the output of the code;

``````>>> print(0xD + 0xE + 0xF)
``````

(a) 42
(b) 36
(c) 39
(d) 40

Ans: (a)
Explanation: Above 0xD, 0xE, 0xF are hexadecimal representations of 13, 14 and 14. We can get this by writing;

``````>>> int(0xF)
``````

33. What is the output of the code;

``````>>> 2**(3**2)
>>> (2**3)**2
>>> 2**3**2
``````

(a) 64, 512, 512
(b) 512, 64, 512
(c) 64, 64, 64
(d) 64, 512, 64

Ans: (b)
Explanation: 512 is a result of (2 ** 9), 64 as a result of (8 ** 4), and 512 as a result of (2 ** (3 ** 2).

34. What is the output of the code;

``````>>> print('0xa'.isdigit())
``````

(a) True
(b) False

Ans: (b)
Explanation: The isdigit function returns true if the passed arguments are digits otherwise it returns false.

35. What is the output of the code;

``````>>> int(1011)
``````

(a) 1011
(b) 11
(c) '1011'
(d) None of the above

Ans: (a)
Explanation: The above code prints an integer 1011. To convert the binary representation to decimal, we write;

``````>>> int('1011', 2)
11
``````

36. What is the output of the code;

``````>>> print('*', "abcde".center(6), '*', sep='')
``````

(a) * abcde*
(b) *abcde*
(c) * abcde *
(d) *abcde *

Ans: (d)
Explanation: .center aligns the string center and uses a specified character as the fill character, in this case, it is '' - an empty space. We pad towards the right until the string is of the even length of '6'. We can change the separator if we wished, for example;

``````>>> print('*', "abcde".center(8), '*', sep='=')
>>> *= abcde  =*
``````

37. What is the output of the code;

``````>>> 2 + 9.00, 2**8.0
``````

(a) (11.000, 256.000)
(b) (11, 256)
(c) (11.0, 256.0)
(d) (11.00, 256.00)

Ans: (c)
Explanation: Above, the result of each expression is rounded off to a single decimal place.

38. What is the output of the code;

``````>>> text = "bikes are fast"
>>> regex = re.compile('(?P<transport>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
>>> matches = re.search(regex, text)
>>> print(matches.groupdict())
``````

(a) <re.Match object; span=(0, 14), match='bikes are fast'>
(b) {'transport': 'bikes', 'verb': 'are', 'adjective': 'fast'}
(d) None of the above

Ans: (b)
Explanation: We print a dictionary containing all matched items in 'text'.

39. What is the output of the code;

``````>>> dictionary = {'Alice':100, 'Bob':100, 'Cat':100}
>>> print(sum(dictionary.values()))
``````

(a) 300
(b) 100
(c) 200
(d) None of the above

Ans: (a)
Explanation: We can sum all elements in a dictionary in Python by using the sum function.

40. What is the data type of the following;

``````x = [1, 23, 'hello', 1]
``````

(a) list
(b) set
(c) map
(d) dict

Ans: (a)
Explanation: In Python lists are enclosed using [] - square brackets. We use {} for sets and dictionaries and () for tuples.

41. What is the output of the code;

``````>>> st = set('abcde')
>>> 'a' in st
``````

(a) False
(b) True
(c) SyntaxError: EOL
(d) None of the above

Ans: (b)
Explanation: The 'in' keyword checks if a value is present in a sequence. In this case, our sequence is a set, we check if 'a' exists in the set. The result is True.

42. What is the output of the code;

``````>>> round(2.999)
``````

(a) 2.0
(b) 3
(c) 2.9
(d) 3.0

Ans: (b)
Explanation: The round() function returns a floating point representing the rounded version of 2.999.

43. Which of the following will result in the output 6?

``````arr = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
``````

(a) arr
(b) arr
(c) arr
(d) arr

Ans: (d)
Explanation: In 'arr' row 1 column 2 (0-indexed) is 6.

44. What is the output of the code;

``````>>> for i in range(len(str)):
...     print(i)
...
``````

(a) 1 2 3
(b) x, y, z
(c) 0 1 2
(d) x y z

Ans: (c)
Explanation: Above we loop iterate n-time, n being the length of the string 'str' while printing the value if 'i' which by default starts at 0. We can specify a start value by writing; range(start_value, len(str)).

45. What is the output of the code;

``````>>> lst_1 = [1, 2, 3, 4]
>>> lst_2 = [7, 2, 5, 1]
>>> lst_3 = list()
>>> lst_3.extend(i for i in lst_1 if i not in (lst_2 + [2, 8]) and i not in [2, 6, 7, 8])
>>> lst_3
``````

(a) [2, 3, 4]
(b) [3, 4]
(c) Error
(d) None of the above

Ans: (b)
Explanation: Above we use the extend() method to add elements of an iterable such as lists, tuples, and strings among others, to the end of the list. In this case, we extend lst_3 with elements in lst_2 appended with [2, 8] that are not in the list - [2, 6, 7, 8]. 3 and 4 and not in the latter lists but are in list 1(lst_1).

46. What is the output of the code;

``````a = b
``````

(a) Syntax Error
(b) Name Error
(c) Value Error
(d) Type Error

Ans: (b)
Explanation: Since we don't define 'b', we will get a Name Error.

47. What is the output of the code;

``````>>> print("Python {0} is {0}".format(('Object', 'Oriented')))
``````

(a) Python is Object Oriented
(b) Python Object is Oriented
(c) Python Oriented is Object
(d) Syntax Error

Ans: (b)
Explanation: We store 'Object' and 'Oriented' inside a tuple and access them by their indices.

48. What is the output of the code;

``````>>> print('{0:.2%}'.format(1/4))
``````

(a) 35.00%
(b) 0.0025%
(c) 25.00%
(d) 0.25%

Ans: (c)
Explanation: Above, we use the '%' character to represent an expression evaluation as a percentage. In this case, 1/4 is equal to 25.00% of 100.00%.

49. What is the output of the code;

``````>>> "abc. DEF".capitalize()
``````

(a) 'Abc. def'
(b) 'Abc. DEF'
(c) 'ABC. def'
(d) None of the above.

Ans: (a)
Explanation: The capitalize function converts the first letter in a string to an upper case, in this case, our 'A' is the only character in the upper case.

50. What is the output of the code;

``````>>> st = set('abc')