Thursday 7 January 2021

Python Loops Interview questions - Multiple choice

Loops Interview questions:

=====================================================================

1. x = [“ab”, “cd”]

for i in x:

              i.upper()

print(x)

a) [‘ab’, ‘cd’] 

b) [‘AB’, ‘CD’] 

c) [None, None] 

d) none of the mentioned

Answer: a

Explanation: The function upper() does not modify a string in place, it returns a new string that isn’t being stored anywhere


2. x = ["ab", "cd"]

for i in x:

x.append(i.upper())

print(x)

a) [‘AB’, ‘CD’] 

b) [‘ab’, ‘cd’, ‘AB’, ‘CD’] 

c) [‘ab’, ‘cd’] 

d) none of the mentioned


Answer: d

Explanation: The loop does not terminate as new elements are being added to the list in each iteration.


3.  i = 1

while True:

if i%3 == 0:

break

print(i)

i + = 1  =è space is the error (+=)

a) 1 2

b) 1 2 3

c) error

d) none of the mentioned

 

Answer: c

Explanation: SyntaxError, there shouldn’t be a space between + and = in +=.

 

4.  i = 1

while True:

if i%0O7 == 0:

break

print(i)

i += 1

a) 1 2 3 4 5 6

b) 1 2 3 4 5 6 7

c) error

d) none of the mentioned


Answer: a

Explanation: Control exits the loop when i becomes 7.


5.  i = 5

while True:

if i%0O11 == 0:  # print(int(0O11)) ==> 9

break

print(i)

i += 1

a) 5 6 7 8 9 10

b) 5 6 7 8

c) 5 6

d) error


Answer: b

Explanation: 0O11 is an octal number.

 

6.  i = 5

while True:

if i%0O9 == 0:

break

print(i)

i += 1


a) 5 6 7 8

b) 5 6 7 8 9

c) 5 6 7 8 9 10 11 12 13 14 15 ….

d) error


Answer: d

Explanation: 9 isn’t allowed in an octal number

 

7.  i = 1

while True:

if i%2 == 0:

break

print(i)

i += 2

a) 1

b) 1 2

c) 1 2 3 4 5 6 ………………

d) 1 3 5 7 9 11 ……………..


Answer: d

Explanation: The loop does not terminate since i is never an even number.

 

8.  i = 2

while True:

if i%3 == 0:

break

print(i)


i += 2

a) 2 4 6 8 10 …

b) 2 4

c) 2 3

d) error


Answer: b

Explanation: The numbers 2 and 4 are printed. The next value of i is 6 which is divisible by 3 and hence control exits the loop.

 

9.  i = 1

while False:

if i%2 == 0:

break

print(i)


i += 2

a) 1

b) 1 3 5 7 …

c) 1 2 3 4 …

d) none of the mentioned


Answer: d

Explanation: Control does not enter the loop because of False.

 

10. True = False

while True:

print(True)

break

a) True

b) False

c) None

d) none of the mentioned


Answer: d

Explanation: SyntaxError, True is a keyword and it’s value cannot be changed.


11. i = 0

while i < 5:

print(i)

i += 1

if i == 3:

break

else:

print(0)

a) 0 1 2 0

b) 0 1 2

c) error

d) none of the mentioned


Answer: b

Explanation: The else part is not executed if control breaks out of the loop.


12. i = 0

while i < 3:

print(i)

i += 1

else:

print(0)

a) 0 1 2 3 0

b) 0 1 2 0

c) 0 1 2

d) error


Answer: b

Explanation: The else part is executed when the condition in the while statement is false.


13. x = "abcdef"

while i in x:

print(i, end=" ")

a) a b c d e f

b) abcdef

c) i i i i i i …

d) error


Answer: d

Explanation: NameError, i is not defined.


14. x = "abcdef"

i = "i"

while i in x:

print(i, end=" ")

a) no output

b) i i i i i i …

c) a b c d e f

d) abcdef


Answer: a  

Explanation: “i” is not in “abcdef”


15. x = "abcdef"

i = "a"

while i in x:

print(i, end = " ")

a) no output

b) i i i i i i …

c) a a a a a a …

d) a b c d e f


Answer: c

Explanation: As the value of i or x isn’t changing, the condition will always evaluate to True.


16. x = "abcdef"

i = "a"

while i in x:

print('i', end = " ")

a) no output

b) i i i i i i …

c) a a a a a a …

d) a b c d e f


Answer: b

Explanation: As the value of i or x isn’t changing, the condition will always evaluate to True.


17. x = "abcdef"

i = "a"

while i in x:

x = x[:-1]

print(i, end = " ")

a) i i i i i i

b) a a a a a a

c) a a a a a

d) none of the mentioned


Answer: b

Explanation: The string x is being shortened by one character in each iteration.


