0% found this document useful (0 votes)
32 views15 pages

Python List Slicing Syntax Quiz

The document contains a series of multiple-choice questions related to Python lists, covering topics such as list creation, slicing, methods, and operations. Each question provides four options, with the goal of testing knowledge on list functionalities in Python. The questions are divided into sets, indicating a structured approach to assessing understanding of the subject.

Uploaded by

urxrur28
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views15 pages

Python List Slicing Syntax Quiz

The document contains a series of multiple-choice questions related to Python lists, covering topics such as list creation, slicing, methods, and operations. Each question provides four options, with the goal of testing knowledge on list functionalities in Python. The questions are divided into sets, indicating a structured approach to assessing understanding of the subject.

Uploaded by

urxrur28
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

List in Python

Set - 1
1)​ 1. Which of the following commands will create a list?
a)​ a) list1 = list()
b)​ b) list1 = []
c)​ c) list1 = list([1, 2, 3])
d)​ d) all of the mentioned

2)​ 2. What is the output when we execute list(“hello”)?


a)​ a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b)​ b) [‘hello’]
c)​ c) [‘llo’]
d)​ d) [‘olleh’]

3)​ 3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?


a)​ a) 5
b)​ b) 4
c)​ c) None
d)​ d) Error

4)​ 4. Suppose list1 is [2445,133,12454,123], what is max(list1)?


a)​ a) 2445
b)​ b) 133
c)​ c) 12454
d)​ d) 123

5)​ 5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?


a)​ a) 3
b)​ b) 5
c)​ c) 25
d)​ d) 1

6)​ 6. Suppose list1 is [1, 5, 9], what is sum(list1)?


a)​ a) 1
b)​ b) 9
c)​ c) 15
d)​ d) Error

7)​ Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for
slicing operation?
a)​ a) print(list1[2:])
b)​ b) print(list1[:2])
c)​ c) print(list1[:-2])
d)​ d) all of the mentioned

8)​ Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
a)​ a) Error
b)​ b) None
c)​ c) 25
d)​ d) 2

9)​ Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a)​ a) [2, 33, 222, 14]
b)​ b) Error
c)​ c) 25
d)​ d) [25, 14, 222, 33, 2]

Set - 2
10)​What will be the output of the following Python code?
names = ['Amir', 'Bear', 'Charlton', 'Daman']
print(names[-1][-1])
a) A
b) Daman
c) Error
d) n

11)​Suppose list1 is [1, 3, 2], What is list1 * 2?


a)​ a) [2, 6, 4]
b)​ b) [1, 3, 2, 1, 3]
c)​ c) [1, 3, 2, 1, 3, 2]
d)​ d) [1, 3, 2, 3, 2, 1]

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


a)​ a) [0, 1, 2, 3]
b)​ b) [0, 1, 2, 3, 4]
c)​ c) [0.0, 0.5, 1.0, 1.5]
d)​ d) [0.0, 0.5, 1.0, 1.5, 2.0]

13)​ What will be the output of the following Python code?


list1 = [11, 2, 23]
list2 = [11, 2, 2]
print(list1 < list2)

a)​ a) True
b)​ b) False
c)​ c) Error
d)​ d) None

14)​To add a new element to a list we use which command?


a)​ a) [Link](5)
b)​ b) [Link](5)
c)​ c) [Link](5)
d)​ d) [Link](5)

15)​To insert 5 to the third position in list1, we use which command?


a)​ a) [Link](3, 5)
b)​ b) [Link](2, 5)
c)​ c) [Link](3, 5)
d)​ d) [Link](3, 5)

16)​To remove the string “hello” from list1, we use which command?
a)​ a) [Link](“hello”)
b)​ b) [Link](hello)
c)​ c) [Link](“hello”)
d)​ d) [Link](“hello”)

17)​Suppose list1 is [3, 4, 5, 20, 5], what is [Link](5)?


a)​ a) 0
b)​ b) 1
c)​ c) 4
d)​ d) 2

18)​Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is [Link](5)?


a)​ a) 0
b)​ b) 4
c)​ c) 1
d)​ d) 2

Set - 3
19)​Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after [Link]()?
a)​ a) [3, 4, 5, 20, 5, 25, 1, 3]
b)​ b) [1, 3, 3, 4, 5, 5, 20, 25]
c)​ c) [25, 20, 5, 5, 4, 3, 3, 1]
d)​ d) [3, 1, 25, 5, 20, 5, 4, 3]

20)​Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after


[Link]([34, 5])?
a)​ a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b)​ b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c)​ c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d)​ d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
21)​Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after
[Link](1)
a)​ a) [3, 4, 5, 20, 5, 25, 1, 3]
b)​ b) [1, 3, 3, 4, 5, 5, 20, 25]
c)​ c) [3, 5, 20, 5, 25, 1, 3]
d)​ d) [1, 3, 4, 5, 20, 5, 25]

22)​Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after


[Link]()?
a)​ a) [3, 4, 5, 20, 5, 25, 1]
b)​ b) [1, 3, 3, 4, 5, 5, 20, 25]
c)​ c) [3, 5, 20, 5, 25, 1, 3]
d)​ d) [1, 3, 4, 5, 20, 5, 25]

23)​What will be the output of the following Python code?


print("Welcome to Python".split())
a)​ a) [‘Welcome’, ‘to’, ‘Python’]
b)​ b) (“Welcome”, “to”, “Python”)
c)​ c) {“Welcome”, “to”, “Python”}
d)​ d) “Welcome”, “to”, “Python”

24)​What will be the output of the following Python code?


print(list("a#b#c#d".split('#')))
a)​ a) [‘a’, ‘b’, ‘c’, ‘d’]
b)​ b) [‘a b c d’]
c)​ c) [‘a#b#c#d’]
d)​ d) [‘abcd’]

25)​What will be the output of the following Python code?


myList = [1, 5, 5, 5, 5, 1]
max = myList[0]
indexOfMax = 0
for i in range(1, len(myList)):
if myList[i] > max:
max = myList[i]
indexOfMax = i
​ print(indexOfMax)
a)​ a) 1
b)​ b) 2
c)​ c) 3
d)​ d) 4

26)​What will be the output of the following Python code?


myList = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
myList[i - 1] = myList[i]

for i in range(0, 6):


print(myList[i], end = " ")
a)​ a) 2 3 4 5 6 1
b)​ b) 6 1 2 3 4 5
c)​ c) 2 3 4 5 6 6
d)​ d) 1 1 2 3 4 5

27)​What will be the output of the following Python code?


list1 = [1, 3]
list2 = list1
list1[0] = 4
print(list2)
a)​ a) [1, 3]
b)​ b) [4, 3]
c)​ c) [1, 4]
d)​ d) [1, 3, 4]

28)​What will be the output of the following Python code?


def f(values):
values[0] = 44
v = [1, 2, 3]
f(v)
print(v)
a)​ a) [1, 44]
b)​ b) [1, 2, 3, 44]
c)​ c) [44, 2, 3]
d)​ d) [1, 2, 3]

Set - 4
29)​What will be the output of the following Python code?
def f(i, values = []):
[Link](i)
return values
f(1)
f(2)
v = f(3)
print(v)
a)​ a) [1] [2] [3]
b)​ b) [1] [1, 2] [1, 2, 3]
c)​ c) [1, 2, 3]
d)​ d) 1 2 3

30)​What will be the output of the following Python code?


names1 = ['Amir', 'Bala', 'Chales']
if 'amir' in names1:
print(1)
else:
print(2)
a)​ a) None
b)​ b) 1
c)​ c) 2
d)​ d) Error

31)​What will be the output of the following Python code?


names1 = ['Amir', 'Bala', 'Charlie']
names2 = [[Link]() for name in names1]
print(names2[2][0])
a)​ a) None
b)​ b) a
c)​ c) b
d)​ d) c

32)​What will be the output of the following Python code?


numbers = [1, 2, 3, 4]
[Link]([5,6,7,8])
print(len(numbers))
a)​ a) 4
b)​ b) 5
c)​ c) 8
d)​ d) 12

