Sunday, January 10, 2021

General Python PEP-8 standards

PEP stands for Python Enhancement Proposal.
  • PEP-8, is a document that provides guidelines and best practices on how to write Python code. 
  • It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. 
  • The primary focus of PEP 8 is to improve the readability and consistency of Python code.

Note: C
learly  I will Update this page later once i got time. till that time follow below  links.

Link : https://www.datacamp.com/community/tutorials/pep8-tutorial-python-code#intro

Link :  https://www.python.org/dev/peps/pep-0008/


Thursday, January 7, 2021

Python Identifier Interview questions - Multiple choice

 ******** Identifiers Interview Questions **********


Is Python case sensitive when dealing with identifiers?

a) yes

b) no

c) machine dependent

d) none of the mentioned


Answer: a

Explanation: Case is always significant.


What is the maximum possible length of an identifier?

a) 31 characters

b) 63 characters

c) 79 characters

d) none of the mentioned


Answer: d

Extra Information: According to the references, Python Documentation: Identifiers, “Identifiers are unlimited in length.”

As a practical matter you should probably not use names longer than about a dozen characters

Identifiers can be of any length.


Which of the following is invalid?

a) _a = 1

b) __a = 1

c) __str__ = 1

d) none of the mentioned


Answer: d

Explanation: All the statements will execute successfully but at the cost of reduced readability.


Which of the following is an invalid variable?

a) my_string_1

b) 1st_string

c) foo

d) _

View Answer


Answer: b

Explanation: Variable names should not start with a number.


Which of the following is not a keyword?

a) eval

b) assert

c) nonlocal

d) pass


Answer: a

Explanation: eval can be used as a variable.


All keywords in Python are in

a) lower case

b) UPPER CASE

c) Capitalized

d) None of the mentioned


Answer: d

Explanation: True, False and None are capitalized while the others are in lower case.


Which of the following is true for variable names in Python?

a) unlimited length

b) all private members must have leading and trailing underscores

c) underscore and ampersand are the only two special characters allowed

d) none of the mentioned


Answer: a

Explanation: Variable names can be of any length.


Which of the following is an invalid statement?

a) abc = 1,000,000

b) a b c = 1000 2000 3000

c) a,b,c = 1000, 2000, 3000

d) a_b_c = 1,000,000


Answer: b

Explanation: Spaces are not allowed in variable names.


Which of the following cannot be a variable?

a) __init__

b) in

c) it

d) on


Answer: b

Explanation: in is a keyword.


Request: If you find this information useful, please provide your valuable comments.

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).


Python Set Interview questions - Multiple choice

Set Interview questions :

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

 1. Which of these about a set is not true?

a) Mutable data type

b) Not Allows duplicate values

c) Data type with unordered values

d) Immutable data type


Answer: d

Explanation: A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations.


2. Which of the following is not the correct syntax for creating a set?

a) set([[1,2],[3,4]])

b) set([1, 2, 2, 3, 4])

c) set((1, 2,3,4))

d) {1, 2, 3, 4}


Answer: a

Explanation: The argument given for the set must be iterable.


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

nums = set([1,1,2,3,3,3,4,4])

print(len(nums))

a) 7

b) Error, invalid syntax for formation of set

c) 4

d) 8


Answer: c

Explanation: A set doesn’t have duplicate items.


4. Which of the following statements is used to create an empty set?

a) { }

b) set()

c) [ ]

d) ( )


Answer: b

Explanation: { } creates a dictionary not a set. Only set() creates an empty set.


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

>>> a={5,4}

>>> b={1,2,4,5}

>>> a<b

a) {1,2}

b) True

c) False

d) Invalid operation


Answer: b

Explanation: a<b returns True if a is a proper subset of b.


6. If a={5, 6, 7, 8}, which of the following statements is false?

a) print(len(a))

b) print(min(a))

c) a.remove(5)

d) a[2]=45


Answer: d

Explanation: The members of a set can be accessed by their index values since the elements of the set are unordered.


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

>>> a={3, 4, 5}

>>> a.update([1, 2, 3])

>>> print(a)

a) Error, no method called update for set data type

b) {1, 2, 3, 4, 5}

