String Manipulation — Question 9
Back to all questionsQuestion 3
Carefully go through the code given below and answer the questions based on it :
theStr = " This is a test "
inputStr = input(" Enter integer: ")
inputlnt = int(inputStr)
testStr = theStr
while inputlnt >= 0 :
testStr = testStr[1:-1]
inputlnt = inputlnt - 1
testBool = 't' in testStr
print (theStr) # Line 1
print (testStr) # Line 2
print (inputlnt) # Line 3
print (testBool) # Line 4 (i) Given the input integer 3, what output is produced by Line 1?
- This is a test
- This is a
- is a test
- is a
- None of these
Answer
Option 1 — This is a test
(ii) Given the input integer 3, what output is produced by Line 2?
- This is a test
- s is a t
- is a test
- is a
- None of these
Answer
Option 2 — s is a t
Explanation
As input is 3 and inside the while loop, inputlnt decreases by 1 in each iteration so the while loop executes 4 times for inputlnt values 3, 2, 1, 0.
1st Iteration
testStr = "This is a test"
2nd Iteration
testStr = "his is a tes"
3rd Iteration
testStr = "is is a te"
4th Iteration
testStr = "s is a t"
(iii) Given the input integer 2, what output is produced by Line 3?
- 0
- 1
- 2
- 3
- None of these
Answer
Option 5 — None of these
Explanation
Value of inputlnt will be -1 as till inputlnt >= 0 the while loop will continue executing.
(iv) Given the input integer 2, what output is produced by Line 4?
- False
- True
- 0
- 1
- None of these
Answer
Option 2 — True
Explanation
As input is 2 and inside the while loop, inputlnt decreases by 1 in each iteration so the while loop executes 3 times for inputlnt values 2, 1, 0.
1st Iteration
testStr = "This is a test"
2nd Iteration
testStr = "his is a tes"
3rd Iteration
testStr = "is is a te"
After the while loop finishes executing, value of testStr is "is is a te". 't' in testStr returns True as letter t is present in testStr.