CBSE Class 11 Computer Science Question 65 of 91

String Manipulation — Question 10

Back to all questions
10
Question

Question 4

Carefully go through the code given below and answer the questions based on it :

testStr = "abcdefghi"                    
inputStr = input ("Enter integer:")      
inputlnt = int(inputStr)                 
count = 2                                
newStr = ''                               
while count <= inputlnt :                
    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 (inputlnt)               # Line 5  

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

  1. abcdefg
  2. aabbccddeeffgg
  3. abcdeefgh
  4. ghi
  5. None of these

Answer

Option 3 — abcdeefgh

Explanation

Input integer is 4 so while loop will execute 3 times for values of count as 2, 3, 4.

1st Iteration
    newStr = newStr + testStr[0:2]
⇒ newStr = '' + ab
⇒ newStr = ab

    testStr = testStr[2:]
⇒ testStr = cdefghi

2nd Iteration
    newStr = newStr + testStr[0:3]
⇒ newStr = ab + cde
⇒ newStr = abcde

    testStr = testStr[2:]
⇒ testStr = efghi

3rd Iteration
    newStr = newStr + testStr[0:4]
⇒ newStr = abcde + efgh
⇒ newStr = abcdeefgh

    testStr = testStr[2:]
⇒ testStr = ghi

(ii) Given the input integer 4, what output is produced by Line 3?

  1. abcdefg
  2. aabbccddeeffgg
  3. abcdeefgh
  4. ghi
  5. None of these

Answer

Option 4 — ghi

Explanation

Input integer is 4 so while loop will execute 3 times for values of count as 2, 3, 4.

1st Iteration
    testStr = testStr[2:]
⇒ testStr = cdefghi

2nd Iteration
    testStr = testStr[2:]
⇒ testStr = efghi

3rd Iteration
    testStr = testStr[2:]
⇒ testStr = ghi

(iii) Given the input integer 3, what output is produced by Line 4?

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

Answer

Option 5 — None of these

Explanation

Looking at the condition of while loop — while count <= inputlnt, the while loop will stop executing when count becomes greater than inputlnt. Value of inputlnt is 3 so when loop stops executing count will be 4.

(iv) Given the input integer 3, what output is produced by Line 5?

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

Answer

Option 4 — 3

Explanation

The input is converted from string to integer and after that its value is unchanged in the code so line 5 prints the input integer 3.

(v) Which statement is equivalent to the statement found in Line 1?

  1. testStr = testStr[2:0]
  2. testStr = testStr[2:-1]
  3. testStr = testStr[2:-2]
  4. testStr = testStr - 2
  5. None of these

Answer

Option 5 — None of these

Answer