String Manipulation — Question 11
Back to all questionsQuestion 5
Carefully go through the code given below and answer the questions based on it :
inputStr = input(" Give me a string:")
biglnt = 0
littlelnt = 0
otherlnt = 0
for ele in inputStr:
if ele >= 'a' and ele <= 'm': # Line 1
littlelnt = littlelnt + 1
elif ele > 'm' and ele <= 'z':
biglnt = biglnt + 1
else:
otherlnt = otherlnt + 1
print (biglnt) # Line 2
print (littlelnt) # Line 3
print (otherlnt) # Line 4
print (inputStr.isdigit()) # Line 5(i) Given the input abcd what output is produced by Line 2?
- 0
- 1
- 2
- 3
- 4
Answer
Option 1 — 0
Explanation
In the input abcd, all the letters are between a and m so the condition — if ele >= 'a' and ele <= 'm' is always true. Hence, biglnt is 0.
(ii) Given the input Hi Mom what output is produced by Line 3?
- 0
- 1
- 2
- 3
- None of these
Answer
Option 3 — 2
Explanation
In the input Hi Mom, only two letters i and m satisfy the condition — if ele >= 'a' and ele <= 'm'. Hence, value of littlelnt is 2.
(iii) Given the input Hi Mom what output is produced by Line 4?
- 0
- 1
- 2
- 3
- None of these
Answer
Option 4 — 3
Explanation
In the input Hi Mom, 3 characters H, M and space are not between a and z. So for these 3 characters the statement in else part — otherlnt = otherlnt + 1 is executed. Hence, value of otherlnt is 3.
(iv) Given the input 1+2 =3 what output is produced by Line 5?
- 0
- 1
- True
- False
- None of these
Answer
Option 4 — False
Explanation
As all characters in the input string 1+2 =3 are not digits hence isdigit() returns False.
(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'?
- No change
- otherlnt would be larger
- littlelnt would be larger
- biglnt would be larger
- None of these
Answer
Option 2 — otherlnt would be larger
Explanation
For letter m, now else case will be executed increasing the value of otherlnt.