c) Error, list can’t be added to set

d) Error, duplicate item present in list


Answer: b

Explanation: The method update adds elements to a set.


8. If a = {5, 6, 7}, what happens when a.add(5) is executed?

a) a={5, 5, 6, 7}

b) a={5, 6, 7}

c) Error as there is no add function for set data type

d) Error as 5 already exists in the set


Answer: b

Explanation: 5 isn’t added again to the set and the set consists of only non-duplicate elements and 5 already exist in the set.


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

>>> a = {4, 5, 6}

>>> b={2, 8, 6}

>>> a+b

a) {4,5,6,2,8}

b) {4,5,6,2,8,6}

c) Error as unsupported operand type for sets

d) Error as the duplicate item 6 is present in both sets


Answer: c

Explanation: Execute in python shell to verify.


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

>>> a={4,5,6}

>>> b={2,8,6}

>>> a-b

a) {4, 5}

b) {6}

c) Error as unsupported operand type for set data type

d) Error as the duplicate item 6 is present in both sets


Answer: a

Explanation: – operator gives the set of elements in set a but not in set b.


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

>>> s = {5,6}

>>> s*3

a) Error as unsupported operand type for set data type

b) {5,6,5,6,5,6}

c) {5,6}

d) Error as multiplication creates duplicate elements which isn’t allowed


Answer: a

Explanation: The multiplication operator isn’t valid for the set data type.


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

>>> a = {5, 6, 7, 8}

>>> b = {7, 5, 6, 8}

>>> a == b

a) True

b) False


Answer: a

Explanation: It is possible to compare two sets and the order of elements in both 

the sets don’t matter if the values of the elements are the same.


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

>>> a={3, 4, 5}

>>> b={5, 6, 7}

>>> a|b  # union

a) Invalid operation

b) {3, 4, 5, 6, 7}

c) {5}

d) {3,4,6,7}


Answer: d

Explanation: The operation in the above piece of code is a union operation. 

This operation produces a set of elements in both set a and set b.


15. Is the following Python code valid?

a={3,4,{7,5}}

print(a[2][0])

a) Yes, 7 is printed

b) Error, elements of a set can’t be printed

c) Error, subsets aren’t allowed

d) Yes, {7,5} is printed


Answer: c

Explanation: In python, elements of a set must not be mutable and sets are mutable. Thus, subsets can’t exist


16. Which of these about a frozenset is not true?

a) Mutable data type

b) Allows duplicate values

c) Data type with unordered values

d) Immutable data type


Answer: a

Explanation: A frozenset is an immutable data type


17. What is the syntax of the following Python code?

>>> a=frozenset(set([5,6,7]))

>>> a

a) {5,6,7}

b) frozenset({5,6,7})

c) Error, not possible to convert set into frozenset

d) Syntax error


Answer: b

Explanation: The above piece of code is the correct syntax for creating a frozenset.


18. Is the following Python code valid?

>>> a=frozenset([5,6,7])

>>> a

>>> a.add(5)

a) Yes, now a is {5,5,6,7}

b) No, frozen set is immutable

c) No, invalid syntax for add method

d) Yes, now a is {5,6,7}


Answer: b

Explanation: Since a frozen set is immutable, add method doesn’t exist for frozen method.


19. Set members must not be hashable.

a) True

b) False


Answer: b

Explanation: Set members must always be hashable.


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

>>> a={1,2,3}

>>> a.intersection_update({2,3,4,5})

>>> a

a) {2,3}

b) Error, duplicate item present in list

c) Error, no method called intersection_update for set data type

d) {1,4,5}


Answer: a

Explanation: The method intersection_update returns a set that is an intersection of both sets.


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

>>> a={1,2,3}

>>> b=a

>>> b.remove(3)

>>> a

a) {1,2,3}

b) Error, copying of sets isn’t allowed

c) {1,2}

d) Error, invalid syntax for remove


Answer: c

Explanation: Any change made in b is reflected in a because b is an alias of a.


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

>>> a={1,2,3}

>>> b=a.copy()

>>> b.add(4)

>>> a

a) {1,2,3}

b) Error, invalid syntax for add

