String Manipulation || Type B || Sumita Arora || Class 11 || Computer science || Information practices || Solution
Q1. What is the result of the following expressions?
(a)
print ("""
1
2
3
""")
(b)
text = "Test. \nNext line."
print (text)
(c)
print ('One', 'Two' * 2)
print ('One' + 'Two' * 2)
print (len(' 0123456789'))
(d)
s = '0123456789'
print (s[3], s[0:3], '-', s [2:5])
print (s[:3], '-', s[3:], ',', s[3:100])
print (s[20:], s[2:1], s[1:1])
(e)
s = '987654321'
print (s[-1], s[-3])
print (s[-3:], s[:-3])
print (s[-100:-3], s[-100:3])
Q2. What will be the output produced by following code fragments?
(a)
y = str(123)
x = "hello" * 3
print (x , y)
x = "hello" + "world"
y = len(x)
print (y, x)
(b)
x = "hello" +
"to Python" +
"world"
for chr in x :
y = chr
print (y, ":", end = ' ')
(c)
x = "hello world"
print (x[:2], x[:-2], x[-2:])
print (x[6], x[2:4])
print (x[2:-3], x[-4:-2])
Q3. Carefully go through the code given below and answer the questions based on it:
theStr = 'This is a test '
inputStr = input ("Enter integer: ")
inputInt = int (inputStr)
testStr = theStr
while inputInt >= 0 :
testStr = testStr[1:-1]
inputInt = inputInt - 1
testBool = 't' in testStr
print (theStr)# Line 1
print (testStr)# Line 2
print (inputInt)# Line 3
print (testBool)# Line 4
(i) Given the input integer 3, what output is produced by Line 1?
(a) This is a test
(b) This is a
(c) is a test
(d) is a
(e) None of these
(ii) Given the input integer 3, what output is produced by Line 2?
(a) This is a test
(b) This is a
(c) is a test
(d) is a
(e) None of these
(iii) Given the input integer 2. What output is produced by Line 3?
(a) 0
(b) 1
(c) 2
(d) 3
(e) None of these
(iv) Given the input integer 2. What output is produced by Line 4?
(a) False
(b) True
(c) 0
(d) 1
(e) None of these
Q4. Carefully go through the code given below and answer the questions based on it
testStr = "abcdefghi"
inputStr = input ("Enter integer : ")
inputInt = int(inputStr)
count = 2
newStr = ' '
while count <= inputInt :
newStr = newStr + testStr[0 : count]
testStr = testStr[2 : ]#Line 1
count = count + 1
print (newStr)#Line 2
print (testStr)#Line 3
print (count)#Line 4
print (inputInt)#Line 5
(i) Given the input integer 4 what output is produced by Line 2?
(a) abcdefg
(b) aabbccddeeffgg
(c) abcdeefg
(d) ghi
(e) None of these
(ii) Given the input integer 4, what output is produced by Line 3?
(a) abcdefg
(b) aabbccddeeffgg
(c) abcdeefg
(d) ghi
(e) None of these
(iii) Given the input integer 3, what output is produced by Line 4?
(a) 0
(b) 1
(c) 2
(d) 3
(e) None of these
(iv) Given the input integer 3, what output is produced by Line 5?
(a) 0
(b) 1
(c) 2
(d) 3
(e) None of these
(v) Which statement is equivalent to the statement found in Line 1?
(a) testStr = testStr[2:0]
(b) testStr = testStr[2:-1]
(c) testStr = testStr[2:-2]
(d) teststr = teststr - 2
(e) None of these
Q5. Carefully go through the code given below and answer the questions based on it.
inputStr = input("Give me a string: ")
bigint = 0
littlelnt = 0
otherlnt = 0
for ele in inputStr :
if ele >= 'a' and ele <= 'm' :#Line 1
littleInt = littleInt + 1
elif ele > 'm' and ele <= 'z' :
bigint = bigint + 1
else :
otherInt - otherInt + 1
print (bigInt)#Line 2
print (littlelnt)#Line 3
print (otherInt)#Line 4
print (inputStr.isdigit())#Line 5
(i) Given the input abcd what output is produced by Line 2?
(a) o
(b) 1
(c) 2
(d) 3
(e) None of these
(ii) Given the input Hi Mom what output is produced by Line 3?
(a) 0
(b) 1
(c) 2
(d) 3
(e) None of these
(iii) Given the input Hi Mom what output is produced by Line 4?
(a) 0
(b) 1
(c) 2
(d) 3
(e) None of These
(iv) Given the input 1+ 2 = 3 what output is produced by Line 5?
(a) 0
(b) 1
(c) True
(d) False
(e) None of these
(v) Give the input Hi Mom, what changes result from modifying Line 1 from
if ele >= 'a' and ele <= 'm' to the expression
if ele >= 'a' and ele <= 'm'?
(a) No change
(b) otherInt would be larger
(c) littlelet would be larger
(d) bigint would be larger
(e) None of these
Q6. Carefully go through the code given below and answer the questions based on it:
in1Str = input ("Enter string of digits: ")
in2Str = input ("Enter string of digits: ")
if len(in1Str) > len(in2Str) :
small = in2Str
large = in1Str
else :
small = in1Str
large = in2Str
newStr = ' '
for element in small :
result = int(element) + int(large[0])
newStr = newStr + str(result)
large = large[1:]
print (len(newStr))#Line 1
print(newStr)#Line 2
print (large)#Line 3
print (small)#Line 4
(i) Given a first input of 12345 and a second input of 246, what result is produced by a Line 1?
(a) 1
(b) 3
(c) 5
(e) None of these
(ii) Given a first input of 12345 and a second input of 246, what result is produced by Line 2?
(a) 369
(b) 246
(c) 234
(d) 345
(e) None of these
(iii) Given a first input of 123 and a second input of 4567, what result is produced by Line 3?
(a) 3
(b) 7
(c) 12
(d) 45
(e) None of these
(iv) Given a first input of 123 and a second input of 4567, what result is produced by Line 4?
(a) 123
(b) 4567
(c) 7
(e) None of these
Q7. Find the output:
(a)
S = input("Enter String :")
RS = ' '
for ch in S :
RS = ch + RS
print(S + RS)
(b)
S = input("Enter string : ")
RS = " "
for ch in S :
RS = ch + 2 + RS
print (RS + S)
Q8. Find the errors:
(a)
S = "PURA VIDA"
print(S[9]) + S[9: 15])
(b)
S = "PURA VIDA"
S1 = S[: 10] + S[10 :]
S2 = S[10] + S[-10]
(c)
S = "PURA VIDA"
S1 = S * 2
S2 = S1[-19] + S1[-20]
S3 = S1[-19:]
(d)
S = "PURA VIDA"
S1 = S[: 5]
S2 = S[5: ]
S3 = S1 * S2
S4 = S2 + '3'
S5 = S1 + 3
Q9. What is the output produced ?
(i) >>> "whenever".find("never")
(ii) >>> "whenever".find("what")
Q10. What is the output produced ?
(i) >>> "-".join(['123','365', '1319'])
(ii) >>> " ".join(['Python', 'is', 'fun'])
Q11. Given a string S, write expressions to print
(i) First five characters of S
(ii) Ninth character of S
(iii) Reversed S
(iv) Alternate characters from reversed S
fekuchand
ReplyDelete??
Deleteyes
Deletevery heplful
ReplyDelete: )
DeletePlese explain properly
ReplyDeleteWhich one .
DeleteWhere is the answer for the questions
ReplyDeletePlease click on Question to get the answer of that question.
DeleteVery usefull thank you
ReplyDeleteMay I know your stream
Deleteyour welcome...........................
DeleteThank for helping us in such big way I always use to write answer for you site it is very helpful to me and others
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )