Class 11 Computer Science Chapter 8 Question Bank CBSE Board Pattern

Section A — MCQs (10 questions, 1 mark each)

Q1. Which of the following is used to create a multi-line string in Python?
(a) Single quotes
(b) Double quotes
(c) Triple quotes
(d) Both (a) and (b)

Q2. What will be the output of the following code?
python str1 = "Hello World!" print(str1[6])
(a) W
(b) o
(c) r
(d) Error

Q3. Which of the following statements is correct about strings in Python?
(a) Strings are mutable
(b) Strings are immutable
(c) Strings can only be created using single quotes
(d) Negative indexing is not allowed in strings

Q4. The expression 'Wor' in "Hello World!" evaluates to:
(a) True
(b) False
(c) Error
(d) None

Q5. What does the slicing operation str1[7:2] return when str1 = "Hello World!"?
(a) "lo World!"
(b) "World"
(c) "" (empty string)
(d) Error

Q6. Which built-in function returns the number of times a substring occurs in a string?
(a) find()
(b) count()
(c) index()
(d) replace()

Q7. Assertion (A): Python does not have a separate character data type.
Reason (R): A string of length one is considered as a character in Python.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.

Q8. Assertion (A): The statement str1[1] = 'a' raises an error when str1 = "Hello".
Reason (R): Strings in Python are immutable.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.

Q9. The output of "Hello World!"[::-1] is:
(a) "Hello World!"
(b) "!dlroW olleH"
(c) "dlroW olleH"
(d) Error

Q10. Which method returns True if all characters in a string are whitespace characters?
(a) islower()
(b) isspace()
(c) isalnum()
(d) istitle()

Section B — Very Short Answer (6 questions, 2 marks each)

Q1. Define a string. How can a string be created in Python? Give one example using triple quotes.

Q2. Differentiate between positive and negative indexing in strings with reference to the string "Hello World!".

Q3. What is the output of the following code? Explain briefly.
python str1 = "Hello" print(str1 * 3)

Q4. What will be the output of the following code?
python str1 = "Hello World!" print(str1[1:5]) print(str1[:5])

Q5. What is the difference between the find() and index() methods in Python strings?

Q6. Write the output of the following:
python str1 = "Hello World!" print('W' in str1) print('My' not in str1)

Section C — Short Answer (5 questions, 3 marks each)

Q1. Explain string traversal using for loop with a suitable example from the chapter.

Q2. Identify the error in the following code and correct it:
python str1 = "Hello World!" str1[0] = 'h' print(str1)

Q3. What will be the output of the following code? Explain.
python str1 = "Hello World!" print(str1[0:10:2]) print(str1[-6:-1])

Q4. Write a short code fragment using while loop to traverse and print all characters of the string "Hello World!" without using the len() function in the loop condition.

Q5. Identify the error in the following code and write the corrected version:
python str1 = 'Hello World!' print(str1[15])

Section D — Long Answer (3 questions, 5 marks each)

Q1. Write a complete Python program using a user-defined function to count the number of times a character (passed as argument) occurs in a given string. The program should take input from the user.

Q2. Explain the algorithm to reverse a string without creating a new string. Dry-run the algorithm with a trace table for the input string "Hello".

Q3. Write a complete Python program using a user-defined function that replaces all vowels in a string with '*'. The function should return the modified string.

Section E — Case/Source-Based (2 questions, 4 marks each)

Q1. A teacher wants to analyse a paragraph entered by students. She needs to count specific characters, check for substrings, and modify the text. Consider the following code structure based on string concepts:
```python paragraph = input("Enter paragraph: ") ch = input("Enter character: ")

Further processing using string operations

`` (i) How will you find the length of the paragraph? (ii) Write a statement to check if the characterchis present in the paragraph. (iii) How can the paragraph be converted to title case? (iv) Write code to count occurrences ofch` in the paragraph.

Q2. A student is creating a program to validate and process usernames. The program checks various string properties and performs slicing operations.
```python username = input("Enter username: ")

Validation and processing

`` (i) How will you check if the username contains only alphabets and digits? (ii) Write a statement to check if the username starts with an uppercase letter. (iii) How can you extract the first three characters of the username? (iv) Write code to replace all occurrences of'a'with'*'` in the username.

Answer Key Attempt all questions first,
then tap to reveal

Section A

Q1. (c) Triple quotes
Q2. (a) W (index 6 points to the 7th character 'W')
Q3. (b) Strings are immutable
Q4. (a) True
Q5. (c) "" (empty string)
Q6. (b) count()
Q7. (a) Both true and R explains A
Q8. (a) Both true and R explains A
Q9. (b) "!dlroW olleH"
Q10. (b) isspace()

Section B

Q1. A string is a sequence made up of one or more UNICODE characters. It can be created by enclosing characters in single, double or triple quotes. Example: str3 = """Hello World!"""
Q2. Positive indexing starts from 0 (left to right). Negative indexing starts from -1 (right to left). For "Hello World!", positive index 0 = 'H', negative index -1 = '!'.
Q3. HelloHelloHello (repetition operator * repeats the string).
Q4. ello and Hello (slicing str1[1:5] and str1[:5]).
Q5. find() returns -1 if substring is not found; index() raises ValueError.
Q6. True
False

Section C

Q1. String can be traversed using for ch in str1: print(ch, end=''). The loop automatically accesses each character from start to end.
Q2. Error: TypeError: 'str' object does not support item assignment. Correction: Create a new string instead of modifying in place (strings are immutable).
Q3. HloWr and World (step size 2 and negative slicing).
Q4. index = 0
while index < len(str1):
print(str1[index], end='')
index += 1
Q5. IndexError: string index out of range. Corrected: Use valid index or len() to check bounds.

Section D

Q1.

```python def charCount(ch, st): count = 0 for character in st: if character == ch: count += 1 return count

st = input("Enter a string: ") ch = input("Enter the character to be searched: ") count = charCount(ch, st) print("Number of times character", ch, "occurs in the string is:", count) `` **Q2.** Algorithm: Use two pointersi=0,j=len-1. Swap characters whilei <= j`. Trace table for "Hello" shows character comparisons and swaps.

Q3.

```python def replaceVowel(st): newstr = '' for character in st: if character in 'aeiouAEIOU': newstr += '*' else: newstr += character return newstr

st = input("Enter a String: ") st1 = replaceVowel(st) print("The original String is:", st) print("The modified String is:", st1) ```

Section E

Q1. (i) len(paragraph) (ii) ch in paragraph (iii) paragraph.title() (iv) paragraph.count(ch)
Q2. (i) username.isalnum() (ii) username[0].isupper() (iii) username[:3] (iv) username.replace('a', '*')

All questions are answerable from the NCERT chapter text. Reviewed by GFIS faculty.