c) {1,2,3,4}

d) Error, copying of sets isn’t allowed


Answer: a

Explanation: In the above piece of code, b is barely a copy and not an alias of a. Hence any change made in b isn’t reflected in a.


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

>>> a={1,2,3}

>>> b=a.add(4)

>>> b


a) None

b) {1,2,3,4}

c) {1,2,3}

d) Nothing is printed


Answer: d

Explanation: The method add returns nothing, hence nothing is printed.


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

>>> a={1,2,3}

>>> b=frozenset([3,4,5])

>>> a-b

a) {1,2}

b) Error as difference between a set and frozenset can’t be found out

c) Error as unsupported operand type for set data type

d) frozenset({1,2})


Answer: a

Explanation: – operator gives the set of elements in set a but not in set b


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

>>> a={5,6,7}

>>> sum(a,5)

a) 5

b) 23

c) 18

d) Invalid syntax for sum method, too many arguments


Answer: b

Explanation: The second parameter is the start value for the sum of elements in set a. Thus, sum(a,5) = 5+(5+6+7)=23


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

>>> a={1,2,3}

>>> {x*2 for x in a|{4,5}}


a) {2,4,6}

b) Error, set comprehensions aren’t allowed

c) {8, 2, 10, 4, 6}

d) {8,10}


Answer: c

Explanation: Set comprehensions are allowed.


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


>>> a={5,6,7,8}

>>> b={7,8,9,10}

>>> len(a+b)


a) 8

b) Error, unsupported operand ‘+’ for sets

c) 6

d) Nothing is displayed


Answer: b

Explanation: Duplicate elements in a+b is eliminated and the length of a+b is computed.


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


a={1,2,3}

b={1,2,3}

c=a.issubset(b)

print(c)


a) True

b) Error, no method called issubset() exists

c) Syntax error for issubset() method

d) False


Answer: a

Explanation: The method issubset() returns True if b is a proper subset of a.


15. Is the following Python code valid?


a={1,2,3}

b={1,2,3,4}

c=a.issuperset(b)

print(c)


a) False

b) True

c) Syntax error for issuperset() method

d) Error, no method called issuperset() exists


Answer: a

Explanation: The method issubset() returns True if b is a proper subset of a.


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


s=set()

type(s)


a) <’set’>

b) <class ‘set’>

c) set

d) class set


Answer: b

Explanation: When we find the type of a set, the output returned is: .


2. The following Python code results in an error.


s={2, 3, 4, [5, 6]}


a) True

b) False


Answer: a

Explanation: The set data type makes use of a principle known as hashing. 

This means that each item in the set should be hashable. Hashable in this context means immutable. List is mutable and hence 

the line of code shown above will result in an error


3. Set makes use of __________

Dictionary makes use of ____________


a) keys, keys

b) key values, keys

c) keys, key values

d) key values, key values


Answer: c

Explanation: Set makes use of keys.

Dictionary makes use of key values.


4. Which of the following lines of code will result in an error?


a) s={abs}

b) s={4, ‘abc’, (1,2)}

c) s={2, 2.2, 3, ‘xyz’}

d) s={san}


Answer: d

Explanation: The line: s={san} will result in an error because ‘san’ is not defined. 

The line s={abs} does not result in an error because abs is a built-in function. 

The other sets shown do not result in an error because all the items are hashable.


7. Write a list comprehension for number and its cube for:


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


a) [x**3 for x in l]

b) [x^3 for x in l]

c) [x**3 in l]

d) [x^3 in l]


Answer: a

Explanation: The list comprehension to print a list of cube of the numbers for the given list is: [x**3 for x in l].


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


s={1, 2, 3}

s.update(4)

s


a) {1, 2, 3, 4}

b) {1, 2, 4, 3}

c) {4, 1, 2, 3}

d) Error


Answer: d

Explanation: The code shown above will result in an error because the argument given to 

the function update should necessarily be an iterable. Hence if we write this function as: s.update([4]), there will be no error.


9. Which of the following functions cannot be used on heterogeneous sets?


a) pop

b) remove

c) update

d) sum


Answer: d

Explanation: The functions sum, min and max cannot be used on mixed type (heterogeneous) sets. 

