Strings and Lists - string slicing, immutability, string methods, list operations, mutability, cloning, list parameters - Question Bank

1. When `my_list = [1, 2, 3, 4, 5]` and `new_list = my_list`, what kind of relationship is established between `my_list` and `new_list`?
A) A deep copy
B) A shallow copy
C) An alias (both variables point to the same list object)
D) A new, independent list
2. What is the slicing syntax `[start:stop:step]` used for in Python?
A) To define a function
B) To create a new list
C) To access elements of sequences like strings and lists with an optional step
D) To modify elements in place
3. Which string method checks if a string ends with a specified suffix?
A) startswith()
B) endswith()
C) contains()
D) matches()
4. What is the consequence of modifying a list passed as an argument to a function without creating a copy?
A) The original list is unaffected.
B) The function operates on a temporary copy.
C) The original list is modified.
D) A new list is automatically created.
5. If `my_list = [1, 2, 3]` is passed to a function `def modify(lst): lst.append(4)`, what will be the value of `my_list` after calling `modify(my_list)`?
A) [1, 2, 3]
B) [4]
C) [1, 2, 3, 4]
D) Error
6. What does `my_string.partition(' ')` return for `my_string = 'hello world'`?
A) ['hello', ' ', 'world']
B) ['hello world']
C) ['hello', 'world']
D) Error
7. Which string method returns a list of all the words in a string, using whitespace as the delimiter?
A) split()
B) join()
C) partition()
D) findall()
8. What is the difference between `sorted(my_list)` and `my_list.sort()`?
A) `sorted()` modifies the original list, `sort()` returns a new sorted list
B) `sort()` modifies the original list, `sorted()` returns a new sorted list
C) They are identical in functionality
D) `sorted()` only works on strings, `sort()` only works on lists
9. Which list method sorts the list in place?
A) sorted()
B) sort()
C) order()
D) arrange()
10. If `my_list = ['a', 'b', 'c']`, what is the result of `my_list * 2`?
A) ['a', 'b', 'c', 'a', 'b', 'c']
B) ['aa', 'bb', 'cc']
C) Error
D) ['a', 'b', 'c']
11. What is the purpose of `del my_list[index]`?
A) To remove the element at `index` and return it
B) To remove the first occurrence of the value `index`
C) To remove the element at `index` without returning it
D) To clear the entire list
12. Which string method converts the first character of each word in a string to uppercase?
A) capitalize()
B) upper()
C) title()
D) casefold()
13. What does `my_string.count('a')` return for `my_string = 'banana'`?
A) 1
B) 2
C) 3
D) 0
14. When passing a list to a function, the function receives a reference to the original list object. This is often referred to as:
A) Pass by value
B) Pass by reference
C) Pass by pointer
D) Pass by copy
15. How can you create a deep copy of `my_list`?
A) Using `my_list.copy()`
B) Using `new_list = my_list[:]`
C) Using `import copy; new_list = copy.deepcopy(my_list)`
D) Using `new_list = my_list`
16. Which module in Python provides functions for creating deep copies?
A) os
B) sys
C) copy
D) math
17. What is a 'deep copy' of a list?
A) A copy where changes to nested mutable objects affect the original
B) A copy where changes to nested mutable objects do not affect the original
C) A copy of only the top-level elements
D) An alias to the original list
18. If `list1 = [1, 2]` and `list2 = [3, 4]`, what is the result of `list1.extend(list2)`?
A) [1, 2, 3, 4]
B) Error
C) Returns a new list [1, 2, 3, 4]
D) [ (1, 2), (3, 4) ]
19. What does the `extend()` list method do?
A) Adds a single element to the end of the list
B) Inserts an element at a specific index
C) Appends elements from an iterable to the end of the list
D) Removes all elements from the list
20. If `my_list = [10, 20, 30]` and you execute `my_list.append(40)`, what is the final state of `my_list`?
A) [10, 20, 30]
B) [40, 10, 20, 30]
C) [10, 20, 30, 40]
D) [10, 20, 40]
21. Which string method returns a copy of the string with its first character capitalized?
A) upper()
B) title()
C) capitalize()
D) startcase()
22. Consider `'-'.join(['a', 'b', 'c'])`. What is the result?
A) 'a-b-c'
B) 'abc'
C) ['a', '-', 'b', '-', 'c']
D) Error
23. What does the `join()` string method do?
A) Splits a string into a list of characters
B) Combines elements of an iterable (like a list) into a string, using the string as a separator
C) Removes duplicate characters from a string
D) Replaces all occurrences of a character with another
24. If `my_list = [1, 2, 3, 4, 5]` and `new_list = my_list[1:4]`, what is `new_list`?
A) [1, 2, 3, 4]
B) [2, 3, 4, 5]
C) [2, 3, 4]
D) [1, 2, 3]
25. Which list operation returns a new list containing elements from a specified range?
A) pop()
B) remove()
C) slice
D) del
26. What will `my_string.replace('l', 'X')` return if `my_string = 'hello world'`?
A) 'heXlo worXd'
B) 'hello Xorld'
C) 'hello world'
D) 'Xello Xorld'
27. Which string method checks if all characters in the string are alphanumeric?
A) isalpha()
B) isdigit()
C) isalnum()
D) islower()
28. What is the term for passing a list to a function such that the function operates on the original list object?
A) Pass by value
B) Pass by reference
C) Pass by object
D) Pass by assignment
29. When a list is passed as an argument to a function, what happens if the function modifies the list?
A) A new list is created automatically
B) The original list outside the function remains unchanged
C) The original list outside the function is modified
D) An error occurs
30. What does slicing a list, like `new_list = old_list[:]`, achieve?
A) It creates an alias
B) It performs a shallow copy of the list
C) It modifies the original list
D) It concatenates the list with itself
31. What is a potential pitfall of assigning one list variable to another using `list2 = list1`?
A) It creates a deep copy
B) It leads to data loss
C) It creates an alias, so changes to one affect the other
D) It doubles the memory usage
32. Which method is commonly used for shallow cloning of a list?
A) list.copy()
B) list.clone()
C) list.duplicate()
D) list.new()
33. What is the term for creating an independent copy of a list?
A) Aliasing
B) Assignment
C) Cloning
D) Referencing
34. If `list1 = [1, 2]` and `list2 = [3, 4]`, what is the result of `list1 + list2`?
A) [1, 2, 3, 4]
B) Error
C) [ (1, 3), (2, 4) ]
D) [1, 2][3, 4]
35. Which list operation is used to combine two lists?
A) append()
B) insert()
C) extend()
D) concatenate()
36. What does `my_list.pop(2)` do if `my_list = [10, 20, 30, 40]`?
A) Removes the element at index 2 (30) and returns it
B) Removes the element with value 2 from the list
C) Removes the last element and returns it
D) Inserts the value 2 at index 2
37. Which list method is used to remove the first occurrence of a specific value from the list?
A) pop()
B) remove()
C) del
D) clear()
38. If `my_list = [1, 2, 3]`, what will `my_list.insert(1, 10)` change the list to?
A) [1, 2, 10, 3]
B) [1, 10, 2, 3]
C) [10, 1, 2, 3]
D) [1, 2, 3, 10]
39. Which operation is used to add an element to the end of a list?
A) insert()
B) append()
C) extend()
D) add()
40. What is the primary characteristic of a list in Python that allows modification after creation?
A) Immutability
B) Dynamic typing
C) Mutability
D) Abstraction
41. Which string method removes whitespace from the beginning and end of a string?
A) strip()
B) lstrip()
C) rstrip()
D) clean()
42. What is the difference between `find()` and `index()` string methods?
A) `find()` returns -1 if not found, `index()` raises ValueError
B) `index()` returns -1 if not found, `find()` raises ValueError
C) They behave identically
D) `find()` only searches from the beginning, `index()` can search from any position
43. Which string method is used to find the first occurrence of a substring within another string?
A) find()
B) index()
C) count()
D) replace()
44. What does the `split()` method do to a string by default?
A) Joins the string elements with a hyphen
B) Replaces all occurrences of a substring
C) Splits the string into a list of words using whitespace as a delimiter
D) Removes leading and trailing whitespace
45. Which string method is used to convert all characters in a string to uppercase?
A) lower()
B) upper()
C) capitalize()
D) title()
46. What is the effect of `my_string[::-1]` on a string `my_string`?
A) Returns the first half of the string
B) Returns the string with characters in reverse order
C) Returns the string with spaces removed
D) Returns the original string unchanged
47. What does the slice `my_string[3:]` represent for `my_string = 'Programming'`?
A) 'Pro'
B) 'gramming'
C) 'Prog'
D) 'ramming'
48. If `my_string = 'Python'`, what will `my_string[0:2]` return?
A) 'P'
B) 'Py'
C) 'thon'
D) 'Python'
49. Which operation is used to access a portion of a string in Python?
A) Indexing
B) Slicing
C) Concatenation
D) Iteration
50. What is the primary characteristic of a string in Python that prevents direct modification after creation?
A) Mutability
B) Immutability
C) Dynamic typing
D) Polymorphism