Expanded: Based on NCERT Exercises. 10 (1-mark: Outputs from Q1), 10 (4-marks: Outputs/Explanations from Q2), 10 (8-marks: Programming Problems with Code). Full questions and mark-appropriate answers.
1-Mark Questions (10 Questions: Outputs from Exercise Q1 - mySubject = "Computer Science")
Q1: What is the output of print(mySubject[0:len(mySubject)])?
Ans: Computer Science (Full string from index 0 to end).
Q2: What is the output of print(mySubject[-7:-1])?
Ans: Scienc (Substring from negative index -7 to -1 exclusive).
Q3: What is the output of print(mySubject[::2])?
Ans: Cmue cec (Every second character from start to end).
Q4: What is the output of print(mySubject[len(mySubject)-1])?
Ans: e (Last character).
Q5: What is the output of print(2*mySubject)?
Ans: Computer ScienceComputer Science (String repeated twice).
Q6: What is the output of print(mySubject[::-2])?
Ans: eniSrtpo (Every second character from end to start).
Q7: What is the output of print(mySubject[:3] + mySubject[3:])?
Ans: Computer Science (Concatenation of first 3 and rest, reconstructs original).
Q8: What is the output of print(mySubject.swapcase())?
Ans: cOMPUTER sCIENCE (Swaps case of all letters).
Q9: What is the output of print(mySubject.startswith('Comp'))?
Ans: True (Starts with 'Comp').
Q10: What is the output of print(mySubject.isalpha())?
Ans: False (Contains space, not purely alphabetic).
4-Mark Questions (10 Questions: Outputs/Explanations from Exercise Q2 - myAddress = "WZ-1,New Ganga Nagar,New Delhi")
Q11: What is the output of print(myAddress.lower())? Explain briefly.
Ans: wz-1,new ganga nagar,new delhi. Converts all uppercase to lowercase; non-letters unchanged. (4 marks: Output + 1 sentence explanation).
Q12: What is the output of print(myAddress.upper())? Explain briefly.
Ans: WZ-1,NEW GANGA NAGAR,NEW DELHI. Converts all lowercase to uppercase. (4 marks: Output + explanation).
Q13: What is the output of print(myAddress.count('New'))? Explain.
Ans: 2. Counts non-overlapping occurrences of 'New'. (4 marks: Output + how count works).
Q14: What is the output of print(myAddress.find('New'))? Explain.
Ans: 5. Returns lowest index of first occurrence; -1 if not found. (4 marks: Output + method behavior).
Q15: What is the output of print(myAddress.rfind('New'))? Explain.
Ans: 21. Returns highest index of occurrence. (4 marks: Output + difference from find).
Q16: What is the output of print(myAddress.split(','))? Explain.
Ans: ['WZ-1', 'New Ganga Nagar', 'New Delhi']. Splits on comma into list. (4 marks: Output + splitting logic).
Q17: What is the output of print(myAddress.split(' '))? Explain.
Ans: ['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']. Splits on space; default whitespace. (4 marks: Output + default behavior).
Q18: What is the output of print(myAddress.replace('New','Old'))? Explain.
Ans: WZ-1,Old Ganga Nagar,Old Delhi. Replaces all 'New' with 'Old'. (4 marks: Output + replacement rule).
Q19: What is the output of print(myAddress.partition(','))? Explain.
Ans: ('WZ-1', ',', 'New Ganga Nagar,New Delhi'). Partitions into tuple at first ','. (4 marks: Output + 3-part structure).
Q20: What is the output of print(myAddress.index('Agra'))? Explain.
Ans: ValueError: substring not found. Raises error if substring absent (unlike find's -1). (4 marks: Output + error vs find).
8-Mark Questions (10 Questions: Programming Problems from NCERT - With Full Code & Explanation)
Q21: Write a program to input line(s) of text until enter, count characters (incl spaces), alphabets, digits, symbols, words (space-separated). (8 marks)
Ans: Use loop for input; counters with isalpha/isalnum/isdigit/not; split for words.
# Program for text analysis
text = ''
while True:
line = input()
if line == '':
break
text += line + ' '
total_chars = len(text)
alphabets = sum(1 for c in text if c.isalpha())
digits = sum(1 for c in text if c.isdigit())
symbols = total_chars - alphabets - digits - text.count(' ')
words = len(text.split())
print(f"Chars: {total_chars}, Alphabets: {alphabets}, Digits: {digits}, Symbols: {symbols}, Words: {words}")
Explanation: Loop accumulates text; counters use built-ins; split() for words. (8 marks: Code + logic + sample output).
Q22: Write UDF to convert multi-word string to title case (param string). Explain. (8 marks)
Ans: Use .title() method.
def toTitle(s):
return s.title()
st = input("Enter string: ")
print(toTitle(st))
Explanation: title() capitalizes first letter of each word. Handles multiple words automatically. (8 marks: Code + method use + example).
Q23: Write deleteChar(str, char) UDF to delete all occurrences of char, return new string. Explain. (8 marks)
Ans: Loop/build new string skipping char.
def deleteChar(st, ch):
newstr = ''
for c in st:
if c != ch:
newstr += c
return newstr
st = input("String: ")
ch = input("Char: ")
print(deleteChar(st, ch))
Explanation: Iteration skips matches; immutable strings require new build. (8 marks: Code + immutability note).
Q24: Input string with digits; UDF to return sum of digits. Explain. (8 marks)
Ans: Loop check isdigit, add int(c).
def sumDigits(st):
total = 0
for c in st:
if c.isdigit():
total += int(c)
return total
st = input("String: ")
print(sumDigits(st))
Explanation: isdigit() identifies; int() converts for sum. (8 marks: Code + type conversion).
Q25: UDF takes sentence, replaces spaces with '-', returns modified. Explain. (8 marks)
Ans: Replace ' ' with '-'.
def replaceSpaces(sent):
return sent.replace(' ', '-')
sent = input("Sentence: ")
print(replaceSpaces(sent))
Explanation: replace() method for all occurrences. (8 marks: Code + method efficiency).
Q26: Explain indexing with positive/negative examples from 'Hello World!'. (8 marks)
Ans: Positive: [0]='H', [6]='W'; Negative: [-1]='!', [-12]='H'. len=12. Table 8.1. Immutability prevents change. (8 marks: Examples + diagram description + errors).
Q27: Detail slicing [n:m:k] with examples. Explain step/negative. (8 marks)
Ans: [1:5]='ello'; [::2]='HloWr'; [::-1]=reverse. Negative slices from end. (8 marks: 3 examples + syntax + edge cases).
Q28: Describe 5 string methods (e.g., lower, find, replace) with code examples. (8 marks)
Ans: lower(): 'HELLO'.lower()='hello'; find(): 'Hello'.find('l')=2; replace(): 'Hello'.replace('l','*')='He**o'. Etc. (8 marks: Methods + outputs + uses).
Q29: Write UDF charCount like Prog 8-1; explain loop. (8 marks)
Ans: As in Prog 8-1. Loop iterates each char, increments if match. Returns count. (8 marks: Code + traversal explanation).
Q30: Explain palindrome check (Prog 8-5); write similar for reverse. (8 marks)
Ans: Two pointers i=0, j=len-1; compare while i<=j. For reverse: range(-1,-len-1,-1). (8 marks: Logic + code snippet + efficiency).