Computer Science-Worksheet
Computer Science-Worksheet
2. Which of the following methods is used to convert all characters of a string to uppercase?
a. upper()
b. capitalize()
c. lower()
d. title()
4. How can you add an element 6 at the end of a list lst in Python?
a. lst.add(6)
b. lst.append(6)
c. lst.insert(6)
d. lst.extend(6)
python
Copy code
s = "Python Programming"
print(s[0:6])
a. Python Programming
b. Python
c. Program
d. Error
13. What does the step argument in slicing do? Explain the output of s = "abcdef";
print(s[::2])
14. Write a code snippet to reverse the string "education" using slicing.
15. How can you extract every second character from the string "abcdefghi"?
16. Given the list lst = [10, 20, 30, 40, 50], what will lst[1:4] return?
a. [10, 20, 30]
b. [20, 30, 40]
c. [30, 40, 50]
d. Error
17. What is the result of the following code?
lst = [1, 2, 3, 4, 5]
print(lst[:])
a. [1, 2, 3, 4, 5]
b. [5, 4, 3, 2, 1]
c. [2, 3, 4]
d. Error
String Slicing
python
Copy code
s = "Python Programming"
print(s[7:18])
a. Python
b. Programming
c. Pytho
d. IndexError
python
Copy code
s = "Python"
print(s[1:5:2])
a. yhn
b. yh
c. yt
d. Error
List Slicing
6. Given lst = [10, 20, 30, 40, 50], what does lst[1:4] return?
a. [10, 20, 30]
b. [20, 30, 40]
c. [30, 40, 50]
d. Error
9. How do you extract every second element from a list lst = [1, 2, 3, 4, 5, 6]?
a. lst[1:5:2]
b. lst[::2]
c. lst[1:4:2]
d. Both b and c
python
Copy code
lst = [10, 20, 30, 40, 50]
print(lst[-3:])