33)​To which of the following the “in” operator can be used to check if an item is in it?
a)​ a) Lists
b)​ b) Dictionary
c)​ c) Set
d)​ d) All of the mentioned

34)​What will be the output of the following Python code?


list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print(len(list1 + list2))
a)​ a) 2
b)​ b) 4
c)​ c) 5
d)​ d) 8

35)​What will be the output of the following Python code?


def addItem(listParam):
listParam += [1]
mylist = [1, 2, 3, 4]
addItem(mylist)
print(len(mylist))
a)​ a) 1
b)​ b) 4
c)​ c) 5
d)​ d) 8

36)​What will be the output of the following Python code?


def increment_items(L, increment):
i=0
while i < len(L):
L[i] = L[i] + increment
i=i+1
values = [1, 2, 3]
print(increment_items(values, 2))
print(values)

a)
None
[3, 4, 5]
b)
None
[1, 2, 3]
c)
[3, 4, 5]
[1, 2, 3]
d)
[3, 4, 5]
None

37)​What will be the output of the following Python code?


veggies = ['carrot', 'broccoli', 'potato', 'asparagus']
[Link]([Link]('broccoli'), 'celery')
print(veggies)
a)​ a) [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’]
b)​ b) [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]
c)​ c) [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]
d)​ d) [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]

Set - 5
38)​What will be the output of the following Python code?
m = [[x, x + 1, x + 2] for x in range(0, 3)]
print(m)
a)​ a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b)​ b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
c)​ c) [1, 2, 3, 4, 5, 6, 7, 8, 9]
d)​ d) [0, 1, 2, 1, 2, 3, 2, 3, 4]

39)​How many elements are in m?


m = [[x, y] for x in range(0, 4) for y in range(0, 4)]
print(m)
a)​ a) 8
b)​ b) 12
c)​ c) 16
d)​ d) 32

40)​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)​ a) 3
b)​ b) 5
c)​ c) 6
d)​ d) 33

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 lst in values:
for element in lst:
if v > element:
v = element
print(v)

a)​ a) 1
b)​ b) 3
c)​ c) 5
d)​ d) 6

42)​What will be the output of the following Python code?


values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]
for row in values:
[Link]()
for element in row:
print(element, end = " ")
print()
a)​ a)
3451
1 2 6 33
b)​ b)
3451
33 6 1 2
c)​ c)
1345
1 2 6 33
d)​ d) 1 3 4 5

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)​ a) 1 2 3 4
b)​ b) 4 5 6 7
c)​ c) 1 3 8 12
d)​ d) 2 5 9 13

44)​What will be the output of the following Python code?


def m(list):
v = list[0]
for e in list:
if v < e: v = e
return v
values = [[3, 4, 5, 1], [33, 6, 1, 2]]
for row in values:
print(m(row), end = " ")
a)​ a) 3 33
b)​ b) 1 1
c)​ c) 5 6
d)​ d) 5 33

45)​What will be the output of the following Python code?


data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(data[1][0][0])
a)​ a) 1
b)​ b) 2
c)​ c) 4
d)​ d) 5
46)​What will be the output of the following Python code?
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def ttt(m):
v = m[0][0]
for row in m:
for element in row:
if v < element: v = element
return v
print(ttt(data[0]))
a)​ a) 1
b)​ b) 2
c)​ c) 4
d)​ d) 5

47)​What will be the output of the following Python code?


points = [[1, 2], [3, 1.5], [0.5, 0.5]]
[Link]()
print(points)
a)​ a) [[1, 2], [3, 1.5], [0.5, 0.5]]
b)​ b) [[3, 1.5], [1, 2], [0.5, 0.5]]
c)​ c) [[0.5, 0.5], [1, 2], [3, 1.5]]
d)​ d) [[0.5, 0.5], [3, 1.5], [1, 2]]

Set - 6
48)​What will be the output of the following Python code?
s="a@b@c@d"
a=list([Link]("@"))
print(a)
b=list([Link]("@",3))
print(b)
a)​ a)
[‘a’, ’b’, ’c’, ’d’]
[‘a’, ’b’, ’c’, ’d’]
b)​ b)
[‘a’,’@’,’b’,’@’,’c’,’@’,’d’]
[‘a’,’b’,’c’,’d’]
c)​ c)
['a', '@', 'b@c@d']
['a', 'b', 'c', 'd']
d)​ d)
[‘a’,’@’,’b@c@d’]
[‘a’,’@’,’b’,’@’,’c’,’@’,’d’]

