GE8151-PROBLEM SOLVING IN PYTHON PROGRAMMING
UNIT-4 (Objective Questions)
COMPOUND DATA: LISTS, TUPLES, DICTIONARIES
List Methods
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Basic Tuples Operations
Operator Description Example
Repetition The repetition operator T1*2 = (1, 2,
enables the tuple elements to 3, 4, 5, 1, 2,
be repeated multiple times. 3, 4, 5)
Concatenatio It concatenates the tuple T1+T2 = (1, 2,
n mentioned on either side of 3, 4, 5, 6, 7,
the operator. 8, 9)
Membership It returns true if a particular print (2 in T1)
item exists in the tuple prints True.
otherwise false
Iteration The for loop is used to iterate for i in T1:
over the tuple elements. print(i)
Output
1
2
3
4
5
Length It is used to get the length of len(T1) = 5
the tuple.
Python Tuple inbuilt functions
S Function Description
N
1 cmp(tuple1, It compares two tuples and returns true if tuple1 is greater than
tuple2) tuple2 otherwise false.
2 len(tuple) It calculates the length of the tuple.
3 max(tuple) It returns the maximum element of the tuple
4 min(tuple) It returns the minimum element of the tuple.
5 tuple(seq) It converts the specified sequence to the tuple.
1. Which of the following commands will create a list?
a) list1 = list()
b) list1 = []
c) list1 = list([1, 2, 3])
d) all of the mentioned
Answer: d
Explanation: Execute in the shell to verify
2. What is the output when we execute list(“hello”)?
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) [‘hello’]
c) [‘llo’]
d) [‘olleh’]
Answer: a
Explanation: Execute in the shell to verify.
3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
a) 5
b) 4
c) None
d) Error
Answer: a
Explanation: Execute in the shell and verify.
4. Suppose list1 is [2445,133,12454,123], what is max(list1)?
a) 2445
b) 133
c) 12454
d) 123
Answer: c
Explanation: Max returns the maximum element in the list.
5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?
a) 3
b) 5
c) 25
d) 1
Answer: d
Explanation: Min returns the minimum element in the list.
6. Suppose list1 is [1, 5, 9], what is sum(list1)?
a) 1
b) 9
c) 15
d) Error
Answer: c
Explanation: Sum returns the sum of all elements in the list.
7. To shuffle the list(say list1) what function do we use?
a) list1.shuffle()
b) shuffle(list1)
c) random.shuffle(list1)
d) random.shuffleList(list1)
Answer: c
Explanation: Execute in the shell to verify.
8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing
operation?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) all of the mentioned
Answer: d
Explanation: Slicing is allowed in lists just as in the case of strings.
9. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
a) Error
b) None
c) 25
d) 2
Answer: c
Explanation: -1 corresponds to the last index in the list.
10. Which of the following would give an error?
a) list1=[]
b) list1=[]*3
c) list1=[2,8,7]
d) None of the above
Ans : d
Explanation: None of the above will result in error.
11. What will be the output of the following Python code?
1. >>>names = [‘Amir’, ‘Bear’, ‘Charlton’, ‘Daman’]
2. >>>print(names[-1][-1])
a) A
b) Daman
c) Error
d) n
Answer: d
Explanation: Execute in the shell to verify.
12. Suppose list1 is [1, 3, 2], What is list1 * 2?
a) [2, 6, 4]
b) [1, 3, 2, 1, 3]
c) [1, 3, 2, 1, 3, 2]
d) [1, 3, 2, 3, 2, 1]
Answer: c
Explanation: Execute in the shell and verify.
13. What will be the output of the following Python code?
1. >>>list1 = [11, 2, 23]
2. >>>list2 = [11, 2, 2]
3. >>>list1 < list2 is
a) True
b) False
c) Error
d) None
Answer: b
Explanation: Elements are compared one by one.
14. To add a new element to a list we use which command?
a) list1.add(5)
b) list1.append(5)
c) list1.addLast(5)
d) list1.addEnd(5)
Answer: b
Explanation: We use the function append to add an element to the list.
15. To insert 5 to the third position in list1, we use which command?
a) list1.insert(3, 5)
b) list1.insert(2, 5)
c) list1.add(3, 5)
d) list1.append(3, 5)
Answer: b
Explanation: Execute in the shell to verify.
16. To remove string “hello” from list1, we use which command?
a) list1.remove(“hello”)
b) list1.remove(hello)
c) list1.removeAll(“hello”)
d) list1.removeOne(“hello”)
Answer: a
Explanation: Execute in the shell to verify.
17. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?
a) 0
b) 1
c) 4
d) 2
Answer: d
Explanation: Execute help(list.index) to get details.
18. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?
a) 0
b) 4
c) 1
d) 2
Answer: d
Explanation: Execute in the shell to verify.
19. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [25, 20, 5, 5, 4, 3, 3, 1]
d) [3, 1, 25, 5, 20, 5, 4, 3]
Answer: d
Explanation: Execute in the shell to verify.
20. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
Answer: a
Explanation: Execute in the shell to verify.
21. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
Answer: c
Explanation: pop() removes the element at the position specified in the parameter.
22. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
Answer: a
Explanation: pop() by default will remove the last element.
23. What will be the output of the following Python code?
>>>"Welcome to Python".split()
a) [“Welcome”, “to”, “Python”]
b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”
Answer: a
Explanation: split() function returns the elements in a list.
24. What will be the output of the following Python code?
>>>list("a#b#c#d".split('#'))
a) [‘a’, ‘b’, ‘c’, ‘d’]
b) [‘a b c d’]
c) [‘a#b#c#d’]
d) [‘abcd’]
Answer: a
Explanation: Execute in the shell to verify.
25. What will be the output of the following Python code?
1. >>>list1 = [1, 3]
2. >>>list2 = list1
3. >>>list1[0] = 4
4. >>>print(list2)
a) [1, 3]
b) [4, 3]
c) [1, 4]
d) [1, 3, 4]
Answer: b
Explanation: Lists should be copied by executing [:] operation.
26. To which of the following the “in” operator can be used to check if an item is in it?
a) Lists
b) Dictionary
c) Set
d) All of the mentioned
Answer: d
Explanation: In can be used in all data structures.
27. What will be the output of the following Python code?
1. list1 = [1, 2, 3, 4]
2. list2 = [5, 6, 7, 8]
3.
4. print(len(list1 + list2))
a) 2
b) 4
c) 5
d) 8
Answer: d
Explanation: + appends all the elements individually into a new list.
28. What will be the output of the following Python code?
1. veggies = ['carrot', 'broccoli', 'potato', 'asparagus']
2. veggies.insert(veggies.index('broccoli'), 'celery')
3. print(veggies)
a) [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’] Correct 1.00
b) [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]
c) [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]
d) [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]
Answer: a
Explanation: Execute in the shell to verify.
29. Which of the following is True regarding lists in Python?
a) Lists are immutable.
b)Size of the lists must be specified before its initialization
c) Elements of lists are stored in contagious memory location.
d)size(list1) command is used to find the size of lists.
Ans : c
Explanation: Elements of lists are stored in contagious memory location is True regarding lists in Python.
30. What will be the output of below Python code?
list1=[8,0,9,5]
print(list1[::-1])
a)[5,9,0,8]
b) [8,0,9]
c) [8,0,9,5]
d) [0,9,5]
Ans : a
Explanation: [5,9,0,8] will be the output of below Python code.
31. Which of the following will give output as [23,2,9,75] ?
If list1=[6,23,3,2,0,9,8,75]
a) print(list1[1:7:2])
b) print(list1[0:7:2])
c)print(list1[1:8:2])
d) print(list1[0:8:2])
Ans : c
Explanation: print(list1[1:8:2]) of the following will give output as [23,2,9,75].
32. The marks of a student on 6 subjects are stored in a list, list1=[80,66,94,87,99,95]. How can the student’s
average mark be calculated?
a)print(avg(list1))
b)print(sum(list1)/len(list1))
c) print(sum(list1)/sizeof(list1))
d) print(total(list1)/len(list1))
Ans : b
Explanation: the student’s average mark be calculated through print(sum(list1)/len(list1)).
33. What will be the output of following Python code?
list1=["Python","Java","c","C","C++"]
print(min(list1))
a)c
b)C++
c) C
d)min function cannot be used on string elements
Ans : c
Explanation: C will be the output of following Python code.
34. The elements of a list are arranged in descending order. Which of the following two will give same outputs?
i. print(list_name.sort())
ii. print(max(list_name))
iii. print(list_name.reverse())
iv. print(list_name[-1])
a) i, ii
b) i, iii
c) ii, iii
d) iii, iv
Ans : b
Explanation: print(list_name.sort()) and print(list_name.reverse()) will give same outputs.
35. What will be the result after the execution of above Python code?
list1=[3,2,5,7,3,6]
list1.pop(3)
print(list1)
a)[3,2,5,3,6]
b) [2,5,7,3,6]
c) [2,5,7,6]
d) [3,2,5,7,3,6]
Ans : a
Explanation: [3,2,5,3,6] will be the result after the execution of above Python code.
36.What will be the output of below Python code?
list1=[1,3,5,2,4,6,2]
list1.remove(2)
print(sum(list1))
a) 18
b) 19
c) 21
d) 22
Ans : c
Explanation: 21 will be the result after the execution of above Python code.
37. What will be the output of below Python code?
list1=["tom","mary","simon"]
list1.insert(5,8)
print(list1)
a) ["tom", "mary", "simon", 5]
b) ["tom", "mary", "simon", 8]
c) [8, "tom", "mary", "simon"]
d) Error
Ans : b
Explanation: ["tom", "mary", "simon", 8] will be the result after the execution of above Python code.
38.Which of the following statements create a dictionary?
a) d = {}
b) d = {“john”:40, “peter”:45}
c) d = {40:”john”, 45:”peter”}
d) All of the mentioned
Answer: d
Explanation: Dictionaries are created by specifying keys and values.
39.What will be the output of the following Python code snippet?
D = {“john”:40, “peter”:45}
a) “john”, 40, 45, and “peter”
b) “john” and “peter”
c) 40 and 45
d) d = (40:”john”, 45:”peter”)
Answer: b
Explanation: Dictionaries appear in the form of keys and values.
40.What will be the output of the following Python code snippet?
D = {“john”:40, “peter”:45}
“john” in d
a) True
b) False
c) None
d) Error
Answer: a
Explanation: In can be used to check if the key is int dictionary.
41. What will be the output of the following Python code snippet?
D1 = {“john”:40, “peter”:45}
D2 = {“john”:466, “peter”:45}
D1 == d2
a) True
b) False
c) None
d) Error
Answer: b
Explanation: If d2 was initialized as d2 = d1 the answer would be true.
42.What will be the output of the following Python code snippet?
D1 = {“john”:40, “peter”:45}
D2 = {“john”:466, “peter”:45}
D1 > d2
a) True
b) False
c) Error
d) None
Answer: c
Explanation: Arithmetic > operator cannot be used with dictionaries.
43.What will be the output of the following Python code snippet?
D = {“john”:40, “peter”:45}
D[“john”]
a) 40
b) 45
c) “john”
d) “peter”
Answer: a
Explanation: Execute in the shell to verify.
44. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we
use?
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”]
d) del d(“john”:40)
Answer: c
Explanation: Execute in the shell to verify.
45. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which
command do we use?
a) d.size()
b) len(d)
c) size(d)
d) d.len()
Answer: b
Explanation: Execute in the shell to verify.
46.What will be the output of the following Python code snippet?
D = {“john”:40, “peter”:45}
Print(list(d.keys()))
a) [“john”, “peter”]
b) [“john”:40, “peter”:45]
c) (“john”, “peter”)
d) (“john”:40, “peter”:45)
Answer: a
Explanation: The output of the code shown above is a list containing only
keys of the dictionary d, in the form of a list.
47. Suppose d = {“john”:40, “peter”:45}, what happens when we try to
retrieve a value using the expression d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception
b) It is executed fine and no exception is raised, and it returns None
c) Since “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error
Answer: c
Explanation: Execute in the shell to verify.
48. Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a) [2, 33, 222, 14]
b) Error
c) 25
d) [25, 14, 222, 33, 2]
Answer: a
Explanation: Execute in the shell to verify.