Font size
WorksheetsPython Quiz-4 List
Total questions: 111
Worksheet time: 58mins
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList()
nameList[1]
NameList(4)
nameList["4"]
The list needs one more name added to the end - "Felipe". Which piece of code below would do this?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]
The list will be slice and show Harry and Jesse only. Which piece of code below would do this?
print(nameList[1:3])
print(nameList[0:1])
print(nameList[1:2])
print(nameList["Harry":"Jesse"])
To insert an the string "Pedro" in the first position of a the nameList we use
nameList.append("Pedro, 1")
nameList.insert("Pedro",0)
nameList.insert(1, "Pedro")
nameList.insert(0, "Pedro")
variable.sort() will do what?
Sort the list into alphabetical order
Sort the list into reverse order
Create a new list
Error
What is the function of the reverse method for lists?
removes the first occurrence of the information inside the parenthesis
adds the information inside the parenthesis to the end of the list
reverses the order of the elements inside a list
sorts the list (lowest to highest for numbers and alphabetical order for strings)
nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]
del nameList[3]
print(nameList)
What would be the output
['John', 'Harry', 'Jesse', 'Marry', 'Larry']
['John', 'Harry', 'Jesse', 'John', 'Larry']
['John', 'Harry', 'John', 'Marry', 'Larry']
Error
nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]
print(nameList.count("John"))
What would be the output
1
2
3
Error
nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]
nameList[6] ="Susie"
print(nameList)
What would be the output
['John', 'Harry', 'Jesse', 'John', 'Marry', 'Larry', 'Susie']
['Susie', 'John', 'Harry', 'Jesse', 'John', 'Marry', 'Larry']
['Susie']
Error
What will be the output?
var1=['sam', "Pam", 12,44]
var1[3]="Ram"
print(var1)
['sam', 'Pam', 12, 44]
['sam', 'Pam', 12, 'Ram']
It will give an error
['sam', 'Pam', 'Ram',44]
What will be the output?
tuple1=['sam', "Pam", 12,44]
print(type(tuple1))
<class 'list'>
<class 'tuple'>
What will be the output?
list1=['sam', "Pam", 12,44]
print(list1[-1])
12
sam
No such index in the list
44
What will be the output?
list1=["Red","Green","Blue","Yellow"]
list1.append("Black")
print(list1)
<class 'list'>
['Red', 'Green', 'Blue', 'Yellow', 'Black']
['Black','Red', 'Green', 'Blue', 'Yellow' ]
AttributeError: 'tuple' object has no attribute 'append'
Which of the statements are true for the code below:
list1=["Red","Green","Yellow","Orange","Black"]
if "Red" in list1 and "Green" in list1 and "Yellow" in list1:
print("We have all the colors of traffic lights")
Print statement will be executed if the list has red/yellow/Blue
Print statement will be executed if the list has all the three colors
It will give syntax error
None of these
What will be the output?
list1=["Red","Green","Yellow","Orange","Black","Emerald blue"]
print(max(list1))
Emerald blue
Black
Red
Yellow
list1=["Red","Green","Yellow","Orange","Black","Emerald blue"]
list1.sort()
print(list1)
["Red","Green","Yellow","Orange","Black","Emerald blue"]
Not a valid function in Python3
['Black', 'Emerald blue', 'Green', 'Orange', 'Red', 'Yellow']
None
list1 = "a", "b", "c", "d";
print(type(list1))
Syntax not correct
("a", "b", "c", "d")
<class 'tuple'>
<class 'list'>
list1=("Red","Green","Yellow","Orange","Black")
list1.append("Emerald blue")
print(list1)
("Red","Green","Yellow","Orange","Black","Emerald blue")
("Red","Green","Yellow","Orange","Black")
AttributeError: 'tuple' object has no attribute 'append'
None of these
tup1=("Red","Green","Yellow","Orange","Black")
del tup1
print(tup1)
NameError: name 'tup1' is not defined
SyntaxError: invalid syntax no keyword del
("Red","Green","Yellow","Orange","Black")
None of these
Which of these is the best description of a list in Python?
A list is a collection of data that has an order and can be changed
A list is a lot of variables
A list is used for shopping
A list is a collection of data that cannot hold duplicated data and cannot be changed
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
List items have an index number. In the following list, which item has the index number of 3?
["John", "Harry", "Jesse", "John", "Harry", "Harry"]
"John"
"Harry"
"Jesse"
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList()
nameList[1]
NameList(4)
nameList["4"]
The list needs one more name added to the end - "Felipe". Which piece of code below would do this?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
Which of these is a list?
fruit = ["pineapples", "mangoes", "bananas"]
fruit = "pineapples", "mangoes", "bananas"
fruit = {"pineapples", "mangoes", "bananas"}
fruit = ("pineapples", "mangoes", "bananas")
If you want to create an empty list called islands, which of the following pieces of code is correct?
islands = { }
islands = ( )
islands = [ ]
islands = < >
Each item in a list has an "address" or index. The first index in a Python list is what?
1
0
a
A
What is the index of the name 'Paula' in the following list:
names = ["Paul","Phillip","Paula","Phillipa"]
0
1
2
3
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
del mariana_islands[0]
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Tinian', 'Rota']
['Saipan', 'Tinian']
['Saipan', 'Rota']
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
mariana_islands.pop()
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Tinian', 'Rota']
['Saipan', 'Tinian']
['Saipan', 'Rota']
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
mariana_islands.remove('Tinian')
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Tinian', 'Rota']
['Saipan', 'Tinian']
['Saipan', 'Rota']
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota', 'Guam']
mariana_islands.sort()
print(mariana_islands)
['Tinian', 'Saipan', 'Rota', 'Guam']
['Guam', 'Tinian', 'Rota', 'Saipan']
['Saipan', 'Rota', 'Guam', 'Tinian']
['Guam', 'Rota', 'Saipan', 'Tinian']
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
mariana_islands.insert(0, 'Guam')
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Guam', 'Tinian', 'Rota']
['Saipan', 'Tinian', 'Rota', 'Guam']
['Guam', 'Saipan', 'Tinian', 'Rota']
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
mariana_islands.append('Guam')
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Guam', 'Tinian', 'Rota']
['Saipan', 'Tinian', 'Rota', 'Guam']
['Guam', 'Saipan', 'Tinian', 'Rota']
What is the output of program below?
mariana_islands = ['Saipan', 'Tinian', 'Rota']
mariana_islands[0] = 'Guam'
print(mariana_islands)
['Saipan', 'Tinian', 'Rota']
['Guam', 'Tinian', 'Rota']
['Saipan', 'Tinian', 'Rota', 'Guam']
['Guam', 'Saipan', 'Tinian', 'Rota']
What is the output of program below?
mariana_islands = ['Guam', 'Saipan', 'Tinian', 'Rota']
print(mariana_islands[-1])
Guam
Saipan
Tinian
Rota
What is the output of program below?
mariana_islands = ['Guam', 'Saipan', 'Tinian', 'Rota']
print(mariana_islands[1])
Guam
Saipan
Tinian
Rota
What is the output of the program below?
mariana_islands = ['Guam', 'Saipan', 'Tinian', 'Rota']
cmni = mariana_islands[1:]
len(cmni)
1
2
3
4
What is the output of the following piece of code when executed in Python shell?
>>> a=("Check")*3
>>> a
(‘Check’,’Check’,’Check’)
* Operator not valid for tuples
(‘CheckCheckCheck’)
Syntax error
Is the following piece of code valid?
>>> a=(1,2,3,4)
>>> del a
No because tuple is immutable
Yes, the entire tuple is deleted
No, invalid syntax for del method
Yes, first element in the tuple is deleted
What type of data is: a=[(1,1),(2,4),(3,9)]?
Array of tuples
List of tuples
Tuples of lists
Invalid type
Which of the following is true for a tuple?
Tup = 1,2,3
Tup = (""1,"2)
Tup = (1,2,3,4)
None of the above
Result of the program above is
('1', '2', '3', '4', '5')
(1, 2, 3, 4, 5)
12345
"12345"
Suppose you have the following tuple definition:
t = ('foo', 'bar', 'baz')
Which of the following statements replaces the second element ('bar') with the string 'qux':
t[1] = 'qux'
t(1) = 'qux'
t[1:1] = 'qux'
error
If list=[17,23,41,10] then list.append(32) will result
[32,17,23,41,10]
[17,23,41,10,32]
[10,17,23,32,41]
[41,32,23,17,10]
Suppose list=[12,3,22,"mago","hello"], what is list[-6]
Error
None
mango
12
L_names=['Harsh','Amit','Sahil','Viresh']
min(L_names)
'Harsh'
'Amit'
'Viresh'
None of the above
L_names=['Harsh','Amit','Sahil','Viresh']
L_names.pop()
'Harsh'
'Amit'
'Viresh'
None of the above
If L1=[2,4,8,16], L2=[32,64] what is L1+L2
[2,4,8,16,32,64]
[2,4,8,16][32,64]
[2,4,8,16],[32,64]
None of the above
If L1=[2,4,8,16] What is the result of the following statement
16 in L1
True
False
L=[2,4,8,16,32,64] what is L[:2]
[2,4]
[2,4,8]
2,4
None of the above
T=((1,2,3),(3,4))
what is T[1][1]
4
3
2
1
Which function can be used to convert list to a tuple
tuple
list
to_list
to_tuple
Suppose list1 is [1, 3, 2], What is list1 * 2 ?
[2, 6, 4].
[1, 3, 2, 1, 3]
[1, 3, 2, 1, 3, 2]
[1, 3, 2, 3, 2, 1]
To remove string “hello” from list1, we use which command?
list1.remove(“hello”)
list1.remove(hello)
list.remove(“hello”)
list.remove(hello)
Suppose a tuple T is declared as T=(20,36,34,49), which of the following is incorrect?
print(T[3])
T[3] = 49
print(min(T))
print(T.count(20))
Identify the data type of T:
T = [‘A’, ‘23’, ‘92’, ‘(10,20)’]
List
Dictionary
String
Tuple
What is the output of the code?
a = (33, 55)
a.append(22)
print(a)
33 55 22
33
55
22
33, 55, 22
Error
T/F A list can contain a group.
True
False
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
List items have an index number. In the following list, which item has the index number of 3?
["John", "Harry", "Jesse", "John", "Harry", "Harry"]
"John"
"Harry"
"Jesse"
"John"
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList()
nameList[1]
NameList(4)
nameList["4"]
The list needs one more name added to the end - "Felipe". Which piece of code below would do this?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
To insert an the string "strawberries" in the first position of a list we use
fruit.append("strawberries, 1")
fruit.insert("strawberries",0)
fruit.insert(1, "strawberries")
fruit.insert(0, "strawberries")
To print a list called fruitList we use
print(fruitList)
list(print)
fruitList.print()
fruitList(print)
Which colour will be replaced with yellow?
red
green
blue
none, yellow will be added to the list
Which symbols are used to open and close a list?
( ) round brackets
( ) curly brackets
[ ] square brackets
" " speech marks
variable.sort() will do what?
Sort the list into alphabetical order
Sort the list into reverse order
Create a new list
Error
What is the function of the reverse method for lists?
removes the first occurrence of the information inside the parenthesis
adds the information inside the parenthesis to the end of the list
reverses the order of the elements inside a list
sorts the list (lowest to highest for numbers and alphabetical order for strings)
If list=[17,23,41,10] then list.append(32) will result
[32,17,23,41,10]
[17,23,41,10,32]
[10,17,23,32,41]
[41,32,23,17,10]
Which of the following python function can be used to add more than one element within an existing list ?
append( )
append_more( )
extend( )
more( )
What is the output of the following list operation
aList = [10, 20, 30, 40, 50, 60, 70, 80]
print(aList[2:5])
print(aList[:4])
print(aList[3:])
[20, 30, 40, 50]
[10, 20, 30, 40]
[30, 40, 50, 60, 70, 80]
[30, 40, 50]
[10, 20, 30, 40]
[40, 50, 60, 70, 80]
None of these
In Python, list is mutable
False
True
___________method adds a single item to the end of the list.
extend( )
append( )
insert( )
list1=[10, 20, 30, 40] and list2=[15, 25, 35, 45] What is the value of list1 when the statement,
list1+=list2
is executed?
[10, 20, 30, 40]
[25, 45, 65, 85]
[10, 15, 20, 25, 30, 35, 40, 45]
[10, 20, 30, 40, 15, 25, 35, 45]
lista=["a111", "b222", "c333", "d444"]
The statement lista.pop(2) is executed. Which of the following is the correct representation of lista after the statement, lista.pop(2) is executed?
‘b222’
'c333’
['a111', 'b222', 'd444’]
["a111", "b222", "c333", "d444"]
Consider a tuple, fruits= (‘apple’, ‘orange’, ‘watermelon’, ‘grapes’, ‘banana’).
What is the output for the statement print(fruits[-2])?
‘banana’
‘grapes’
‘watermelon’
‘orange’
Which of the following is TRUE about tuples?
Tuples represent elements of the same data type.
The length of a tuple can be changed.
New elements are inserted to tuples using the append() method.
Elements of a tuple cannot be modified.
Consider the list L= [0, 1, 1, 2, 3, 5, 8, 13, 21, 34].What will be output of the statement L [3:6]?
[2, 3, 5]
[0, 1, 1]
[1, 2, 3]
[2, 3, 5, 8]
Choose the correct about Tuples
Collection of items which are ordered
tuples cannot be changed
tuples uses square brackets
values of tuples can be accessed by indexing
Choose the correct syntax of Tuple
tuple=[200,50]
tuple=(200,50)
tuple={200,50}
Which of the following is FALSE about lists?
A list is an ordered collection.
A list is a collection of heterogeneous elements.
A list consists of elements of the same data type only.
A list consists of zero or more elements.
Guess the output:
li = [2, 3, 5]
li.append("Rahul")
print(li)
SyntaxError
[2, 3, 5, 'Rahul']
['Rahul', 2, 3, 5]
The count() method returns the number of times the specified element appears in the list.
What is the output of this code?
3
2
1
0
Output of this code?
[2,4,6,8,10]
['2','4','6','8','10']
[0,2,4,6,8]
[10,8,6,4,2]
print([i.lower() for i in "HELLO"])
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
‘hello’
[‘hello’]
hello
print([[i+j for i in "abc"] for j in "def"])
[‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
[[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
[[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
[‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]
print([if i%2==0: i; else: i+1; for i in range(4)])
[0, 2, 2, 4]
[1, 1, 3, 3]
error
none of the mentioned
Write the list comprehension to pick out only negative integers from a given list ‘a’.
[x<0 in a]
[x for x<0 in a]
[x in a for x<0]
[x for x in a if x<0]
Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9]
[x**3 for x in l]
[x*3 for x in l]
[x**3 in l]
[x^3 in l]
Read the information given below carefully and write a list comprehension such that the output is: [‘e’, ‘o’]
w="hello"
v=('a', 'e', 'i', 'o', 'u')
[x for w in v if x in v]
[x for x in w if x in v]
[x for x in v if w in v]
[x for v in w for x in w]
Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.
[x in range(1, 1000) if x%3==0]
[x for x in range(1000) if x%3==0]
[x%3 for x in range(1, 1000)]
[x%3=0 for x in range(1, 1000)]
Write a list comprehension equivalent for the Python code shown below.
[i for i in range(1, 100) if int(i*0.5)==(i*0.5)]
[i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
[i for i in range(1, 101) if int(i*0.5)=(i*0.5)]
[i for i in range(1, 100) if int(i*0.5)=(i*0.5)]
Write a list comprehension to produce the list: [1, 2, 4, 8, 16, ..., 1024].
[2**x for x in range(0, 10)]
[2**x for x in range(11)]
[2*x for x in range(0, 11)]
[2*x for x in range(1, 11)]
What will be the output of the following Python code?
[‘good’, ‘oh’, ‘excellent’, ‘450’ ]
[‘good’]
[‘good’, ‘#450’]
[‘oh!’, ‘excellent!’, ‘#450’]
grades = [2.0, 3.0, 4.0]
Which of the following code can be used to iterate over the grades list ?
for grade in grades :
# todo
i = 0
while i < len(grades) :
#todo
i = i + 1
[print ( i ) for i in grades]
for i, grade in enumerate(grades) :
# todo
a = [ 1, 3, 5 ]
b = [ 7, 9, 11 ]
How do you merge these two lists ?
a + b
a.extend(b)
a.merge(b)
numbers = [1, 2, 3, 4, 5]
Which of the following code removes all the elements from the list but retains the list
numbers.clear()
del numbers
delete numbers
numbers.del()
numbers = [1,2, 3, 4, 5, 6, 7, 8, 9, 10]
Which of the following code will give you the output as the array below
[1, 3, 5, 7, 9]
numbers[::2]
numbers[:2]
numbers[0:9:2]
numbers[2:0:9]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Which of the following code will give the output as
[4, 6, 8]
[numbers[i] for i in [3, 5,7]]
numbers[4,6,8]
numbers[3,5,7]
names = ["Hyderabad", "New Delhi", "New York"]
What gets printed when you say
names[1][-1]
i
d
k
y
What is the output of the output of the code above ?
1700
1200
800
2400
The output of the code above is
Chennai
Hyderabad
New York
New Delhi
What is the output of the program above
Error
-1
0
None
False