The functions pop, remove, update etc can be used on homogenous as well as heterogeneous sets. 

An example of heterogeneous sets is: {‘abc’, 4, (1, 2)}


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


s={4>3, 0, 3-3}

all(s)

any(s)


a)

True

False


b)

False

True


c)

True 

True


d)

False

False


Answer: b

Explanation: The function all returns true only if all the conditions given are true. 

But in the example shown above, we have 0 as a value. Hence false is returned. 

Similarly, any returns true if any one condition is true. Since the condition 4>3 is true, true is returned


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


z=set('abc$de')

'a' in z

a) True

b) False

c) No output

d) Error


Answer: a

Explanation: The code shown above is used to check whether a particular item is a part of a given set or not. 

Since ‘a’ is a part of the set z, the output is true. Note that this code would result in an error in the absence of the quotes.


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


z=set('abc')

z.add('san')

z.update(set(['p', 'q']))

z


a) {‘abc’, ‘p’, ‘q’, ‘san’}

b) {‘a’, ‘b’, ‘c’, [‘p’, ‘q’], ‘san}

c) {‘a’, ‘c’, ‘c’, ‘p’, ‘q’, ‘s’, ‘a’, ‘n’}

d) {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}


Answer: d

Explanation: The code shown first adds the element ‘san’ to the set z. 

The set z is then updated and two more elements, namely, ‘p’ and ‘q’ are added to it. 

Hence the output is: {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}


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


s=set([1, 2, 3])

s.union([4, 5])

s|([4, 5])


a)

   {1, 2, 3, 4, 5}

   {1, 2, 3, 4, 5}


b)

   Error

   {1, 2, 3, 4, 5}


c)

   {1, 2, 3, 4, 5}

   Error


d)

   Error

   Error

   

Answer: c

Explanation: The first function in the code shown above returns the set {1, 2, 3, 4, 5}. 

This is because the method of the function union allows any iterable. 

However the second function results in an error because of unsupported data type, that is list and set.


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


for x in set('pqr'):

print(x*2)


a)

pp

qq

rr

b)

pqr

pqr

c) ppqqrr

d) pqrpqr


Answer: a

Explanation: The code shown above prints each element of the set twice separately. Hence the output of this code is:

pp

qq

rr


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


{a**2 for a in range(4)}


a) {1, 4, 9, 16}

b) {0, 1, 4, 9, 16}

c) Error

d) {0, 1, 4, 9}


Answer: d

Explanation: The code shown above returns a set containing the square of values in the range 0-3, 

that is 0, 1, 2 and 3. Hence the output of this line of code is: {0, 1, 4, 9}.


7. What will be the output of the following Python function?


{x for x in 'abc'}

{x*3 for x in 'abc'}


a)

   {abc}

aaa

bbb

ccc


b)

abc

   abc abc abc


c)

{‘a’, ‘b’, ‘c’}

   {‘aaa’, ‘bbb’, ‘ccc’}


d)

{‘a’, ‘b’, ‘c’}

abc

abc

abc


Answer: c

Explanation: The first function prints each element of the set separately, hence the output is: {‘a’, ‘b’, ‘c’}. 

The second function prints each element of the set thrice, contained in a new set. 

Hence the output of the second function is: {‘aaa’, ‘bbb’, ‘ccc’}. (Note that the order may not be the same)


8. The output of the following code is: class<’set’>.


type({})


a) True

b) False


Answer: b

Explanation: The output of the line of code shown above is: class<’dict’>. This is because {} represents an empty dictionary, 

whereas set() initializes an empty set. Hence the statement is false.


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


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

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

a==b

set(a)==set(b)


a)

   True

   False


b)

   False

   False


c)

   False

   True


d)

True

True


Answer: c

Explanation: In the code shown above, when we check the equality of the two lists, a and b, 

we get the output false. This is because of the difference in the order of elements of the two lists. 

However, when these lists are converted to sets and checked for equality, the output is true. 

This is known as order-neutral equality. Two sets are said to be equal if and only if they contain exactly 

the same elements, regardless of order.


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


l=[1, 2, 4, 5, 2, 'xy', 4]

set(l)

