0% found this document useful (0 votes)
122 views17 pages

Computer Half Yearly Question Bank

This is computer science with python question bank for half yearly examination, this gives you a complete overview of what might come in the paper along with complete learning of the chapters

Uploaded by

jyeshthsharma25
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)
122 views17 pages

Computer Half Yearly Question Bank

This is computer science with python question bank for half yearly examination, this gives you a complete overview of what might come in the paper along with complete learning of the chapters

Uploaded by

jyeshthsharma25
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
You are on page 1/ 17

Half Yearly Question Bank –

Chapters –
String Manipulation, List Manipulation, Tuples

1. Which of the following functions is used to display the length of a string?


a) length() b) len() c) size() d) count()

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()

8. The statement 'World' * 'Tour' will result in ________________.


a) WorldTour b) World * Tour c) 'World' * 'Tour' d) Error

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]

14. Which built-in function can create an empty list?


a) index() b) createlist() c) list() d) make()

15. A _____________ uses square brackets for comma-separated values in Python.


a) List b) String c) Tuple d) Dictionary

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

19. What is the correct way to get the length of a list?


a) a.count() b) count(a) c) len(a) d) length(a)
20. Which of the following is incorrect for the list data type?
a) Lists are ordered. b) List data item can be modified after it is declared.
c) List can contain duplicate values. d) None of these

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()

28. Choose the correct statement for tuple:


a) Tuple is ordered and mutable b) Tuple is unordered and immutable
c) Tuple is ordered and immutable d) Tuple is unordered and mutable

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)

36. Which of the following operations is incorrect in a tuple?


a) print(myTup[2]) b) myTup[2]= "abc"
c) print(len(myTup)) d) print(min(myTup))

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

1. Indexing of strings can be either positive or negative.


True

2. Negative indexing starts with -1.


True

3. The capitalize() function converts a whole string into uppercase.


False
(Explanation: capitalize() only converts the first character to uppercase and the rest to lowercase.)

4. The isalnum() function returns true if a digit is present in the string.


False
(Explanation: isalnum() returns True if all characters in the string are alphanumeric, not just digits.)

5. The isdigit() function and isalnum() are the same functions.


False
(Explanation: isdigit() checks if all characters in the string are digits, whereas isalnum() checks for both
alphabetic and numeric characters.)

6. If you remove both the indices during slicing then it will return the whole string from 0 to the last index.
True

7. String is an immutable data type.


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.)

11. A list can contain elements of different data types.


True

12. A list cannot contain duplicate elements.


False
(Explanation: Lists can contain duplicate elements.)

13. The list items are enclosed within square brackets [ ].


True

14. Two lists can be compared using the '==' operator.


True

15. The sort() function sorts the elements and returns another list.
False
(Explanation: The sort() function sorts the list in place and returns None.)

16. A list is an immutable data type.


False
(Explanation: Lists are mutable; you can change their elements.)

17. The pop() and remove() functions are the same.


False
(Explanation: pop() removes an element by index and returns it, while remove() removes the first
occurrence of a value.)

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.)

21. Tuples are an immutable data type.


True

22. A tuple can store values of different data types.


True

23. Tuple and list are similar.


False
(Explanation: Although both can store multiple elements, tuples are immutable, while lists are mutable.)

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.)

26. A tuple can contain duplicate values.


True

27. A tuple stores and displays elements in an order.


True

28. The function sum() cannot work with nested tuples.


True
29. You cannot perform a slicing operation on tuples.
False
(Explanation: Slicing operations can be performed on tuples, similar to lists and strings.)

30. The sorted() function does not change the original tuple.
True

Questions and Answers.

1. What are the features of the list data type?


The main features of the lists are given as follows:

1. The lists are ordered.


2. The lists are mutable.
3. Lists can be nested.
4. Lists are dynamic.
5. Lists can contain different types of data.
6. List elements can be accessed through their index values.

2. What do you understand by the feature "Lists are ordered"?


A list is an ordered collection of objects which means that the order in which you specify the elements
when you define a list is maintained and stored as it is.

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

4. How can you create an empty list?


The syntax to create an empty list is:

empty_list = []
Here, empty_list is a list data type identifier.

5. What is the difference between the pop() and remove() functions?


The pop() function takes an index as its argument and will remove the element at that index. If no
argument is specified, the pop() function will remove the last element. The pop() function also returns
the element it has removed. The remove() function takes an element as its argument and removes it if it
is in the list. If it is not in the list, then the remove() function will give a value error. Also, the remove()
function cannot remove multiple items from the list at a time.

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:

 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 the list itemlist. When the list is
printed, the list [1, 2, 3, 1, 2, 3, 1, 2, 3] will be displayed.

7. What is the list type in Python?


A Python list data type is a collection of data elements of the same or different data types.

8. Explain any three features of list data type.

1. Lists are ordered: A list is an ordered collection of objects.


2. Lists are mutable: It means that the elements of the list can be changed even after the list is defined.
3. Lists are dynamic: Lists are easy to store and handle different operations with ease.

9. What is indexing in list data type?


The elements of a list are indexed in a specific order, with 0 as the first index.

10. What is the difference between append() and extend() functions?


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.

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:

num = [2, 2.5, 3]


s = sum(num)
print(s)

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.

15. What is the difference between pop() and remove() functions?


The pop() function removes the element at the given index from the list and returns the removed item.
The remove() method removes the first matching element that has been provided as the function
argument from the list.

16. What is the purpose of the insert() function in Python list()?


The built-in function insert() allows you to add an element at the specified position in the list.

17. What is the purpose of the list() function in Python?


