Computer Science

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 

Given the input integer 2, what output is produced by Line 4?

  1. False
  2. True
  3. 0
  4. 1
  5. None of these

Python String Manipulation

6 Likes

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.

Answered By

1 Like


Related Questions