l


a)

{1, 2, 4, 5, 2, ‘xy’, 4}

[1, 2, 4, 5, 2, ‘xy’, 4]


b)


{1, 2, 4, 5, ‘xy’}

[1, 2, 4, 5, 2, ‘xy’, 4]


c)


{1, 5, ‘xy’}

[1, 5, ‘xy’]


d)


{1, 2, 4, 5, ‘xy’}

[1, 2, 4, 5, ‘xy’]


Answer: b

Explanation: In the code shown above, the function set(l) converts the given list into a set. 

When this happens, all the duplicates are automatically removed. Hence the output is: {1, 2, 4, 5, ‘xy’}. 

On the other hand, the list l remains unchanged. Therefore the output is: [1, 2, 4, 5, 2, ‘xy’, 4].

Note that the order of the elements may not be the same.


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


s1={3, 4}

s2={1, 2}

s3=set()

i=0

j=0

for i in s1:

for j in s2:

s3.add((i,j))

i+=1

j+=1

print(s3)


a) {(3, 4), (1, 2)}

b) Error

c) {(4, 2), (3, 1), (4, 1), (5, 2)}

d) {(3, 1), (4, 2)}


Answer: c

Explanation: The code shown above finds the Cartesian product of the two sets, s1 and s2. 

The Cartesian product of these two sets is stored in a third set, that is, s3. 

Hence the output of this code is: {(4, 2), (3, 1), (4, 1), (5, 2)}.


2. The ____________ function removes the first element of a set and the last element of a list.


a) remove

b) pop

c) discard

d) dispose


Answer: b

Explanation: The function pop removes the first element when used on a set and the last element when used to a list.


3. The difference between the functions discard and remove is that:


a) Discard removes the last element of the set whereas remove removes the first element of the set

b) Discard throws an error if the specified element is not present in the set whereas remove does not throw an error in case of absence of the specified element

c) Remove removes the last element of the set whereas discard removes the first element of the set

d) Remove throws an error if the specified element is not present in the set whereas discard does not throw an error in case of absence of the specified element


Answer: d

Explanation: The function remove removes the element if it is present in the set. If the element is not present, 

it throws an error. The function discard removes the element if it is present in the set. 

If the element is not present, no action is performed (Error is not thrown).


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


s1={1, 2, 3}

s2={3, 4, 5, 6}

s1.difference(s2)

s2.difference(s1)


a)


{1, 2}

{4, 5, 6}


b)


{1, 2}

{1, 2}


c)


{4, 5, 6}

{1, 2}


d)

{4, 5, 6}

{4, 5, 6}


Answer: a

Explanation: The function s1.difference(s2) returns a set containing the elements 

which are present in the set s1 but not in the set s2. Similarly, the function s2.difference(s1) 

returns a set containing elements which are present in the set s2 but not in the set s1. Hence the output of the code shown above will be:

{1, 2}

{4, 5, 6}.


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


s1={1, 2, 3}

s2={4, 5, 6}

s1.isdisjoint(s2)

s2.isdisjoint(s1)


a)


True

False


b)


False 

True


c)


True

True


d)


False

False


Answer: c

Explanation: The function isdisjoint returns true the two sets in question are disjoint, that is if they do not have even a single element in common. 

The two sets s1 and s2 do not have any elements in common, hence true is returned in both the cases.


6. If we have two sets, s1 and s2, and we want to check if all the elements of s1 are present in s2 or not, we can use the function:


a) s2.issubset(s1)

b) s2.issuperset(s1)

c) s1.issuperset(s2)

d) s1.isset(s2)


Answer: b

Explanation: Since we are checking whether all the elements present in the set s1 are present in the set s2. 

This means that s1 is the subset and s1 is the superset. Hence the function to be used is: s2.issuperset(s1). This operation can also be performed by the function: s1.issubset(s2).


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


s1={1, 2, 3, 8}

s2={3, 4, 5, 6}

s1|s2

s1.union(s2)


a)


{3}

{1, 2, 3, 4, 5, 6, 8}


b)


{1, 2, 4, 5, 6, 8}

{1, 2, 4, 5, 6, 8}


c)


{3}

