0% found this document useful (0 votes)
163 views6 pages

Class 11 String With Solution

string

Uploaded by

Ak Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views6 pages

Class 11 String With Solution

string

Uploaded by

Ak Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Worksheet on String Manipulation (Class 11)

Section A: Multiple Choice Questions (25 Questions)

Instructions: Choose the most appropriate answer from the given options.
1. What will be the output of the following Python code?
Python
s = "Python"
print(s[1:5:2])
(A) yh
(B) yt
(C) y
(D) h
2. If s = "Hello World", what is the value of s.find('l', 3)?
(A) 2
(B) 3
(C) 9
(D) -1
3. Which of the following methods returns a copy of the string with its first character capitalized and the rest in
lowercase?
(A) capitalize()
(B) title()
(C) upper()
(D) casefold()
4. Given s = "Programming", what is the output of s.replace('m', 'm', 1)?
(A) Programing
(B) Prograimming
(C) Programming
(D) Prograamming
5. What will be the output of len(" Python ".strip())?
(A) 10
(B) 8
(C) 6
(D) 4
6. How many times will World be printed by the following code?
Python
s = "Hello World"
print(s.count('o'))
(A) 0
(B) 1
(C) 2
(D) 3
7. Which of the following is an immutable data type in Python?
(A) List
(B) Dictionary
(C) String
(D) Set
8. What is the output of print("abc".startswith("a"))?
(A) True
(B) False
(C) Error
(D) None
9. What is the result of print("Python" + " " * 2 + "is cool")?
(A) Python is cool
(B) Python is cool
(C) Python is cool (with one space)
(D) Python is cool
10. What is the value of s[len(s)//2] if s = "Computer"?
(A) p
(B) u
(C) t
(D) e
11. What does the partition() method return?
(A) A list of substrings
(B) A tuple of three strings
(C) A string with the first occurrence of the specified separator removed
(D) The index of the first occurrence of the separator
12. If s = "cbse" and s.upper() == "CBSE", what will the expression evaluate to?
(A) True
(B) False
(C) Error
(D) None
13. Which of the following statements about split() and join() methods is correct?
(A) split() takes a string and returns a list, while join() takes a list and returns a string.
(B) split() takes a list and returns a string, while join() takes a string and returns a list.
(C) Both split() and join() take a string and return a list.
(D) Both split() and join() take a list and return a string.
14. What is the output of "Hello".center(10, '*')?
(A) **Hello***
(B) ***Hello**
(C) Hello
(D) *****Hello
15. What is the output of the code s = "hello"; s[0] = 'H'; print(s)?
(A) Hello
(B) hEllo
(C) Error
(D) HELLO
16. What is the output of print('py' in 'python')?
(A) True
(B) False
(C) Error
(D) None
17. Which of the following is true about string slicing in Python?
(A) The last index is exclusive.
(B) The first index is exclusive.
(C) Both indices are exclusive.
(D) Both indices are inclusive.
18. What does s.isalnum() return if s = "123Test"?
(A) True
(B) False
(C) Error
(D) None
19. What is the output of print("Python"[::-1])?
(A) Python
(B) nohtyp
(C) n
(D) P
20. What is the output of print("123".isdecimal())?
(A) True
(B) False
(C) Error
(D) None
21. What does the strip() method do?
(A) Removes whitespace from the right side of the string.
(B) Removes whitespace from the left side of the string.
(C) Removes leading and trailing whitespace.
(D) Removes all whitespace characters.
22. Which of the following will result in an error?
(A) s = 'A' * 3
(B) s = 'B' + 'C'
(C) s = "D" + 5
(D) s = "E" * 2
23. What will be the output of s = "CBSE"; print(s.islower())?
(A) True
(B) False
(C) Error
(D) None
24. What does the rfind() method do?
(A) Finds the first occurrence of a substring.
(B) Finds the last occurrence of a substring.
(C) Finds all occurrences of a substring.
(D) Replaces the last occurrence of a substring.
25. What is the output of print("Python is fun".split(' '))?
(A) ['Python', 'is', 'fun']
(B) ['Python', 'is', 'fun'] (with single quotes)
(C) Pythonisfun
(D) Python is fun

Section B: Find the Output (15 Questions)

Instructions: Predict the output for each of the following code snippets.

1. Python
2. s = "Informatics"
3. for i in range(len(s)):
4. if i % 2 == 0:
5. print(s[i], end='')
6. Python
7. s = "CBSE"
8. new_s = ''
9. for i in s:
10. new_s += i.lower()
11. print(new_s)
12. Python
13. str1 = "Hello World"
14. str2 = str1.replace('o', 'a')
15. print(str2)
16. Python
17. s = "python"
18. print(s.upper()[1:3])
19. Python
20. s = "programming"
21. print(s.count('g'))
22. Python
23. s = "Computer Science"
24. print(s.split())
25. Python
26. s = "Data Science"
27. print(s.split('a'))
28. Python
29. s = "hello"
30. print(s.capitalize())
31. Python
32. s = "PYTHON"
33. print(s.swapcase())
34. Python
35. s = "hello world"
36. print(s.find('o', 5))
37. Python
38. s = " Python "
39. print(s.strip())
40. Python
41. s = "abcde"
42. print(s[len(s)-1])
43. Python
44. s = "Computer"
45. print(s.find('m'))
46. Python
47. s = "Python is awesome"
48. print(" ".join(s.split()))
49. Python
50. s = "programming"
51. print(s.startswith("p"))

Section C: Programming Based Questions (5 Questions)

Instructions: Write Python code to solve the following problems.

1. Write a Python program that takes a string as input and counts the number of vowels (a, e, i, o, u), both
uppercase and lowercase, and prints the total count.
2. Write a program to check if a given string is a palindrome. A palindrome is a sequence of characters that
reads the same forwards and backward (e.g., "madam").
3. Write a Python program to reverse a given string without using a built-in function like s[::-1].
4. Write a program to remove all leading and trailing whitespace from a given string without using the
strip() method.
5. Write a program that takes a string and a substring as input and counts the number of times the substring
appears in the main string. Do not use the count() method.

Solutions

Section A: Multiple Choice Questions - Solutions

1. (B) yt
2. (C) 9
3. (A) capitalize()
4. (C) Programming
5. (C) 6
6. (B) 1
7. (C) String
8. (A) True
9. (B) Python is cool
10. (A) p
11. (B) A tuple of three strings
12. (A) True
13. (A) split() takes a string and returns a list, while join() takes a list and returns a string.
14. (B) ***Hello**
15. (C) Error (Strings are immutable)
16. (A) True
17. (A) The last index is exclusive.
18. (A) True
19. (B) nohtyp
20. (A) True
21. (C) Removes leading and trailing whitespace.
22. (C) s = "D" + 5 (Cannot concatenate a string and an integer)
23. (B) False
24. (B) Finds the last occurrence of a substring.
25. (A) ['Python', 'is', 'fun']

Section B: Find the Output - Solutions

1. Ifrmtc
2. cbse
3. Hella Warld
4. YT
5. 2
6. ['Computer', 'Science']
7. ['D', 't', ' Science']
8. Hello
9. python
10. 7
11. Python
12. e
13. 2
14. Python is awesome
15. True

Section C: Programming Based Questions - Solutions

1. Python
2. s = input("Enter a string: ")
3. vowels = "aeiouAEIOU"
4. count = 0
5. for char in s:
6. if char in vowels:
7. count += 1
8. print("Number of vowels:", count)
9. Python
10. s = input("Enter a string: ")
11. s_rev = s[::-1]
12. if s == s_rev:
13. print("The string is a palindrome.")
14. else:
15. print("The string is not a palindrome.")
16. Python
17. s = input("Enter a string: ")
18. reversed_s = ""
19. for char in s:
20. reversed_s = char + reversed_s
21. print("Reversed string:", reversed_s)
22. Python
23. s = input("Enter a string: ")
24. start_index = 0
25. end_index = len(s) - 1
26.
27. while start_index <= end_index and s[start_index].isspace():
28. start_index += 1
29.
30. while end_index >= start_index and s[end_index].isspace():
31. end_index -= 1
32.
33. trimmed_s = s[start_index:end_index + 1]
34. print("Trimmed string:", trimmed_s)
35. Python
36. main_string = input("Enter the main string: ")
37. sub_string = input("Enter the substring to count: ")
38. count = 0
39. for i in range(len(main_string) - len(sub_string) + 1):
40. if main_string[i:i + len(sub_string)] == sub_string:
41. count += 1
42. print("The substring appears", count, "times.")

You might also like