The list() function is also called a constructor. It is used to create a list with values and also create an
empty list.

18. How can you compare two lists?


Lists can be compared using sort() and sorted() functions.

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:

sub = ['Hindi', 'Eng', 'CS', 'Maths']


for i in range(len(sub)):
print(sub[i])

20. What is the purpose of the function reverse()?


The function reverse() is used to reverse the elements of the list. This function does not return any value.
21. Write a program that reverses the list without using a built-in function.

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.

l1 = input("Enter first list: ")


l2 = input("Enter second list: ")
l3 = l1 + l2
print("Joined list:", l3)

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)

25. Is “d = (3)” and “d = (3,)” the same? Justify your answer.


d = (3) is of int data type, while d = (3,) is of tuple data type. To declare a tuple with a single element, a
comma (,) is used after the element.
26. What is the difference between the in operator and index() function?
The index() function returns the index of the first occurrence of a given element in a tuple. If the element
is not present, it raises an error. The in operator checks for membership and returns True or False.

27. Create an empty tuple.


t = ()

28. What is the purpose of the tuple() function in Python?


The tuple() function is used to create an empty tuple or a tuple with values.

29. What are the characteristics of a tuple?


The characteristics of a tuple are:

 Ordered
 Immutable
 Allows duplicate values
 Allows different data type values

30. What is the purpose of the sorted() function?


The sorted() function sorts the elements of a tuple in ascending order by default. It returns a new sorted
list and does not modify the original tuple.

31. Explain the functions max(), min(), and sum() in a tuple.

 max() returns the highest value item from a tuple.


 min() returns the minimum value item from a tuple.
 sum() returns the total of all items in a tuple.

32. What is packing and unpacking in a Tuple? Explain with an example.


Packing: Creating a tuple from a set of values.
Unpacking: Separating individual values from a tuple.
Example:

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.

34. Question: What are the characteristics of a tuple data type?


Answer:

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

Questions and Answers :

36. Question: What is indexing in a string?


Answer: Elements of the string data type can be accessed by using index values. The process of storing and
retrieving elements of an ordered data type using index or key values is called indexing. Index values are
numeric, either positive or negative.

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.

38. Question: What is string traversing?


Answer: Traversing means visiting the characters of the strings at least once. The traversal starts at the
beginning, selects each character, performs the operation, and continues doing so until the end. This pattern
of processing is called traversal.

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.

40. Question: What is string slicing? What is its purpose?


Answer: A slice means "a part of" something. Similarly, Python allows you to fetch a substring from a
string. The substring or a part of the string can be accessed by using the slicing operator colon :.
Example: S = "PROGRAM"
print(S[1:4])
Output: ROG

41. Question: What is the purpose of the len() function?


Answer: The len() function can be used to find the length of a given string. It takes the name of the string as
a parameter and returns the length of the string as a numeric value.

42. Question: What will be the output of the following code?


S = 'Best'
print(S[4])
Answer: It will give an error (IndexError: string index out of range) because the last index is 3 and there is
nothing on index 4.

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.

44. Question: What is the replace() method? Write its syntax.


Answer: This method replaces the occurrences of old characters with new character(s) in the string.
Syntax: str.replace(old, new[, max])

45. Question: What is string slicing?


Answer: Slicing in Python means fetching a substring from a string.

46. Question: What is the purpose of membership operators in Python?


Answer: Membership operators are used to test the presence of an element in a string.

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.

48. Question: Compare the split() and partition() functions.


Answer:
The split() function breaks up a given string at the specified separator and returns a list of substrings.
The partition() method breaks the string from a given parameter. It splits the string from the first
occurrence of the parameter and returns a tuple.

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.

50. Question: Explain the advantages of the index() function.


Answer: The index() method returns the index of the first character of the substring in a string if found. It
gives an error if the substring is not found.

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

53. Question: Define indexing. Explain by giving an example.


Answer: Elements of the string data type can be accessed by using index values. The process of storing and
retrieving elements of an ordered data type using index or key values is called indexing. Index values are
numeric, either positive or negative.

54. Question: What do you mean by traversing a string? Give an example.


Answer: Traversing means visiting the characters of a string at least once. The traversal starts at the
beginning and continues until the end.

Example:
S = 'TRAIN'
I=0
while I < len(S):
C = S[I]
print(C)
I=I+1

55. Question: What is the difference between a string and a list?


Answer: The list and string data types are different in the following ways:

Strings are immutable, whereas lists are mutable.


Strings can only store character data, whereas lists can store different types of data.
Strings store the characters in their consecutive locations, whereas the list stores the references to their
elements.

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

59. Question: How can you create an empty list?


Answer: The syntax to create an empty list is:
empty_list = []
Here, empty_list is a list data type identifier.

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].

62. Question: What is the list type in Python?


Answer: A Python list data type is a collection of data elements of the same or different data types.

63. Question: Explain any three features of the list data type.
Answer:

Lists are ordered: A list is an ordered collection of objects.


Lists are mutable: It means that the elements of the list can be changed even after the list is defined.
Lists are dynamic: Lists are easy to store and handle different operations with ease.

64. Question: What is indexing in the list data type?


Answer: The elements of a list are indexed in a specific order, with 0 as the first index.

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)

67. Question: How will you create an empty list?


Answer: To create an empty list, use the following syntax:
empty_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.

69. Question: What is the purpose of sorted()? Write its syntax.


Answer: The sorted() function returns a new sorted list from the elements of the list.
Syntax: sorted(list)

70. Question: How will you reverse a list?


Answer: The reverse() function is used to reverse the list.
Syntax: list.reverse()

You might also like