49)​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)
a)​ a) 10
b)​ b) [1, 3, 5, 7]
c)​ c) 4
d)​ d) [1, 3, 6, 10]

50)​What will be the output of the following Python code?


a="hello"
b=list(([Link](),len(x)) for x in a)
print(b)
a)​ a) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1)]
b)​ b) [(‘HELLO’, 5)]
c)​ c) [(‘H’, 5), (‘E’, 5), (‘L’, 5), (‘L’, 5), (‘O’, 5)]
d)​ d) Syntax error

51)​What will be the output of the following Python code?


a = [2, 4, 6, 8]
b = [sum(a[0:x+1]) for x in range(0, len(a))]
print(b)
a)​ a) 10
b)​ b) [2, 3, 6, 7]
c)​ c) 4
d)​ d) [2, 6, 12, 20]

52)​What will be the output of the following Python code?


a=[[]]*3
a[1].append(7)
print(a)
a)​ a) Syntax error
b)​ b) [[7], [7], [7]]
c)​ c) [[7], [], []]
d)​ d) [[],7, [], []]

53)​What will be the output of the following Python code?


lst=[3,4,6,1,2]
lst[1:2]=[7,8]
print(lst)
a)​ a) [3, 7, 8, 6, 1, 2]
b)​ b) Syntax error
c)​ c) [3,[7,8],6,1,2]
d)​ d) [3, 4, 6, 7, 8]
Set - 7
54)​What will be the output of the following Python code?
a=[1,2,3]
b=[Link](4)
print(a)
print(b)
a)​ a)
[1, 2, 3, 4]
[1, 2, 3, 4]
b)​ b)
[1, 2, 3, 4]
None
c)​ c) Syntax error
d)​ d)
[1, 2, 3]
[1, 2, 3, 4]

55)​What will be the output of the following Python code?


a=[13,56,17]
[Link]([87])
[Link]([45,67])
print(a)
a)​ a) [13, 56, 17, [87], 45, 67]
b)​ b) [13, 56, 17, 87, 45, 67]
c)​ c) [13, 56, 17, 87,[ 45, 67]]
d)​ d) [13, 56, 17, [87], [45, 67]]

56)​What is the output of the following piece of code?


a=list((45,)*4)
print((45)*4)
print(a)
a)​ a)

180
[(45), (45), (45),(45)]
b)​ b)

(45, 45, 45, 45)


[45, 45, 45, 45]
c)​ c)

180
[45, 45, 45, 45]
d)​ d) Syntax error
57)​What will be the output of the following Python code?
lst=[[1,2],[3,4]]
print(sum(lst,[]))
a)​ a) [[3], [7]]
b)​ b) [1, 2, 3, 4]
c)​ c) Error
d)​ d) [10]
58)​What will be the output of the following Python code?
word1="Apple"
word2="Apple"
list1=[1,2,3]
list2=[1,2,3]
print(word1 is word2)
print(list1 is list2)
a)​ a)
True
True
b)​ b)
False
True
c)​ c)
False
False
d)​ d)
True
False
59)​What will be the output of the following Python code?
def unpack(a,b,c,d):
print(a+d)
x = [1,2,3,4]
unpack(*x)
a)​ a) Error
b)​ b) [1, 4]
c)​ c) [5]
d)​ d) 5
60)​What will be the output of the following Python code?
places = ['Bangalore', 'Mumbai', 'Delhi']
places1 = places
places2 = places[:]
places1[1]="Pune"
places2[2]="Hyderabad"
print(places)
a)​ a) [‘Bangalore’, ‘Pune’, ‘Hyderabad’]
b)​ b) [‘Bangalore’, ‘Pune’, ‘Delhi’]
c)​ c) [‘Bangalore’, ‘Mumbai’, ‘Delhi’]
d)​ d) [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’]
61)​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)​ a) 5 5 1 2 3
b)​ b) 5 1 2 3 4
c)​ c) 2 3 4 5 1
d)​ d) 2 3 4 5 5
62)​What will be the output of the following Python code?
def change(var, lst):
var = 1
lst[0] = 44
k=3
a = [1, 2, 3]
change(k, a)
print(k)
print(a)
a)​ a)
3
[44, 2, 3]
b)​ b)
1
[1, 2, 3]
c)​ c)
3
[1, 2, 3]
d)​ d)
1
[44, 2, 3]
63)​What will be the output of the following Python code?
a = [1, 5, 7, 9, 9, 1]
b=a[0]
x= 0
for x in range(1, len(a)):
if a[x] > b:
b = a[x]
b= x
print(b)
a)​ a) 5
b)​ b) 3
c)​ c) 4
d)​ d) 0
64)​What will be the output of the following Python code?
a=["Apple","Ball","Cobra"]
[Link](key=len)
print(a)
a)​ a) [‘Apple’, ‘Ball’, ‘Cobra’]
b)​ b) [‘Ball’, ‘Apple’, ‘Cobra’]
c)​ c) [‘Cobra’, ‘Apple’, ‘Ball’]
d)​ d) Invalid syntax for sort()

