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

SEQUENCE DATA TYPES IN PYTHON

00:00:00

What is the output of print(k) in the following Python code snippet?

  k = [print(i) for i in my_string if i not in "aeiou"]  print(k)

Option A. all characters of my_string that aren’t vowels
Option B. a list of Nones
Option C. list of Trues
Option D. list of Falses
True Answer B

Explanation :
print() returns None.

What is the output of print(k) in the following Python code snippet?

  k = [print(i) for i in my_string if i not in "aeiou"]  print(k)

Question No# : 01 out of 20



Write the list comprehension to pick out only negative integers from a given list ‘l’.

Option A. [x<0 in l]
Option B. [x for x<0 in l]
Option C. [x in l for x<0]
Option D. [x for x in l if x<0]
True Answer D

Explanation :

To pick out only the negative numbers from a given list ‘l’, the correct list comprehension statement would be: [x for x in l if x<0].

For example if we have a list l=[-65, 2, 7, -99, -4, 3]
>>> [x for x in l if x<0]
The output would be: [-65, -99, -4].

Write the list comprehension to pick out only negative integers from a given list ‘l’.

Question No# : 02 out of 20



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

 

  1.   d1 = {"john":40, "peter":45}
  2.   d2 = {"john":466, "peter":45}
  3.   d1 > d2

 

 

Option A. True
Option B. False
Option C. Error
Option D. None
True Answer C

Explanation :
Arithmetic &gt; operator cannot be used with dictionaries.

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

 

  1.   d1 = {"john":40, "peter":45}
  2.   d2 = {"john":466, "peter":45}
  3.   d1 > d2

 

 

Question No# : 03 out of 20



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

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

  a.setdefault(4,"D") 

print(a)

