WorksheetsLists: Introduction and Basic Creation
Total questions: 84
Worksheet time: 42mins
Which statement best defines a Python list as introduced in this section?
A fixed-size container that cannot change once created
An ordered, changeable collection that allows duplicate members
An unordered collection of unique items only
A data type used only for numeric values
Which of the following directly demonstrates list mutability as described?
Lists are created using parentheses
Lists can grow and shrink as needed
Lists cannot contain duplicate items
Lists are not a sequence type
What syntax is used to declare a list with initial values according to the example?
Curly braces with semicolons between values
Parentheses with spaces between values
Square brackets with values separated by commas
Angle brackets with colon-separated values
In the example list1 = [1,2,3,'A','B',7,8,[10,11]], what concept does the inner [10,11] illustrate?
Tuple conversion
List immutability
Nested list
Set membership
Which Python list operation is demonstrated by the expression [1, 2, 3] + [4, 5, 6] producing [1, 2, 3, 4, 5, 6]?
Length
Concatenation
Repetition
Membership
In the table of basic list operations, what does the expression 3 in [1, 2, 3] evaluate to?
False
True
3
[3]
Given L = ['mrcet', 'college', 'MRCET!'], what does L[2] return?
'mrcet'
'college'
'MRCET!'
IndexError
Which statement best describes list indexing based on the example L[2] -> MRCET! ?
Offsets start at one
Offsets start at zero
Negative indexes are not allowed
Indexes must be even numbers
Which operation is shown by the code for x in [1, 2, 3]: print x producing 1 2 3?
Membership
Iteration
Concatenation
Slicing
What does a negative index like L[-2] refer to in Python lists?
The second element from the left
The last element
The second element from the right
An invalid position
If list1 = [1,2,3,4,5,6,7,8,9,10], what is the result of list1[:1]?
[1]
[2]
[1, 2]
[]
You have x = [1, 2, 4, 6, 7]. Which call inserts the value 3 at index 2 to obtain [1, 2, 3, 4, 6, 7]?
x.insert(2, 3)
x.insert(3, 2)
x.append(3)
x.extend([3])
Starting with x = [5, 3, 8, 6], which code deletes the element at index 1 so x becomes [5, 8, 6]?
del x(1)
x.remove(1)
del x[1]
x.pop(1.0)
Given x = [1, 2, 10, 4, 6, 7], what does x.pop() return?
[1, 2, 10, 4, 6]
7
6
[1, 2, 10, 4, 6, 7]
After executing x = [1, 2, 10, 4, 6, 7]; x.pop(), what is the value of x?
[1, 2, 10, 4, 6]
[1, 2, 10, 4, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
[1, 2, 4, 6]
What item is removed and returned by x.pop(2) when x = [1, 2, 10, 4, 6]?
1
2
10
4
After running x = [1, 33, 2, 10, 4, 6]; x.remove(33), what is x?
[1, 33, 2, 10, 4, 6]
[1, 2, 10, 4, 6]
[33, 2, 10, 4, 6]
[1, 2, 4, 6]
Which statement best describes remove()?
Removes by index and returns the element
Removes the specified item value from the list
Reverses the list in place
Sorts the list in descending order
Which method reverses the order of elements in a list in place?
reverse()
reversed()
invert()
flip()
If x = [1, 2, 3, 4, 5, 6, 7] and x.reverse() is executed, what is x afterward?
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
[1, 3, 5, 7, 2, 4, 6]
[2, 3, 4, 5, 6, 7, 1]
What does sort() do to a list of numbers by default?
Sorts in descending order
Sorts in ascending order
Randomizes the order
Removes duplicates then sorts
Given x = [7, 6, 5, 4, 3, 2, 1]; x.sort(); what is x?
[7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7]
[1, 3, 5, 7, 2, 4, 6]
[7, 5, 3, 1, 2, 4, 6]
For x = [10, 1, 5, 3, 8, 7], what is the result after x.sort()?
[10, 1, 5, 3, 8, 7]
[1, 3, 5, 7, 8, 10]
[10, 8, 7, 5, 3, 1]
[1, 5, 3, 7, 8, 10]
In Python, which function is commonly used with a for loop to iterate over list indices?
range()
map()
input()
print()
Which statement best defines a mutable object in Python?
An object that cannot be changed after creation
An object that can be changed after it is created
An object that is always numeric
An object stored in read-only memory
Given x = [1,5,8,4], what is x after executing x.append(10)?
[1,5,8,4]
[1,5,8,4,10]
[10,1,5,8,4]
[1,5,8,4,[10]]
Given a = [81, 82, 83]; b = a; b[0] = 5; what is printed by print(a)?
[81, 82, 83]
[5, 82, 83]
[81, 5, 83]
Raises an error
Which option correctly shows a conditional list comprehension that keeps z**2 only when z > 4 for z in range(10)?
[z**2 for z in range(10) if z > 4]
[z**2 if z > 4 for z in range(10)]
[for z in range(10) if z > 4: z**2]
list(map(lambda z: z**2 if z > 4, range(10)))
Which output matches the list comprehension [x**2 for x in range(1, 11) if x % 2 == 1]?
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 9, 25, 49, 81]
[2, 4, 6, 8, 10]
[1, 3, 5, 7, 9]
Which statement best describes a list comprehension in Python?
A way to declare immutable sequences using parentheses
A concise way to create new lists by applying an expression to items in an iterable and optionally filtering them
A function that sorts a list in place
A special loop that can only be used with numbers
Which statement best defines a Python tuple?
An unordered, changeable collection written with square brackets
An ordered, unchangeable collection written with round brackets
An ordered, changeable collection written with curly braces
An unordered, unchangeable collection written with angle brackets
Which property of tuples is highlighted as a reason to use them when list contents should not change?
Tuples are sortable by default
Tuples are immutable
Tuples automatically remove duplicates
Tuples support arithmetic operations
According to the material, which statement about tuple efficiency is correct?
Tuples are less efficient than lists due to Python’s implementation
Tuples and lists have identical efficiency in all cases
Tuples are more efficient than lists due to Python’s implementation
Efficiency is not discussed for tuples
Which of the following is NOT a shown way to construct a tuple in the material?
x = ()
x = (1,2,3)
x = tuple(list1)
x = {1,2,3}
Given x = [4, 5, 66, 9], what is the result of y = tuple(x) followed by printing y?
[4, 5, 66, 9]
(4, 5, 66, 9)
(4; 5; 66; 9)
{4, 5, 66, 9}
Which output is shown when creating an empty tuple using x = () and then printing x?
None
[]
()
{}
Which of the following lists includes only operations mentioned as applicable to tuples in the material?
Access items, change items, loop, count, index, length
Append, pop, sort, reverse
Insert, remove, extend, clear
Map, filter, reduce
Tuples support all operations for sequences, but which nuance is noted about their elements?
Elements must all be numbers
Elements cannot be accessed by index
Member objects may be mutable
Elements are automatically sorted
What will be printed by the code: x=('a','b','c','g'); print(x[2])
a
b
c
g
Given x=(2,5,7,'4',8). What happens if we run x[1]=10?
The tuple becomes (2,10,7,'4',8)
A ValueError is raised
A TypeError stating tuple does not support item assignment is raised
Nothing happens; Python ignores it
Consider x=(4,5,6,7,2,'aa'). What will a for i in x: print(i) loop output last?
2
'aa'
7
6
For x=(1,2,3,4,5,6,2,10,2,11,12,2), what is x.count(2)?
2
3
4
5
For x=(1,2,3,4,5,6,2,10,2,11,12,2), what does x.index(2) return?
0
1
6
8
What is the length of x=(1,2,3,4,5,6,2,10,2,11,12,2) using len(x)?
10
11
12
13
Which statement best describes tuple assignment as shown?
It allows changing elements of a tuple in place
It enables assigning more than one variable at a time using tuple structure
It converts a tuple to a list automatically
It only works for numeric tuples
Given tup1=('mrcret','eng college','2004','cse','it','csit') and tup2=(1,2,3,4,5,6,7), what is the result of print(tup1[0]) and print(tup2[1:4]) respectively?
'mrcret' and (2,3,4)
'eng college' and (1,2,3)
'mrcret' and (1,2,3)
'csit' and (3,4,5)
In Python, what is the best description of a tuple as presented?
A mutable sequence created only with parentheses
An immutable comma-separated sequence of items, created with or without parentheses
A dictionary of key-value pairs
A list that can only store numbers
Consider the function: def fun(): str = "mrce t college"; x = 20; return str, x. When called as str, x = fun(), what will print(str); print(x) output?
mrce t college then 20
20 then mrce t college
('mrce t college', 20) on one line
An error because two values cannot be returned
The function circleInfo(r) computes c = 2 * 3.14159 * r and a = 3.14159 * r * r and returns (c, a). What is the printed result of print(circleInfo(10)) as shown?
(31.4159, 314.159)
(62.8318, 314.159)
(62.8318, 628.318)
(20, 200)
What does a tuple comprehension like x = (i for i in 'abc') actually produce according to the material?
A tuple ('a','b','c')
A list ['a','b','c']
A special generator object that can be iterated once
A string 'abc'
Given x = (i for i in 'abc'), what happens when you run: for i in x: print(i)?
It raises a TypeError because x is not iterable
It prints a, b, c on separate lines by iterating once over the generator
It prints ('a', 'b', 'c')
It prints nothing because generators are empty
Which statement about generators from tuple comprehensions is correct based on the text?
They can be iterated multiple times without re-creation
They must always be converted to lists before use
They can be iterated, but only once
They store all produced values in memory at creation
What is the value of z after executing z = [(x, x**2) for x in range(6)] as shown?
[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
[(0, 0), (2, 4), (4, 16)]
Which code snippet demonstrates a set comprehension that filters characters not in 'abc' from the string 'abracadabra'?
{x for x in 'abracadabra' if x not in 'abc'}
[x for x in 'abracadabra' if x not in 'abc']
(x for x in 'abracadabra' if x not in 'abc')
set('abracadabra') - set('abc')
What is the result of the set comprehension {3*x for x in range(10) if x > 5}?
{18, 21, 24, 27, 30}
{24, 18, 27, 21}
{15, 18, 21, 24, 27}
{6, 9, 12, 15, 18}
Which statement best defines a Python dictionary as introduced here?
An ordered, immutable collection of values
An unordered, changeable, and indexed collection of key-value pairs written with curly brackets
A sequence of values accessed by integer positions only
A collection that allows duplicate keys but not duplicate values
Which of the following is a valid way to construct the dictionary with keys 1, 2, 3 mapping to 'A', 'B', 'c' respectively?
X=[1:'A',2:'B',3:'c']
X={(1,'A'),(2,'B'),(3,'c')}
X={1:'A',2:'B',3:'c'}
X=dict(['A', 'B', 'c'])
Given dict1 = {"brand":"mrcet","model":"college","year":2004}, which method would remove all items from dict1 according to the table shown?
remove()
clear()
delete()
empty()
Which dictionary method returns a shallow copy of the dictionary?
copy()
fromkeys()
items()
update()
What does fromkeys(seq, v) return?
A list of keys from seq
A new dictionary with keys from seq and values equal to v
A tuple of (key, value) pairs
A shallow copy of an existing dictionary
Which method safely retrieves the value for a key and returns a default if the key does not exist?
keys()
get(key, d)
pop(key)
values()
What does items() return according to the material?
A list of keys
A list of values
A new view of the dictionary's items (key, value)
A shallow copy of the dictionary
If you want a new view of the dictionary's keys, which method should you use?
values()
items()
keys()
setdefault()
Which statement best describes values()?
Returns a new view of the dictionary's values
Returns the value for a key or inserts it
Removes and returns an arbitrary (key, value)
Updates the dictionary with pairs from another
Given dict1 = {"brand":"mrcet","model":"college","year":2004}, what does dict1["brand"] return?
"brand"
"mrcet"
("brand","mrcet")
None
Using the same dict1, which expression returns dict_keys(['brand','model','year'])?
dict1.items()
dict1.keys()
dict1.values()
dict1.copy()
For dict1 as defined, which output matches dict1.items()?
dict_items([('brand','mrcet'),('model','college'),('year',2004)])
dict_items(['brand','model','year'])
dict_items(['mrcet','college',2004])
dict_items('brand','mrcet')
Which loop prints mrcet, college, 2004 on separate lines for dict1?
for items in dict1.keys(): print(items)
for items in dict1.values(): print(items)
for i in dict1.items(): print(i[0])
for k,v in dict1.items(): print(k)
What is the correct way to change the value of key 'year' to 2005 in the dictionary dict1 = {'brand':'mrcet','model':'college','year':2004}?
dict1.update('year':2005)
dict1['year']=2005
dict1.change('year',2005)
update(dict1,'year',2005)
Given dict1 = {'brand':'mrcet','model':'college','year':2004}, what does dict1.pop('model') return?
'college'
'model'
{'model':'college'}
None
After executing dict1 = {'brand':'mrcet','model':'college','year':2004}; dict1.pop('model'), what is a correct resulting dictionary?
{'brand':'mrcet','year':2005}
{'brand':'mrcet','model':'college'}
{'brand':'mrcet','year':2004}
{'model':'college','year':2004}
Which statement removes the key 5 from the dictionary x = {1:1, 2:4, 3:9, 4:16, 5:25}?
x.remove(5)
del x[5]
x.popitem(5)
x.delete(5)
What is the output of y after y = len(x) where x = {1:1, 2:4, 3:9, 4:16}?
3
4
5
16
Consider x = {1:1, 2:4, 3:9, 4:16, 5:25}. What does the loop for key in x: print(key, x[key]) print?
Each key only
Each value only
Each key followed by its value
Key-value pairs as tuples only
Which loop correctly iterates over both keys and values directly as a pair from dictionary x?
for k in x: print(k)
for v in x.values(): print(v)
for k,v in x.items(): print(k,v)
for item in x.keys(): print(item, x.items())
Given customers = [{'uid':1,'name':'John'},{'uid':2,'name':'Smith'},{'uid':3,'name':'Andersson'}], what does the loop for x in customers: print(x['uid'], x['name']) output first?
John 1
1 John
uid name
{'uid':1,'name':'John'}
To change the name of the second customer from 'Smith' to 'charlie' in the list customers, which statement is used?
customers['uid'][2]['name']='charlie'
customers[2]['name']='charlie'
customers[1]['name']='charlie'
customers.pop(1)['name']='charlie'
Which statement adds a new field 'password' with value '123456' to every dictionary in customers during iteration?
for x in customers: x.add('password','123456')
for x in customers: x['password']='123456'
customers['password']='123456'
customers.append({'password':'123456'})
Given the list named customers as shown: [{'uid': 1, 'name': 'John', 'password': '123456'}, {'uid': 2, 'name': 'Smith', 'password': '123456'}, {'uid': 3, 'name': 'charlie', 'password': '123456'}], which Python statement removes the second dictionary (index 1) from the list?
del customers['uid']
del customers[1]
del customers(1)
customers.remove(1)
After executing del customers[1] twice on the original customers list of three dictionaries, what will print(customers) output?
[{'uid': 1, 'name': 'John', 'password': '123456'}]
[{'uid': 1, 'name': 'John', 'password': '123456'}, {'uid': 3, 'name': 'charlie', 'password': '123456'}]
[]
An IndexError is printed instead of a list
To delete only the 'uid' field from every dictionary in the customers list, which loop matches the example shown?
for x in customers: del x['uid']
for x in customers: del customers['uid']
for x in range(len(customers)): del 'uid'
for x in customers: x = del['uid']
After running the loop that deletes x['uid'] for each x in customers, what does x evaluate to in the example?
{'uid': 1, 'name': 'John', 'password': '123456'}
{'name': 'John', 'password': '123456'}
{'uid': 3, 'name': 'charlie', 'password': '123456'}
'uid'
What is the resulting dictionary from the comprehension z = {x: x**2 for x in (2,4,6)}?
{2: 2, 4: 4, 6: 6}
{2: 4, 4: 16, 6: 36}
{4: 2, 16: 4, 36: 6}
{2: 8, 4: 64, 6: 216}
Consider dict11 = {x: x*x for x in range(6)}. Which key–value pair is included?
(6, 36)
(5, 5)
(4, 16)
(1, 0)