Common questions

Powered by AI

List mutability implies that changes made to a list object through one reference are visible to other references pointing to the same object. For example, if `list1 = [1, 3]` and `list2 = list1`, changing `list1[0] = 4` results in both `list1` and `list2` reflecting the change, outputting `[4, 3]` .

Using a mutable object like a list as a default argument can lead to unexpected behaviors because the same object is used across function calls. For example, the function `f(3)` appends `3` to the list `[1, 2, 3]`, leading to `[1, 2, 3, 1, 2, 3]` due to the persistent default list .

Slicing with negative indices allows for accessing elements relative to the list's end. For a list `list1` like `[2, 33, 222, 14, 25]`, `list1[-1]` gives `25`, and `list1[:-1]` retrieves every element except the last, resulting in `[2, 33, 222, 14]` .

Python's `sort()` method can be customized using the `key` parameter, which accepts a function to determine the sort order. When sorting the list `['Apple', 'Ball', 'Cobra']` by the length of its elements, it results in the order `['Ball', 'Apple', 'Cobra']` .

The correct methods for creating a list in Python from the given options are: `list1 = list()`, `list1 = []`, and `list1 = list([1, 2, 3])`. Therefore, the answer is all of the mentioned .

Python treats slicing of lists by specifying a range of indices and returns a new list containing the elements within that range. For the list `[4, 2, 2, 4, 5, 2, 1, 0]`, the slicing operations `list1[2:]`, `list1[:2]`, and `list1[:-2]` are all valid and hence the correct syntax for slicing operations are all of the mentioned .

The `append()` method adds its argument as a single element to the end of a list, potentially increasing its length by one, whereas `extend()` adds each element of its iterable argument to the list, potentially increasing its length by the number of elements in the iterable. For instance, appending `[87]` to `[13, 56, 17]` results in `[13, 56, 17, [87]]`, while extending with `[45, 67]` results in `[13, 56, 17, [87], 45, 67]` .

Modifying a list inside a nested loop via element-wise assignment will reflect changes accordingly. In the nested loop iterating over `values = [[3, 4, 5, 1], [33, 6, 1, 2]]`, sorting each row results in `[1, 3, 4, 5]` and `[1, 2, 6, 33]`. Therefore, printing each element in order outputs `1 3 4 5 1 2 6 33` .

The `is` operator checks for object identity. For strings, such as `word1 = "Apple"` and `word2 = "Apple"`, `word1 is word2` is True due to string interning, which optimizes memory usage by storing only one instance of identical strings. In contrast, `list1 = [1, 2, 3]` and `list2 = [1, 2, 3]` results in `list1 is list2` being False since they are separate object instances in memory .

The output of the expression `list1 * 2` when performed on the list `[1, 3, 2]` is a new list `[1, 3, 2, 1, 3, 2]`, which effectively repeats the original list twice .

You might also like