{3}


d)


{1, 2, 3, 4, 5, 6, 8}

{1, 2, 3, 4, 5, 6, 8}


Answer: d

Explanation: The function s1|s2 as well as the function s1.union(s2) returns a union of the two sets s1 and s2. 

Hence the output of both of these functions is: {1, 2, 3, 4, 5, 6, 8}


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


a=set('abc')

b=set('def')

b.intersection_update(a)

a

b


a)


set()

(‘e’, ‘d’, ‘f’}


b)


{}

{}


c)


{‘b’, ‘c’, ‘a’}

set()


d)


set()

set()


Answer: c

Explanation: The function b.intersection_update(a) puts those elements in the set b which are common to both the sets a and b. 

The set a remains as it is. Since there are no common elements between the sets a and b, the output is:‘b’, ‘c’, ‘a’} set().


9. What will be the output of the following Python code, if s1= {1, 2, 3}?


s1.issubset(s1)


a) True

b) Error

c) No output

d) False


Answer: a

Explanation: Every set is a subset of itself and hence the output of this line of code is true.


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


x=set('abcde')

y=set('xyzbd')

x.difference_update(y)

x

y


a)


   {‘a’, ‘b’, ‘c’, ‘d’, ‘e’}

   {‘x’, ‘y’, ‘z’}

b)


   {‘a’, ‘c’, ‘e’}

   {‘x’, ‘y’, ‘z’, ‘b’, ‘d’}

c)


   {‘b’, ‘d’}

   {‘b’, ‘d’}

d)


   {‘a’, ‘c’, ‘e’}

   {‘x’, ‘y’, ‘z’}


Answer: b

Explanation: The function x.difference_update(y) removes all the elements of the set y from the set x. 

Hence the output of the code is:{‘a’, ‘c’, ‘e’} {‘x’, ‘y’, ‘z’, ‘b’, ‘d’}.


Request: If you find this information useful, please provide your valuable comments.


Python Dictionary Interview questions - Multiple choice

 Dictionary:

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

1.Which of the following statements create a dictionary?

a) d = {}

b) d = {“john”:40, “peter”:45}

c) d = {40:”john”, 45:”peter”}

d) All of the mentioned


Answer: d

Explanation: Dictionaries are created by specifying keys and values.


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

d = {"john":40, "peter":45}

"john" in d

a) True

b) False

c) None

d) Error


Answer: a

Explanation: In can be used to check if the key is in the dictionary.


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

d1 = {"john":40, "peter":45}

d2 = {"john":466, "peter":45}

d1 == d2

a) True

b) False

c) None

d) Error


Answer: b

Explanation: If d2 was initialized as d2 = d1 the answer would be true.


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

d1 = {"john":40, "peter":45}

d2 = {"john":466, "peter":45}

d1 > d2

a) True

b) False

c) Error

d) None


Answer: c

Explanation: Arithmetic > operator cannot be used with dictionaries.


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

d = {"john":40, "peter":45}

d["john"]

a) 40

b) 45

c) “john”

d) “peter”


Answer: a

Explanation: Execute in the shell to verify.


6. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?

a) d.delete(“john”:40)

b) d.delete(“john”)

c) del d[“john”]

d) del d(“john”:40)


Answer: c

Explanation: Execute in the shell to verify.


7. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in the dictionary which command do we use?

a) d.size()

b) len(d)

c) size(d)

d) d.len()


Answer: b

Explanation: Execute in the shell to verify.


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

d = {"john":40, "peter":45}

print(list(d.keys()))

a) [“john”, “peter”]

b) [“john”:40, “peter”:45]

c) (“john”, “peter”)

d) (“john”:40, “peter”:45)


Answer: a

Explanation: The output of the code shown above is a list containing only keys of the dictionary d, in the form of a list.


9. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression d[“rohith”]?

a) Since “rohith” is not a value in the set, Python raises a KeyError exception

b) It is executed fine and no exception is raised, and it returns None

c) Since “rohith” is not a key in the set, Python raises a KeyError exception

d) Since “rohith” is not a key in the set, Python raises a syntax error


Answer: c

Explanation: Execute in the shell to verify.


