KnowledgeBoat Logo

Computer Science

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

Given a first input of 12345 and a second input of 246, what result is produced by Line 2?

  1. 369
  2. 246
  3. 234
  4. 345
  5. None of these

Python String Manipulation

2 Likes

Answer

Option 1 — 369

Explanation

For loop executes 3 times as length of smaller input is 3.

1st Iteration
    result = 2 + 1
⇒ result = 3

    newStr = '' + '3'
⇒ newStr = '3'

    large = 2345

2nd Iteration
    result = 4 + 2
⇒ result = 6

    newStr = '3' + '6'
⇒ newStr = '36'

    large = 345

3rd Iteration
    result = 6 + 3
⇒ result = 9

    newStr = '36' + '9'
⇒ newStr = '369'

    large = 45

Final value of newStr is '369'.

Answered By

1 Like


Related Questions