18. x = "abcdef"

i = "a"

while i in x[:-1]:

print(i, end = " ")

a) a a a a a

b) a a a a a a

c) a a a a a a …

d) a


Answer: c

Explanation: String x is not being altered and i is in x[:-1]


19. x = "abcdef"

i = "a"

while i in x:

x = x[1:]

print(i, end = " ")

a) a a a a a a

b) a

c) no output

d) error


Answer: b

Explanation: The string x is being shortened by one character in each iteration.


20. x = "abcdef"

i = "a"

while i in x[1:]:

print(i, end = " ")

a) a a a a a a

b) a

c) no output

d) error


Answer: c

Explanation: i is not in x[1:].


21. x = 'abcd'

for i in x:

print(i)

x.upper()

a) a B C D

b) a b c d

c) A B C D

d) error


Answer: b

Explanation: Changes do not happen in-place, rather a new instance of the string is returned.


22. x = 'abcd'

for i in x:

print(i.upper())

a) a b c d

b) A B C D

c) a B C D

d) error


Answer: b

Explanation: The instance of the string returned by upper() is being printed.


23. x = 'abcd'

for i in range(x):

print(i)

a) a b c d

b) 0 1 2 3

c) error

d) none of the mentioned


Answer: c

Explanation: range(str) is not allowed.


24. x = 'abcd'

for i in range(len(x)):

print(i)

a) a b c d

b) 0 1 2 3

c) error

d) 1 2 3 4


Answer: b

Explanation: i takes values 0, 1, 2 and 3.


25. x = 'abcd'

for i in range(len(x)):

print(i.upper())

a) a b c d

b) 0 1 2 3

c) error

d) 1 2 3 4


Answer: c

Explanation: Objects of type int have no attribute upper()


26. x = 'abcd'

for i in range(len(x)):

i.upper()

print (x)

a) a b c d

b) 0 1 2 3

c) error

d) none of the mentioned


Answer: c

Explanation: Objects of type int have no attribute upper().


27. x = 'abcd'

for i in range(len(x)):

i.upper()

print (x)

a) a b c d

b) 0 1 2 3

c) error

d) none of the mentioned


Answer: c

Explanation: Objects of type int have no attribute upper().


28. x = 'abcd'

for i in range(len(x)):

i[x].upper()

print (x)

a) abcd

b) ABCD

c) error

d) none of the mentioned


Answer: c

Explanation: Objects of type int aren’t subscriptable. However, if the statement was x[i], 

an error would not have been thrown.


29. x = 'abcd'

for i in range(len(x)):

x = 'a'

print(x)


a) a

b) abcd abcd abcd

c) a a a a

d) none of the mentioned


Answer: c

Explanation: range() is computed only at the time of entering the loop.


30. x = 'abcd' 

for i in range(len(x)):

print(x)

x = 'a'


a) a

b) abcd abcd abcd abcd

c) a a a a

d) none of the mentioned


Answer: d

Explanation: abcd a a a is the output as x is modified only after ‘abcd’ has been printed once.


31. x = 123

for i in x:

print(i)


a) 1 2 3

b) 123

c) error

d) none of the mentioned


Answer: c

Explanation: Objects of type int are not iterable.


32. d = {0: 'a', 1: 'b', 2: 'c'}

for i in d:

print(i)


a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned


Answer: a

Explanation: Loops over the keys of the dictionary.


33. d = {0: 'a', 1: 'b', 2: 'c'}

for x, y in d:

print(x, y)


a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned


Answer: d

Explanation: Error, objects of type int aren’t iterable.


34. d = {0: 'a', 1: 'b', 2: 'c'}

for x, y in d.items():

print(x, y)


a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned


Answer: c

Explanation: Loops over key, value pairs.


35. d = {0: 'a', 1: 'b', 2: 'c'}

for x in d.keys():

print(d[x])


a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned


Answer: b

Explanation: Loops over the keys and prints the values.


36. d = {0: 'a', 1: 'b', 2: 'c'}

for x in d.values():

print(x)


a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned


Answer: b

Explanation: Loops over the values.


37. d = {0: 'a', 1: 'b', 2: 'c'}

for x in d.values():

print(d[x])


a) 0 1 2

b) a b c

c) 0 a 1 b 2 c

d) none of the mentioned


Answer: d

Explanation: Causes a KeyError.


38. d = {0, 1, 2}

for x in d.values():

print(x)


a) 0 1 2

b) None None None

c) error

d) none of the mentioned

Answer: c

Explanation: Objects of type set have no attribute values.


39. d = {0, 1, 2}

for x in d:

print(x)


a) 0 1 2

b) {0, 1, 2} {0, 1, 2} {0, 1, 2}

c) error

d) none of the mentioned


Answer: a

Explanation: Loops over the elements of the set and prints them.


