ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
s = 'abc'
t = s
s += 'd'
u = list('abc')
v = u
u += ['d']
print(s, t, u is v, v)- A. abcd abcd True ['a', 'b', 'c', 'd']
- B. abcd abc False ['a', 'b', 'c']
- C. abcd abc True ['a', 'b', 'c']
- D. abcd abc True ['a', 'b', 'c', 'd']
Correct answer: D. abcd abc True ['a', 'b', 'c', 'd']
Strings are immutable, so += creates a new string for s and leaves t alone. Lists are changed in place by +=, so u and v still refer to the same list.
More Python Programming questions
- What is printed? python print(bin(0b1011011).count('1'), (0b1011011).bitcount(), 0b1011011 & (0b1011011 - 1))
- What is printed? python def f(n): c = 0 while n: n &= n - 1 c += 1 return c print(f(255), f(-1 & 0xFF), f(0))
- What is printed? python s = 'abc' print(s[::-1] + s[0], s[1:] + s[:1], s[-1] + s[-2:])
- What is printed? python print(len('é'), len('é'.encode()), len(f'{"é":>3}'), len(str(0.1 + 0.2)))
- What is printed? python print('%-6s|' % 'ab', '%6.2f|' % 3.14159, '%+.1e' % 12345.6789)
- What is printed? python from decimal import Decimal, ROUNDHALFEVEN print(Decimal('2.5').quantize(Decimal('1'), rounding=ROUNDHALFEVEN)…