QUIZ ON : PROGRAMMING AND PROBLEM SOLVING THROUGH PYTHON - SEQUENCE DATA TYPES IN PYTHON

SEQUENCE DATA TYPES IN PYTHON

00:00:00

What will be the output of the following Python code?

>>>m = [[x, x + 1, x + 2] for x in range(0, 3)]

Option A. [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Option B. [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Option C. [1, 2, 3, 4, 5, 6, 7, 8, 9]
Option D. [0, 1, 2, 1, 2, 3, 2, 3, 4]
True Answer B

Explanation :
Execute in the shell to verify.

What will be the output of the following Python code?

>>>m = [[x, x + 1, x + 2] for x in range(0, 3)]

Question No# : 01 out of 20



What will be the output of the following Python code?

  1. >>>t=(1,2,4,3)
  2. >>>t[1:3]

Option A. (1, 2)
Option B. (1, 2, 4)
Option C. (2, 4)
Option D. (2, 4, 3)
True Answer C

Explanation :
Slicing in tuples takes place just as it does in strings.

What will be the output of the following Python code?

  1. >>>t=(1,2,4,3)
  2. >>>t[1:3]

Question No# : 02 out of 20



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

Option A. {1: ‘A’, 2: ‘B’}
Option B. dict([[1,”A”],[2,”B”]])
Option C. {1,”A”,2”B”}
Option D. { }
True Answer C

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

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

Question No# : 03 out of 20



What will be the output of the following Python code?

  l1=[10, 20, 30]  l2=[-10, -20, -30]  l3=[x+y for x, y in zip(l1, l2)]  l3

Option A. Error
Option B. 0
Option C. [-20, -60, -80]
Option D. [0, 0, 0]
True Answer D

Explanation :
The code shown above returns x+y, for x belonging to the list l1 and y belonging to the list l2. That is, l3=[10-10, 20-20, 30-20], which is, [0, 0, 0].

What will be the output of the following Python code?

  l1=[10, 20, 30]  l2=[-10, -20, -30]  l3=[x+y for x, y in zip(l1, l2)]  l3

Question No# : 04 out of 20



What will be the output of the following Python code?

  1.     values = [[3, 4, 5, 1], [33, 6, 1, 2]]
  2.      
  3.     v = values[0][0]
  4. for lst in values:
  5.         for element in lst:
  6.             if v > element:
  7.                 v = element
  8.      
  9.     print(v)

Option A. 1
Option B. 3
Option C. 5
Option D. 6
True Answer A

Explanation :
Execute in the shell to verify.

What will be the output of the following Python code?

  1.     values = [[3, 4, 5, 1], [33, 6, 1, 2]]
  2.      
  3.     v = values[0][0]
  4. for lst in values:
  5.         for element in lst:
  6.             if v > element:
  7.                 v = element
  8.      
  9.     print(v)

Question No# : 05 out of 20



What will be the output of the following Python code?

'{0:.2f}'.format(1.234)

Option A. ‘1’
Option B. ‘1.234’
Option C. ‘1.23’
Option D. ‘1.2’
True Answer C

Explanation :
The code shown above displays the string method to round off a given decimal number to two decimal places. Hence the output of the code is: ‘1.23’.

What will be the output of the following Python code?

'{0:.2f}'.format(1.234)

Question No# : 06 out of 20



Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:

Option A. [0, 1, 2, 3]
Option B. [0, 1, 2, 3, 4]
Option C. [0.0, 0.5, 1.0, 1.5]
Option D. [0.0, 0.5, 1.0, 1.5, 2.0]
True Answer C

Explanation :
Execute in the shell to verify.

Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:

Question No# : 07 out of 20



What will be the output of the following Python code?

  1.     data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
  2.      
  3.     def ttt(m):
  4.         v = m[0][0]
  5.      
  6.         for row in m:
  7.             for element in row:
  8.                if v < element: v = element
  9.      
  10.         return v
  11.      
  12.     print(ttt(data[0]))

Option A. 1
Option B. 2
Option C. 4
Option D. 5
True Answer C

Explanation :
Execute in the shell to verify.

What will be the output of the following Python code?

  1.     data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
  2.      
  3.     def ttt(m):
  4.         v = m[0][0]
  5.      
  6.         for row in m:
  7.             for element in row:
  8.                if v < element: v = element
  9.      
  10.         return v
  11.      
  12.     print(ttt(data[0]))

Question No# : 08 out of 20



Which of the following functions will return the symmetric difference between two sets, x and y?

Option A. x | y
Option B. x ^ y
Option C. x &amp; y
Option D. x – y
True Answer C

Explanation :
The function x ^ y returns the symmetric difference between the two sets x and y. This is basically an XOR operation being performed on the two sets.

Which of the following functions will return the symmetric difference between two sets, x and y?

Question No# : 09 out of 20



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

Option A. 0
Option B. 2
Option C. Error as the key-value pair of 1:’A’ is already deleted
Option D. 1
True Answer B

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

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

Question No# : 10 out of 20



Which of the following commands will create a list?

Option A. list1 = list()
Option B. list1 = []
Option C. list1 = list([1, 2, 3])
Option D. all of the mentioned
True Answer D

Explanation :
Execute in the shell to verify

Which of the following commands will create a list?

Question No# : 11 out of 20



Input order is preserved in sets.

Option A. TRUE
Option B. FALSE
Option C.
Option D.
True Answer B

Explanation :

The input order in sets is not maintained. This is demonstrated by the code shown below:

>>> s={2, 6, 8, 1, 5}

>>>

{8, 1, 2, 5, 6}

Input order is preserved in sets.

Question No# : 12 out of 20



What will be the output of the following Python code?

  a=[1,2,3,4]  b=[sum(a[0:x+1]) for x in range(0,len(a))]  print(b)

Option A. 10
Option B. [1,3,5,7]
Option C. 4
Option D. [1,3,6,10]
True Answer D

Explanation :
The above code returns the cumulative sum of elements in a list.

What will be the output of the following Python code?

  a=[1,2,3,4]  b=[sum(a[0:x+1]) for x in range(0,len(a))]  print(b)

Question No# : 13 out of 20



What will be the output of the following Python code?

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

>>> b=a.copy()  >>> b.add(4) 

>>> a

Option A. {1,2,3}
Option B. Error, invalid syntax for add
Option C. {1,2,3,4}
Option D. Error, copying of sets isn’t allowed
True 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.

What will be the output of the following Python code?

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

>>> b=a.copy()  >>> b.add(4) 

>>> a

Question No# : 14 out of 20



What will be the output of the following Python code?

  1.   d = {"john":40, "peter":45}
  2.   d["john"]

Option A. 40
Option B. 45
Option C. “john”
Option D. “peter”
True Answer A

Explanation :
Execute in the shell to verify.

What will be the output of the following Python code?

  1.   d = {"john":40, "peter":45}
  2.   d["john"]

Question No# : 15 out of 20



What will be the output of the following Python code?

  A = [[1, 2, 3],       [4, 5, 6],       [7, 8, 9]]  B = [[3, 3, 3],       [4, 4, 4],       [5, 5, 5]]  [B[row][col]*A[row][col] for row in range(3) for col in range(3)]

Option A. [3, 6, 9, 16, 20, 24, 35, 40, 45]
Option B. Error
Option C. [0, 30, 60, 120, 160, 200, 300, 350, 400]
Option D. 0
True Answer A

Explanation :
In the code shown above, we have used list comprehension to combine values of multiple matrices. We have multiplied the elements of the matrix B with that of the matrix A, in the range(3). Hence the output of this code is: [3, 6, 9, 16, 20, 24, 35, 40, 45].

What will be the output of the following Python code?

  A = [[1, 2, 3],       [4, 5, 6],       [7, 8, 9]]  B = [[3, 3, 3],       [4, 4, 4],       [5, 5, 5]]  [B[row][col]*A[row][col] for row in range(3) for col in range(3)]

Question No# : 16 out of 20



To shuffle the list(say list1) what function do we use?

Option A. list1.shuffle()
Option B. shuffle(list1)
Option C. random.shuffle(list1)
Option D. random.shuffleList(list1)
True Answer C

Explanation :
Execute in the shell to verify.

To shuffle the list(say list1) what function do we use?

Question No# : 17 out of 20



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

Option A. {(3, 4), (1, 2)}
Option B. Error
Option C. {(4, 2), (3, 1), (4, 1), (5, 2)}
Option D. {(3, 1), (4, 2)}
True 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)}.

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

Question No# : 18 out of 20



Is the following Python code valid?

>>> a=(1,2,3)  >>> b=('A','B','C')  >>> c=zip(a,b)

Option A. Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
Option B. Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
Option C. No because tuples are immutable
Option D. No because the syntax for zip function isn’t valid
True Answer A

Explanation :
Zip function combines individual elements of two iterables into tuples. Execute in Python shell to verify.

Is the following Python code valid?

>>> a=(1,2,3)  >>> b=('A','B','C')  >>> c=zip(a,b)

Question No# : 19 out of 20



In order to store values in terms of key and value we use what core data type.

Option A. list
Option B. tuple
Option C. class
Option D. dictionary
True Answer D

Explanation :
Dictionary stores values in terms of keys and values.

In order to store values in terms of key and value we use what core data type.

Question No# : 20 out of 20