Option A. {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
Option B. None
Option C. Error
Option D. [1,3,6,10]
True Answer A

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

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

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

  a.setdefault(4,"D") 

print(a)

Question No# : 04 out of 20



What will be the output of the following Python code?

  1.   names1 = ['Amir', 'Bear', 'Charlton', 'Daman']
  2.   names2 = names1
  3.   names3 = names1[:]
  4.    
  5.   names2[0] = 'Alice'
  6.   names3[1] = 'Bob'
  7.    
  8.   sum = 0
  9. for ls in (names1, names2, names3):
  10.       if ls[0] == 'Alice':
  11.           sum += 1
  12.       if ls[1] == 'Bob':
  13.           sum += 10
  14.    
  15.   print sum

Option A. 11
Option B. 12
Option C. 21
Option D. 22
True Answer B

Explanation :
When assigning names1 to names2, we create a second reference to the same list. Changes to names2 affect names1. When assigning the slice of all elements in names1 to names3, we are creating a full copy of names1 which can be modified independently.

What will be the output of the following Python code?

  1.   names1 = ['Amir', 'Bear', 'Charlton', 'Daman']
  2.   names2 = names1
  3.   names3 = names1[:]
  4.    
  5.   names2[0] = 'Alice'
  6.   names3[1] = 'Bob'
  7.    
  8.   sum = 0
  9. for ls in (names1, names2, names3):
  10.       if ls[0] == 'Alice':
  11.           sum += 1
  12.       if ls[1] == 'Bob':
  13.           sum += 10
  14.    
  15.   print sum

Question No# : 05 out of 20



Is the following Python code valid?

>>> a=2,3,4,5  >>> a

Option A. Yes, 2 is printed
Option B. Yes, [2,3,4,5] is printed
Option C. No, too many values to unpack
Option D. Yes, (2,3,4,5) is printed
True Answer D

Explanation :
A tuple needn’t be enclosed in parenthesis.

Is the following Python code valid?

>>> a=2,3,4,5  >>> a

Question No# : 06 out of 20



What will be the output of the following Python code?

  A = [[1, 2, 3],       [4, 5, 6],       [7, 8, 9]]  [A[i][i] for i in range(len(A))]

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

Explanation :
We can also perform tasks like pulling out a diagonal. The expression shown above uses range to generate the list of offsets and the indices with the row and column the same, picking out A[0][0], then A[1][1] and so on. Hence the output of the code is: [1, 5, 9].

What will be the output of the following Python code?

  A = [[1, 2, 3],       [4, 5, 6],       [7, 8, 9]]  [A[i][i] for i in range(len(A))]

Question No# : 07 out of 20



What will be the output of the following Python code?

  x=[[1],[2]]  print(" ".join(list(map(str,x))))

Option A. [1] [2]
Option B. [49] [50]
Option C. Syntax error
Option D. [[1]] [[2]]
True Answer A

Explanation :
The elements 1 and 2 are first put into separate lists and then combined with a space in between using the join attribute.

What will be the output of the following Python code?

  x=[[1],[2]]  print(" ".join(list(map(str,x))))

Question No# : 08 out of 20



What will be the output of the following Python code?

>>> import collections

>>> a=dict()  >>> a=collections.defaultdict(str) 

>>> a['A']

Option A. An exception is thrown since the dictionary is empty
Option B. ‘ ‘
Option C. ‘A’
Option D. 0
True Answer B

Explanation :
The statement a=collections.defaultdict(str) gives the default value of ‘ ‘ even if the keys don’t exist in the dictionary.

What will be the output of the following Python code?

>>> import collections

>>> a=dict()  >>> a=collections.defaultdict(str) 

>>> a['A']

Question No# : 09 out of 20



What will be the output of the following Python code?

'{0:f}, {1:2f}, {2:05.2f}'.format(1.23456, 1.23456, 1.23456)

Option A. Error
Option B. ‘1.234560, 1.22345, 1.23’
Option C. No output
Option D. ‘1.234560, 1.234560, 01.23’
True Answer D

Explanation :
In the code shown above, various formatting options are displayed using the format option. Hence the output of this code is: ‘1.234560, 1.234560, 01.23’

What will be the output of the following Python code?

'{0:f}, {1:2f}, {2:05.2f}'.format(1.23456, 1.23456, 1.23456)

Question No# : 10 out of 20



What will be the output of the following Python code?

  1. >>>list1 = [1, 3]
  2. >>>list2 = list1
  3. >>>list1[0] = 4
  4. >>>print(list2)

Option A. [1, 3]
Option B. [4, 3]
Option C. [1, 4]
Option D. [1, 3, 4]
True Answer B

Explanation :
Lists should be copied by executing [:] operation.

What will be the output of the following Python code?

  1. >>>list1 = [1, 3]
  2. >>>list2 = list1
  3. >>>list1[0] = 4
  4. >>>print(list2)

Question No# : 11 out of 20



What will be the output of the following Python code?

>>> a=(2,3,4)  >>> sum(a,3)

Option A. Too many arguments for sum() method
Option B. The method sum() doesn’t exist for tuples
Option C. 12
Option D. 9
True Answer C

Explanation :
In the above case, 3 is the starting value to which the sum of the tuple is added to.

What will be the output of the following Python code?

>>> a=(2,3,4)  >>> sum(a,3)

Question No# : 12 out of 20



What will be the output of the following Python code?

>>> a=(1,2,(4,5))  >>> b=(1,2,(3,4))  >>> a<b

Option A. False
Option B. True
Option C. Error, &lt; operator is not valid for tuples
Option D. Error, &lt; operator is valid for tuples but not if there are sub-tuples
True Answer A

Explanation :
Since the first element in the sub-tuple of a is larger that the first element in the subtuple of b, False is printed.

What will be the output of the following Python code?

>>> a=(1,2,(4,5))  >>> b=(1,2,(3,4))  >>> a<b

Question No# : 13 out of 20



What will be the output of the following Python code?

l=list('HELLO')

'first={0[0]}, third={0[2]}'.format(l)

Option A. ‘first=H, third=L’
Option B. ‘first=0, third=2’
Option C. Error
Option D. ‘first=0, third=L’
True Answer A

Explanation :
In the code shown above, the value for first is substituted by l[0], that is H and the value for third is substituted by l[2], that is L. Hence the output of the code shown above is: ‘first=H, third=L’. The list l= [‘H’, ‘E’, ‘L’, ‘L’, ‘O’].

What will be the output of the following Python code?

l=list('HELLO')

'first={0[0]}, third={0[2]}'.format(l)

Question No# : 14 out of 20



What will be the output of the following Python code?

  a=165  b=sum(list(map(int,str(a))))  print(b)

Option A. 561
Option B. 5
Option C. 12
Option D. Syntax error
True Answer C

Explanation :
First, map converts the number to string and then places the individual digits in a list. Then, sum finds the sum of the digits in the list. The code basically finds the sum of digits in the number.

What will be the output of the following Python code?

  a=165  b=sum(list(map(int,str(a))))  print(b)

Question No# : 15 out of 20



What will be the output of the following Python code?

  count={}  count[(1,2,4)] = 5  count[(4,2,1)] = 7  count[(1,2)] = 6  count[(4,2,1)] = 2  tot = 0  for i in count:      tot=tot+count[i]  print(len(count)+tot)

Option A. 25
Option B. 17
Option C. 16
Option D. Tuples can’t be made keys of a dictionary
True Answer C

Explanation :
Tuples can be made keys of a dictionary. Length of the dictionary is 3 as the value of the key (4,2,1) is modified to 2. The value of the variable tot is 5+6+2=13.

What will be the output of the following Python code?

  count={}  count[(1,2,4)] = 5  count[(4,2,1)] = 7  count[(1,2)] = 6  count[(4,2,1)] = 2  tot = 0  for i in count:      tot=tot+count[i]  print(len(count)+tot)

Question No# : 16 out of 20



Is the following Python code valid?

>>> a,b,c=1,2,3  >>> a,b,c

Option A. Yes, [1,2,3] is printed
Option B. No, invalid syntax
Option C. Yes, (1,2,3) is printed
Option D. 1 is printed
True Answer C

Explanation :
A tuple needn’t be enclosed in parenthesis.

Is the following Python code valid?

>>> a,b,c=1,2,3  >>> a,b,c

Question No# : 17 out of 20



Which of the following is constructed by placing expressions within square brackets?

Option A. Tuples
Option B. Lists
Option C. Classes
Option D. quadrats
True Answer B

Explanation :
List in is constructed by placing expressions within square brackets separated by commas

Which of the following is constructed by placing expressions within square brackets?

Question No# : 18 out of 20

What will be the output of the following Python code?

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

  a.clear() 

print(a)

Option A. None
Option B. { None:None, None:None, None:None}
Option C. {1:None, 2:None, 3:None}
Option D. { }
True Answer D

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

What will be the output of the following Python code?

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

  a.clear() 

print(a)

Question No# : 19 out of 20



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

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

Option A. {1, 4, 9, 16}
Option B. {0, 1, 4, 9, 16}
Option C. Error
Option D. {0, 1, 4, 9}
True 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}.

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

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

Question No# : 20 out of 20




CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad