0% found this document useful (0 votes)
93 views5 pages

Computer Science-Worksheet

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)
93 views5 pages

Computer Science-Worksheet

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/ 5

Multiple Choice Questions (MCQs)

1. What is the output of the following code?


python
Copy code
s = "Hello World"
print(s[0:5])
a. Hello World
b. Hello
c. H
d. World

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

3. What will the following code output?


python
Copy code
lst = [1, 2, 3, 4, 5]
print(lst[1:4])
a. [1, 2, 3]
b. [2, 3, 4]
c. [3, 4, 5]
d. Error

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)

5. Which of the following statements is true about lists in Python?


a. Lists are immutable.
b. Lists can contain elements of different types.
c. Lists are defined using parentheses ().
d. None of the above.

6. What will be the result of the following code?


python
Copy code
s = "Python Programming"
print(s[-1])
a. g
b. m
c. P
d. IndexError

7. Which method is used to find the length of a list or a string?


a. len()
b. size()
c. count()
d. range()
8. If lst = [10, 20, 30, 40, 50], what will lst.pop(2) return?
a. 10
b. 20
c. 30
d. 40

9. Which of the following statements will create a list in Python?


a. lst = (1, 2, 3)
b. lst = {1, 2, 3}
c. lst = [1, 2, 3]
d. lst = 1, 2, 3

10. What is the result of "Python" + "Programming" in Python?


a. Python Programming
b. PythonProgramming
c. Error
d. None of the above

11.What is the output of the following code?

python
Copy code
s = "Python Programming"
print(s[0:6])

a. Python Programming
b. Python
c. Program
d. Error

12. If s = "Hello World", what does s[-5:] return?


a. Hello
b. World
c. lo Wo
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

1. What will be the output of the following code?

python
Copy code
s = "Python Programming"
print(s[7:18])

a. Python
b. Programming
c. Pytho
d. IndexError

2. If s = "Hello World", what does s[::2] return?


a. HloWrd
b. Hello World
c. el ol
d. Error

3. What is the output of the following code?

python
Copy code
s = "Python"
print(s[1:5:2])

a. yhn
b. yh
c. yt
d. Error

4. How can you reverse a string s = "education"?


a. s[::-1]
b. s[-1:-len(s):-1]
c. s[-1:-(len(s)+1):-1]
d. All of the above
5. Which slicing will return the substring "thon" from the string "Python Programming"?
a. s[2:6]
b. s[3:7]
c. s[2:6]
d. s[1:5]

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

7. What is the result of lst = [1, 2, 3, 4, 5]; print(lst[::-1])?


a. [1, 2, 3, 4, 5]
b. [5, 4, 3, 2, 1]
c. [2, 3, 4]
d. Error

8. What does lst[:3] return if

lst = [10, 20, 30, 40, 50]?


a. [30, 40, 50]
b. [10, 20, 30]
c. [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

10. What is the output of the following code?

python
Copy code
lst = [10, 20, 30, 40, 50]
print(lst[-3:])

a. [30, 40, 50]


b. [40, 50]
c. [10, 20, 30]
d. Error

You might also like