Strings and Lists - string slicing, immutability, string methods, list operations, mutability, cloning, list parameters - One Line Questions
1.
What is the difference between `find()` and `index()` string methods? —
`find()` returns -1 if not found, `index()` raises ValueError
2.
What is the difference between `sorted(my_list)` and `my_list.sort()`? —
`sort()` modifies the original list, `sorted()` returns a new sorted list
3.
Consider `'-'.join(['a', 'b', 'c'])`. What is the result? —
'a-b-c'
4.
What will `my_string.replace('l', 'X')` return if `my_string = 'hello world'`? —
'heXlo worXd'
5.
If `my_string = 'Python'`, what will `my_string[0:2]` return? —
'Py'
6.
What does the slice `my_string[3:]` represent for `my_string = 'Programming'`? —
'ramming'
7.
If `my_list = ['a', 'b', 'c']`, what is the result of `my_list * 2`? —
['a', 'b', 'c', 'a', 'b', 'c']
8.
What does `my_string.partition(' ')` return for `my_string = 'hello world'`? —
['hello', ' ', 'world']
9.
If `my_list = [1, 2, 3]`, what will `my_list.insert(1, 10)` change the list to? —
[1, 10, 2, 3]
10.
If `list1 = [1, 2]` and `list2 = [3, 4]`, what is the result of `list1 + list2`? —
[1, 2, 3, 4]
11.
If `my_list = [1, 2, 3, 4, 5]` and `new_list = my_list[1:4]`, what is `new_list`? —
[2, 3, 4]
12.
If `list1 = [1, 2]` and `list2 = [3, 4]`, what is the result of `list1.extend(list2)`? —
[1, 2, 3, 4]
13.
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)`? —
[1, 2, 3, 4]
14.
If `my_list = [10, 20, 30]` and you execute `my_list.append(40)`, what is the final state of `my_list`? —
[10, 20, 30, 40]
15.
What does `my_string.count('a')` return for `my_string = 'banana'`? —
3
16.
What is a 'deep copy' of a list? —
A copy where changes to nested mutable objects do not affect the original
17.
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`? —
An alias (both variables point to the same list object)
18.
When a list is passed as an argument to a function, what happens if the function modifies the list? —
The original list outside the function is modified
19.
What does the `extend()` list method do? —
Appends elements from an iterable to the end of the list
20.
What is the term for creating an independent copy of a list? —
Cloning
21.
Which list operation is used to combine two lists? —
extend()
22.
Which string method converts the first character of each word in a string to uppercase? —
title()
23.
Which string method is used to find the first occurrence of a substring within another string? —
find()
24.
What is the primary characteristic of a list in Python that allows modification after creation? —
Mutability
25.
Which operation is used to access a portion of a string in Python? —
Slicing
26.
Which operation is used to add an element to the end of a list? —
append()
27.
Which string method checks if all characters in the string are alphanumeric? —
isalnum()
28.
What is a potential pitfall of assigning one list variable to another using `list2 = list1`? —
It creates an alias, so changes to one affect the other
29.
What does slicing a list, like `new_list = old_list[:]`, achieve? —
It performs a shallow copy of the list
30.
What does the `split()` method do to a string by default? —
Splits the string into a list of words using whitespace as a delimiter
31.
Which method is commonly used for shallow cloning of a list? —
list.copy()
32.
Which string method is used to convert all characters in a string to uppercase? —
upper()
33.
What is the primary characteristic of a string in Python that prevents direct modification after creation? —
Immutability
34.
Which module in Python provides functions for creating deep copies? —
copy
35.
What is the term for passing a list to a function such that the function operates on the original list object? —
Pass by reference
36.
When passing a list to a function, the function receives a reference to the original list object. This is often referred to as: —
Pass by reference
37.
Which list method is used to remove the first occurrence of a specific value from the list? —
remove()
38.
Which list operation returns a new list containing elements from a specified range? —
slice
39.
What does `my_list.pop(2)` do if `my_list = [10, 20, 30, 40]`? —
Removes the element at index 2 (30) and returns it
40.
What is the effect of `my_string[::-1]` on a string `my_string`? —
Returns the string with characters in reverse order
41.
Which list method sorts the list in place? —
sort()
42.
Which string method returns a list of all the words in a string, using whitespace as the delimiter? —
split()
43.
What does the `join()` string method do? —
Combines elements of an iterable (like a list) into a string, using the string as a separator
44.
Which string method checks if a string ends with a specified suffix? —
endswith()
45.
Which string method removes whitespace from the beginning and end of a string? —
strip()
46.
What is the consequence of modifying a list passed as an argument to a function without creating a copy? —
The original list is modified.
47.
What is the slicing syntax `[start:stop:step]` used for in Python? —
To access elements of sequences like strings and lists with an optional step
48.
What is the purpose of `del my_list[index]`? —
To remove the element at `index` without returning it
49.
Which string method returns a copy of the string with its first character capitalized? —
capitalize()
50.
How can you create a deep copy of `my_list`? —
Using `import copy; new_list = copy.deepcopy(my_list)`