10. Which of these about a dictionary is false?

a) The values of a dictionary can be accessed using keys

b) The keys of a dictionary can be accessed using values

c) Dictionaries aren’t ordered

d) Dictionaries are mutable


Answer: b

Explanation: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.


11. Which of the following is not a declaration of the dictionary?

a) {1: ‘A’, 2: ‘B’}

b) dict([[1,”A”],[2,”B”]])

c) {1, "A", 2, "B"}

d) { }


Answer: c

Explanation: Option c is a set, not a dictionary.


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

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

print(a.get(1,4))

a) 1

b) A

c) 4

d) Invalid syntax for get method.


Answer: b

Explanation: The get() method returns the value of the key if the key is present in 

the dictionary and the default value(second parameter) if the key isn’t present in the dictionary.


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

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

print(a.get(5,4))

a) Error, invalid syntax

b) A

c) 5

d) 4


Answer: d

Explanation: The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.


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

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

print(a.setdefault(3))

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}

b) C

c) {1: 3, 2: 3, 3: 3}

d) No method called setdefault() exists for dictionary


Answer: b

Explanation: setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.


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

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

a.setdefault(4,"D")

print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}

b) None

c) Error

d) [1,3,6,10]


Answer: a

Explanation: setdefault() will set dict[key]=default if key is not already in the dictionary.


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

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

b={4:"D", 5:"E"}

a.update(b)

print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}

b) Method update() doesn’t exist for dictionaries

c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}

d) {4: ‘D’, 5: ‘E’}


Answer: c

Explanation: update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.


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

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

b=a.copy()

b[2]="D"

print(a)

a) Error, copy() method doesn’t exist for dictionaries

b) {1: ‘A’, 2: ‘B’, 3: ‘C’}

c) {1: ‘A’, 2: ‘D’, 3: ‘C’}

d) “None” is printed


Answer: b

Explanation: .copy performs deep copy (creates separate memory location)Changes made in the 2nd dictionary aren’t reflected in the original one.


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

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

a.clear()

print(a)

a) None

b) { None:None, None:None, None:None}

c) {1:None, 2:None, 3:None}

d) { }


Answer: d

Explanation: The clear() method clears all the key-value pairs in the dictionary.


19. Which of the following isn’t true about dictionary keys?

a) When duplicate keys are encountered, the last assignment wins

b) Keys must be immutable

c) Keys must be integers

d) None


Answer: c

Explanation: Keys of a dictionary may be any data type that is immutable.


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

a={1:5, 2:3, 3:4}

a.pop(2)

print(a)

a) {1: 5}

b) {1: 5, 2: 3}

c) Error, syntax error for pop() method

d) {1: 5, 3: 4}


Answer: d

Explanation: pop() method removes the key-value pair for the key mentioned in the pop() method.


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

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

>>> a.items()

a) Syntax error

b) dict_items([(‘A’), (‘B’), (‘C’)])

c) dict_items([(1,2,3)])

d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])


Answer: d

Explanation: The method items() returns the list of tuples with each tuple having a key-value pair.


22. Which of the statements about dictionary values is false?

a) More than one key can have the same value

b) The values of the dictionary can be accessed as dict[key]

c) Values of a dictionary must be unique

d) Values of a dictionary can be a mixture of letters and numbers


Answer: c

Explanation: More than one key can have the same value.


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

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

>>> del a

a) method del doesn’t exist for the dictionary

b) del deletes the values in the dictionary

c) del deletes the entire dictionary

d) del deletes the keys in the dictionary


Answer: c

Explanation: del deletes the entire dictionary and any further attempt to access it will throw an error.


24. If a is a dictionary with some key-value pairs, what does a.popitem() do?

a) Removes an arbitrary element

b) Removes all the key-value pairs

c) Removes the key-value pair for the key given as an argument

d) Invalid method for dictionary


Answer: a

Explanation: The method pop item() removes a random key-value pair.


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

numbers = letters = narendra= {}

numbers[1] = 56

numbers[3] = 7

letters[4] = 'B'

narendra['Numbers'] = numbers

narendra['Letters'] = letters

print(narendra)

a) Error, dictionary in a dictionary can’t exist