40. d = {0, 1, 2}

for x in d:

print(d.add(x))


a) 0 1 2

b) 0 1 2 0 1 2 0 1 2 …

c) None None None

d) None of the mentioned


Answer: c

Explanation: Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.

41. What will be the output of the following Python code?

values = [[3, 4, 5, 1], [33, 6, 1, 2]] 

v = values[0][0]

for row in range(0, len(values)):

for column in range(0, len(values[row])):

if v < values[row][column]:

v = values[row][column]

print(v)

a) 3

b) 5

c) 6

d) 33


Answer: d

Explanation: Execute in the shell to verify.


42. What will be the output of the following Python code?

values = [[3, 4, 5, 1], [33, 6, 1, 2]]

v = values[0][0]

for lst in values:

for element in lst:

if v > element:

v = element

print(v)

a) 1

b) 3

c) 5

d) 6


Answer: a

Explanation: Execute in the shell to verify.


43. What will be the output of the following Python code?

matrix = [[1, 2, 3, 4],

        [4, 5, 6, 7],

        [8, 9, 10, 11],

        [12, 13, 14, 15]]

for i in range(0, 4):

print(matrix[i][1], end = " ")

a) 1 2 3 4

b) 4 5 6 7

c) 1 3 8 12

d) 2 5 9 13


Answer: d

Explanation: Execute in the shell to verify.

45. What will be the output of the following Python code?

values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]

for row in values:

row.sort()

for element in row:

print(element, end = " ")

a) The program prints two rows 3 4 5 1 followed by 33 6 1 2

b) The program prints on row 3 4 5 1 33 6 1 2

c) The program prints two rows 3 4 5 1 followed by 33 6 1 2

d) The program prints two rows 1 3 4 5 followed by 1 2 6 33


Answer: d

Explanation: Execute in the shell to verify.


46. What will be the output of the following Python code?

a= [1, 2, 3, 4, 5]

for i in range(1, 5):

a[i-1] = a[i]

for i in range(0, 5): 

print(a[i],end = " ")

a) 5 5 1 2 3

b) 5 1 2 3 4

c) 2 3 4 5 1

d) 2 3 4 5 5


Answer: d

Explanation: The items having indexes from 1 to 4 are shifted forward by one index due to 

the first for-loop and the item of index four are printed again because of the second for-loop.


47. What will be the output of the following Python code?

num = ['One', 'Two', 'Three']

for i, x in enumerate(num):

print('{}: {}'.format(i, x),end=" ")

a) 1: 2: 3:

b) Exception is thrown

c) One Two Three

d) 0: One 1: Two 2: ThreeV


Answer: d

Explanation: enumerate(iterator,start=0) is a built-in function which returns (0,lst[0]),(1,lst[1]) 

and so on where lst is a list(iterator).


48. What will be the output of the following Python code?

a = [1, 5, 7, 9, 9, 1]

b = a[0]   # b=1

x= 0

for x in range(1, len(a)):            # for x in range(1, 6)

if a[x] > b:

b = a[x]

b = x

print(b)

a) 5

b) 3

c) 4

d) 0


Answer: c

Explanation: The above piece of code basically prints the index of the largest element in the list.

49. What will be the output of the following Python code snippet?

a={1:"A", 2:"B", 3:"C"}

for i,j in a.items():

print(i,j,end=" ")

a) 1 A 2 B 3 C

b) 1 2 3

c) A B C

d) 1:”A” 2:”B” 3:”C”


Answer: a

Explanation: In the above code, variables i and j iterate over the keys and values of the dictionary respectively

50. What will be the output of the following Python code?

a={1:"A",2:"B",3:"C"}

for i in a:

print(i,end=" ")


a) 1 2 3

b) ‘A’ ‘B’ ‘C’

c) 1 ‘A’ 2 ‘B’ 3 ‘C’

d) Error, it should be: for i in a.items():


Answer: a

Explanation: The variable I iterates over the keys of the dictionary and hence the keys are printed.

51. What will be the output of the following Python code snippet?

a = {}

a[1] = 1

a['1'] = 2

a[1]=a[1]+1

count = 0

for i in a:

count += a[i]

print(count)

a) 1

b) 2

c) 4

d) Error, the keys can’t be a mixture of letters and numbers


Answer: c

Explanation: The above piece of code basically finds the sum of the values of keys.


52. What will be the output of the following Python code snippet?

a = {}

a[1] = 1

a['1'] = 2

a[1.0]=4

count = 0

for i in a:

count += a[i]

print(count)

a) An exception is thrown

b) 3

c) 6

d) 2


Answer: c

Explanation: The value of key 1 is 4 since 1 and 1.0 are the same. 

Then, the function count() gives the sum of all the values of the keys (2+4).


No comments:

Post a Comment