Computer Half Yearly Question Bank
Computer Half Yearly Question Bank
Chapters –
String Manipulation, List Manipulation, Tuples
2. The ________________ operator is used to repeat a string for a specified number of times.
a) + b) * c) / d) #
3. The ________________ method replaces the occurrences of old characters with the new character(s) of
the string.
a) change() b) modify() c) replace() d) changeall()
4. What will be the output of the following code? S="Old is Gold" print(S[1:8])
a) Old is b) ld is G c) Old is G d) ld is Go
5. The slicing statement using print(S[-4:]) will give ________________ if S = "Good Morning".
a) Good b) Good M c) gnin d) ning
6. Which of the following function removes the specified characters from the beginning and end of the
string?
a) lstrip() b) rstrip() c) strip() d) cut()
7. The ________________ function checks whether the string consists of whitespaces only.
a) space() b) allspace() c) isspace() d) findspace()
9. Which of the following statement will be used to convert the string elements into lowercase?
a) print(S.lower()) b) print(S.lowercase()) c) print(S.changelower()) d) print(lower())
10. Which of the following is not a valid string operation in Python?
a) Wel' + 'come b) 'Welcome' * 2 c) 'Welcome' + 2 d) 2 * 'Welcome'
11. The ________________ function converts the first character of every word in the sentence to uppercase.
a) istitle() b) upper() c) lower() d) title()
12. What will be the output of the following statement? "PYTHON IS A HIGH LEVEL
LANGUAGE".title()
a) 'Python Is A High Level Language' b) 'Python is a high level language' c) True
13. How will you access the substring "Karan" from the following string? str = "My name is Karan"
a) str[11:16] b) str(11:16) c) str[11][16] d) str[11-16]
16. Which of the following is the correct way to access a specific element from a nested list?
a) list[row_size ] b) list[row_size][column_size]
c) list[(row_size)(column_size)] d) None of the above
17. Which of the following is the correct way to find the biggest element from a Python list?
a) print(maximum(mylist)) b) print(mylist.max())
c) print(max(mylist)) d) print(mylist.maximum())
18. How can you fetch the last element from a list with negative indexing?
a) print("Last element = ", mylist[0]) b) print("Last element = ", mylist[])
c) print("Last element = ", mylist[-1]) d) None of the above
21. Choose the correct output for the following code: l1=[1, 2, 3, 4] l2=['a', 'b', 'c'] l1==l2
a) True b) False c) Equal d) Not Equal
22. What will be the print statement to display 'pencil' from the given list? f = ['pen', 'pencil', 'eraser']
a) print(f[3:8]) b) print(f) c) print(f['pencil']) d) print(f[1])
23. Choose the correct output generated by the following code: x=['tree', 'plant', 'soil'] x.append('air') print(x)
a) ['tree', 'plant', 'soil', 'air'] b) ['air', 'tree', 'plant', 'soil']
c) ['tree', 'plant', 'air','soil'] d) Any of these
24. If you want to add an element 'apple' at position 3 in the following list called things which function will
you use? things = ['orange', 'banana', 'cherry', 'papaya', 'pineapple']
a) insert() b) extend() c) append() d) index()
25. Which of the following functions will add only one value to the list at the end?
a) append() b) add() c) extend() d) insert()
26. Predict the output of the following code: seq= [2, 4, 5, 7] num = seq[:] print(num)
a) [2, 4, 5, 7] b) No output c) An error d) [7, 5, 4, 2]
27. Which of the following functions will add the list of elements at the end of another list?
a) list() b) add() c) append() d) extend()
29. Consider the following code and find the correct output: myT = "Red", 20, "Green" a, b, c = myT
print(a)
a) ("Red", 20, "Green") b) TypeError c) Red d) No output
30. Consider the following code and predict the output: tup1 = (123, 'a') print(max(tup1))
a) 'a' b) 123 c) (123,'a') d) TypeError
31. What is the output of the following tuple operation? aTuple = (1, 2, 3, 4, 5, 6, 7, 8) print(aTuple[2:5],
aTuple[:4], aTuple[3:])
a) (3, 4, 5) (1, 2, 3, 4) (4, 5, 6, 7, 8) b) (2, 3, 4, 5) (4) (3)
c) (2, 3, 4, 5) (1, 2, 3, 4) (8, 7, 6) d) (3, 4, 5) (1, 2, 3) (4, 5, 6, 7, 8)
32. What will be the output of the following code? aTup = (100, 200, 300, 400, 500) print(aTup[-2])
print(aTup[-4:-1])
a) Index Error: tuple out of range b) 400 (200, 300, 400)
c) 200 (300, 400, 500) d) 400 (300, 400, 500)
33. Choose the correct way to access value 5 from the following tuple: a = ("Marks", [10, 5, 30], (20, 15,
25))
a) print(a[0][1]) b) print(a[1][1]) c) a[1:2][1] d) a[1:2][1]
34. *What is the output of the following tuple operation? aTuple = (1, 2, 3, 4) aTuple = 2 print(aTuple)
a) (1, 2, 3, 4) b) (1, 2, 3, 4, 1, 2, 3, 4) c) (1, 1, 2, 2, 3, 3, 4, 4) d) (1, 2, 3, 4, 2, 3, 4)
35. Which of the following functions will return the largest value from the following tuple? aTup = (40, 23,
12, 65)
a) max(aTup) b) maxim(aTup) c) high(aTup) d) highest(aTup)
37. What will be the output of the following code? str1 = 'abc' str2 = 'xyz' print(str1, str2) print(str1 + str2)
a) abc xyz abcxyz b) abc xyz abc xyz
c) abcxyz abcxyz d) abc xyz xyzabc
38. Choose the correct statement to add value 1 to the list y inside the following tuple: tup1 = ('Red', [2, 4,
5], 'Green')
a) tup1[1].append(1) b) tup1.append(1) c) tup1.add(1) d) tup1.insert(1)
39. Which of the following statements will access the value 5 from the given tuple? T = ("abc", 1, 2, 3,
"xyz", 4, 5, 6)
a) T[-1] b) T[-2] c) T[-3] d) T[-4]
TRUE / FALSE
6. If you remove both the indices during slicing then it will return the whole string from 0 to the last index.
True
8. The built-in function isspace() counts and returns the total number of whitespaces present in the string.
False
(Explanation: isspace() returns True if all characters in the string are whitespace characters.)
9. The isdigit() function returns True if all the values in the string are digits; otherwise, it returns False.
True
10. The built-in functions split() and partition() are the same and return the same output for the same input.
False
(Explanation: split() splits a string into a list based on a delimiter, while partition() splits a string into a
tuple containing the part before the separator, the separator itself, and the part after.)
15. The sort() function sorts the elements and returns another list.
False
(Explanation: The sort() function sorts the list in place and returns None.)
18. The insert() function will add an element at the specified position in the list.
True
19. The pop() function will return the updated list after popping out the element.
False
(Explanation: The pop() function returns the element that was removed, not the updated list.)
20. The append() and extend() functions perform the same type of operation.
False
(Explanation: append() adds a single element to the end of the list, while extend() adds elements from
another iterable.)
24. A tuple that stores another tuple as an element is called a nested tuple.
True
25. tup = 67, tup = (67), and tup = (67,) are the same statements.
False
(Explanation: tup = 67 is an integer, tup = (67) is also an integer due to operator precedence, and tup =
(67,) is a tuple.)
30. The sorted() function does not change the original tuple.
True
3. What are the different operations that you can perform on a list?
The different operations that can be performed on a list are:
1. Creating a list
2. Concatenation
3. Replication or repetition
4. Slicing
5. Accessing elements using index value
6. Traversing
empty_list = []
Here, empty_list is a list data type identifier.
6. What will be the output of the following operations on the given list? itemlist=[1, 2, 3]
a. itemlist * 3
b. itemlist *= 3
Answer:
11. If you want to add all the numbers present in the list, then which function will you use? Also, write
the syntax of that function.
The sum() function adds the elements of the sequence and returns the sum.
For example:
12. What is the difference between the string and list data types?
A list is an ordered sequence of object types, and a string is an ordered sequence of characters.
13. What is slicing in lists?
Slicing means dividing the list into various sub-lists. The colon (:) operator is used to access the part of
the list.
For example:
l = [1, 2, 3, 4]
print(l[0:2])
14. What is the difference between the sort() and sorted() functions?
The sort() function arranges the elements of a given list either in an ascending or descending order. It
changes the list and does not return any value. The sorted() function arranges the elements of a given list
either in an ascending or descending order. It returns the sorted list.
19. What is the meaning of traversing a list? Explain by giving a suitable example.
Traversing a list means to visit every element of the list.
Example:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l = len(numbers)
for i in range(int(l/2)):
n = numbers[i]
numbers[i] = numbers[l-i-1]
numbers[l-i-1] = n
print(numbers)
22. Write a program that takes elements from a user in two lists and creates a third list that contains all of the elements
from the first and second lists.
23. Write a program that enters a list and moves all the duplicate values at the end of the list.
a_list = [5, 2, 5, 9, 7, 8, 8, 1, 2, 7]
b_list = []
c_list = []
for i in a_list:
if i not in b_list:
b_list.append(i)
else:
c_list.append(i)
final_list = b_list + c_list
print(final_list)
24. Which built-in function is used to count the total number of elements of the tuple?
len()
Syntax: len(tuple)
Ordered
Immutable
Allows duplicate values
Allows different data type values
t1 = (1, 2, 3, 4)
a, b, c, d = t1
print(a, b, c, d)
print(t1)
33. Question: What are the similarities between list and tuple data types?
Answer:
1. Both are sequence data types that store collections of items or values.
2. Both are ordered collection data types.
3. Both can store values of any data type (e.g., string, integer, float, list, and tuple).
4. Items or values can be accessed by index value or indexing in both.
5. Both can contain duplicate values.
1. Ordered: Tuple items are stored in an order, and this order cannot be changed.
2. Immutable: Tuples are unchangeable or immutable. You cannot change, add, or remove items after
the tuple has been created.
3. Allows Duplicates and Different Types of Data: A tuple can contain duplicate items and items of
different data types.
35. Question: What is the difference between the 'in' operator and index() function as they are both used to
find an element in the tuple?
Answer:
'in' Operator: Checks whether a value exists in the tuple or not. Returns True if the value is found;
otherwise, False.
Example:
X = (20, 30, 40, 50)
20 in X returns True
100 in X returns False
index() Function: Returns the index of the first occurrence of the given element in the tuple. Raises
an error if the element is not present.
Example:
X = (20, 30, 40, 50)
print(X.index(30)) outputs 1
print(X.index(100)) raises ValueError: tuple.index(x): x not in tuple
37. Question: What is the difference between find() and index() built-in functions of Python?
Answer: The index() function and find() function both return the index of the first character of the substring.
The difference is that the find() function returns -1 if the value is not found, whereas the index() function
returns a ValueError.
39. Question: What are the different operations that can be performed on strings?
Answer: You can perform concatenation, repetition, traversing, and slicing operations on strings. You can
also use the membership operators in and not in with the strings.
43. Question: Write the use of the following functions: isspace(), istitle(), isupper()
Answer:
isspace(): It returns True if all the characters in the string are whitespaces.
istitle(): It returns True if the string follows the rules of title case.
isupper(): It returns True if all the characters in the string are uppercase.
47. Question: What is the purpose of lstrip() and rstrip() functions in Python strings?
Answer: The lstrip() function trims the specified characters from the beginning of the string. The rstrip()
function trims the specified characters from the end of the string.
49. Question: What are the different operations that can be performed on strings?
Answer: The operations like concatenation, repetition, and slicing can be performed on strings. Also, the
presence of a character in a string can be checked.
51. Question: What is the difference between len() and index() functions?
Answer: The len() function is used to find the length of a given string. The index() method returns the index
of the first character of the substring in the string if found. It gives an error if the substring is not found.
52. Question: Explain the concatenation operation with the help of an example.
Answer: String concatenation refers to joining two strings or joining the characters of both strings one after
another. The + operator is used for concatenation in Python.
Example:
X = 'DREAM'
Y = ' BIG'
print(X + Y)
Output: DREAM BIG
Example:
S = 'TRAIN'
I=0
while I < len(S):
C = S[I]
print(C)
I=I+1
56. Question: What are the features of the list data type?
Answer: The main features of lists are:
Lists are ordered.
Lists are mutable.
Lists can be nested.
Lists are dynamic.
Lists can contain different types of data.
List elements can be accessed through their index values.
57. Question: What do you understand by the feature "Lists are ordered"?
Answer: A list is an ordered collection of objects, meaning that the order in which you specify the elements
when you define a list is maintained and stored as it is.
58. Question: What are the different operations that you can perform on a list?
Answer: The different operations that can be performed on a list are:
Creating a list
Concatenation
Replication or repetition
Slicing
Accessing elements using index value
Traversing
60. Question: What is the difference between the pop() and remove() functions?
Answer:
The pop() function takes an index as its argument and removes the element at that index. If no argument is
specified, pop() removes the last element and returns it.
The remove() function takes an element as its argument and removes it if it is in the list. If the element is not
found, remove() raises a ValueError. It cannot remove multiple items at a time.
61. Question: What will be the output of the following operations on the given list?
itemlist = [1, 2, 3]
a. itemlist * 3
b. itemlist *= 3
Answer:
a. It will repeat the itemlist three times: [1, 2, 3, 1, 2, 3, 1, 2, 3]
b. It will replicate the list items three times and assign them to itemlist. The list will display [1, 2, 3, 1, 2, 3,
1, 2, 3].
63. Question: Explain any three features of the list data type.
Answer:
65. Question: What is the difference between append() and extend() functions?
Answer:
The append() function adds its argument as a single element at the end of the list.
The extend() function iterates over its arguments and adds each element to the list at the end.
66. Question: If you want to add all the numbers present in the list, which function will you use? Also, write
the syntax of that function.
Answer: The sum() function adds the elements of the sequence and returns the sum.
Syntax: sum(list)
68. Question: What is the difference between min() and max() functions?
Answer:
The min() function returns the minimum value from the list.
The max() function returns the maximum value from the list.