b) ‘Numbers’: {1: 56, 3: 7}

c) {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}

d) {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}


Answer: d

Explanation: Dictionary in a dictionary (nested dictionaries) can exist.


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

test = {1:'A', 2:'B', 3:'C'}

test = {}

print(len(test))

a) 0

b) None

c) 3

d) An exception is thrown


Answer: a

Explanation: In the second line of code, the dictionary becomes an empty dictionary. Thus, length=0.


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

test = {1:'A', 2:'B', 3:'C'}

del test[1]

test[1] = 'D'

del test[2]

print(len(test))

a) 0

b) 2

c) Error as the key-value pair of 1:’A’ is already deleted

d) 1


Answer: b

Explanation: After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.


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

a={}

a['a']=1

a['b']=[2,3,4]

print(a)

a) Exception is thrown

b) {‘b’: [2], ‘a’: 1}

c) {‘b’: [2], ‘a’: [3]}

d) {‘b’: [2, 3, 4], ‘a’: 1}


Answer: d

Explanation: Dictionary is unordered. Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.


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

a = {}

a[2] = 1

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

print(a[1][1])

a) [2,3,4]

b) 3

c) 2

d) An exception is thrown


Answer: (b) 

Explanation: Now, a={1:[2,3,4],2:1} . a[1][1] refers to second element having key 1.


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

a={'B':5, 'A':9, 'C':7}

sorted(a)

a) [‘A’,’B’,’C’]

b) [‘B’,’C’,’A’]

c) [5,7,9]

d) [9,5,7]


Answer: a

Explanation: Return a new sorted list of keys in the dictionary.


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

>>> a={}

>>> a.fromkeys( [1, 2, 3], "check")

a) Syntax error

b) {1:”check”, 2:”check”, 3:”check”}

c) “check”

d) {1:None, 2:None, 3:None}


Answer: b

Explanation: The dictionary takes values of keys from the list and initializes them to the default value 

(value is given in the second parameter). Execute in Python shell to verify.


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

b = {}

all(b)

a) { }

b) False

c) True

d) An exception is thrown


Answer: c

Explanation: Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.


33. If b is a dictionary, what does any(b) do?

a) Returns True if any key of the dictionary is true

b) Method any() doesn’t exist for dictionary

c) Returns True if all keys of the dictionary are true

d) None


Answer: a

Explanation: Method any() returns True if any key of the dictionary is true and False if the dictionary is empty.


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

        a = {"a":1, "b":2, "c":3}

b = dict(zip(a.values(),a.keys()))

        print(b)

a) {‘a’: 1, ‘b’: 2, ‘c’: 3}

b) An exception is thrown

c) {‘a’: ‘b’: ‘c’: }

d) {1: ‘a’, 2: ‘b’, 3: ‘c’}


Answer: d

Explanation: The above piece of code inverts the key-value pairs in the dictionary.


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

a = dict()

a[1]

a) An exception is thrown since the dictionary is empty

b) ‘ ‘

c) 1

d) 0


Answer: a

Explanation: The values of a dictionary can be accessed through the keys only if the keys exist in the dictionary.


36. what is the output of the following code snippet?

    dict1 = {'a': 10, 'b': 8}
    dict2 = {'d': 6, 'c': 4}
    print(dict(dict1, **dict2))
Answer: {'a': 10, 'b': 8, 'd': 6, 'c': 4}
========================================================

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

print(list(zip((1,2,3),('a'),('xxx','yyy'))))

print(list(zip((2,4),('b','c'),('yy','xx'))))

a) [(1,2,3),(‘a’),(‘xxx’,’yyy’)]

[(2,4),(‘b’,’c’),(‘yy’,’xx’)]

b) [(1, 'a', 'xxx'),(2,’ ‘,’yyy’),(3,’ ‘,’ ‘)]

[(2, 'b', 'yy'), (4, 'c', 'xx')]

c) Syntax error

d) [(1, 'a', 'xxx')]

[(2, 'b', 'yy'), (4, 'c', 'xx')]


Answer: d

Explanation: The zip function combines the individual attributes of the lists into a list of tuples.


Request: If you find this information useful, please provide your valuable comments.